2017-06-15 16 views
2

オブジェクト:Androidアプリケーションのクラッシュは、私は、Android Studioでこのコードをプログラムした

1)Karte.java

package barsoftware.suedtirolpointer; 

import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.model.CameraPosition; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class Karte extends AppCompatActivity implements OnMapReadyCallback { 

GoogleMap m_map; 
boolean mapReady=false; 

MarkerOptions Rieserfernerhütte; 

MarkerOptions Dahoam; 


static final CameraPosition SÜDTIROL = CameraPosition.builder() 
     .target(new LatLng(46.470576, 11.339986)) 
     .zoom(8) 
     .bearing(0) 
     .tilt(0) 
     .build(); 


@Override 
public Resources getResources() { 
    return super.getResources(); 
} 

////////////////////////////// onCreate ////////////////////////////// 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //// LAYOUT //// 

    setContentView(R.layout.karte); 

    //// KARTEN POI'S //// 
    Rieserfernerhütte = new MarkerOptions() 
      .position(new LatLng(46.5324, 12.0446)) 
      .title("Rieserfernerhütte"); 

    Dahoam = new MarkerOptions() 
      .position(new LatLng(46.738886, 12.166471)) 
      .title("Dahoam"); 

    //// KARTEN IMPLEMENTIERUNG //// 
    MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

    //// UP BOOTON //// 

    ActionBar ab = getSupportActionBar(); 
    ab.setDisplayHomeAsUpEnabled(true); 

} 

@Override 
public void onMapReady(GoogleMap map) { 
    mapReady = true; 
    m_map = map; 
    m_map.addMarker(Rieserfernerhütte); 
    m_map.addMarker(Dahoam); 
    flyTo(SÜDTIROL); 
} 

private void flyTo(CameraPosition target) 
{ 
    m_map.moveCamera(CameraUpdateFactory.newCameraPosition(target)); 
} 




////////////////////////////////// MENU ////////////////////////////////// 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_no_karte, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    if (id == R.id.menu_home) { 
     Intent Home = new Intent(Karte.this, 
       Start.class); 
     startActivity(Home); 
    } 

    if (id == R.id.menu_karte) { 

    } 

    if (id == R.id.menu_teilnehmer) { 
     Intent Teilnehmer = new Intent(Karte.this, 
       Teilnehmer.class); 
     startActivity(Teilnehmer); 
    } 

    if (id == R.id.menu_einstellungen) { 
     Intent Einstellungen = new Intent(Karte.this, 
       Einstellungen.class); 
     startActivity(Einstellungen); 
    } 
    if (id == R.id.menu_update) { 
     Intent Update = new Intent(Karte.this, 
       Update.class); 
     startActivity(Update); 
    } 
    if (id == R.id.menu_teilen) { 
     Intent Teilen = new Intent(Karte.this, 
       Teilen.class); 
     startActivity(Teilen); 
    } 

    return super.onOptionsItemSelected(item); 
    } 

} 

2)karte.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:map="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

<fragment 
    android:id="@+id/map" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment" /> 

</RelativeLayout> 

3)Androidマニフェスト

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="barsoftware.suedtirolpointer"> 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

<uses-feature android:glEsVersion="0x00020000" 
       android:required="true"/> 


<application 

    android:allowBackup="true" 
    android:label="@string/app_name" 
    android:icon="@drawable/icon" 
    android:theme="@style/AppTheme"> 

    <activity 
     android:name=".Start" 
     android:label="@string/act_home" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity android:name=".Karte" 
       android:label="@string/act_karte" 
       android:parentActivityName=".Start"/> 

    <activity android:name=".Teilnehmer" 
       android:label="@string/act_teilnehmer" 
       android:parentActivityName=".Start"/> 

    <activity android:name=".Einstellungen" 
       android:label="@string/act_einstellungen" 
       android:parentActivityName=".Start"/> 

    <activity android:name=".Update" 
       android:label="@string/act_update" 
       android:parentActivityName=".Start"/> 

    <activity android:name=".Teilen" 
       android:label="@string/act_teilen" 
       android:parentActivityName=".Start"/> 

    <activity android:name=".Permission" 
       android:label="Permission"/> 

    <meta-data android:name="com.google.android.gms.version" 
       android:value="@integer/google_play_services_version"/> 

    <meta-data android:name="com.google.android.geo.API_KEY" 
       android:value="AIzaSyCT_CaCyQ2uLFclTcrWex-AEmJ1aHWhR08"/> 

</application> 
</manifest> 

私の電話やエミュレータでアプリケーションを実行すると、アプリケーションがクラッシュします。 Android-Studioにはエラーやバグはありません。誰でも私を見せてもらえますか?エラーは何ですか?

+0

エラーとは何か、エラーも投稿してください。 – Jarvis

+0

@Jarvis私はエラーは発生していないが、起動時にアプリがクラッシュする –

答えて

1

あなたは 「SupportMapFragment」が、中を使用しているレイアウトファイルにKevinnオニールから説明方法だけをjavaファイルではありません。 .javaファイルに置き換える:

<?xml version="1.0" encoding="utf-8"?> 
:あなたが最初の行を書いていなかった

SupportMapFragment supportMapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
supportMapFragment.getMapAsync(this); 

も.xmlファイル(アンドロイドManifestrとレイアウトファイル)に:

MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map); 
mapFragment.getMapAsync(this); 

.xmlが終わるすべてのファイルにこの行を追加します。 私はあなたを助けてくれることを願っています。

2

karte.xmlではSupportMapFragmentを使用していますが、Karte.javaではSupportFragmentManagerを使用していません。

SupportMapFragmentあなたはonCreate()でこれをしようと使用しているのハンドルを取得するには、次の

SupportMapFragment supportMapFragment = 
    (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
supportMapFragment.getMapAsync(this); 
関連する問題