2017-06-15 108 views
2

我已經在Android Studio中編程驗證碼:的Android應用程序崩潰對象

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

<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

從Kevinn奧尼爾在佈局文件中解釋瞭如何只是你正在使用的 「SupportMapFragment」但在該java文件不是。在替換的.java:

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

有:

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

而且也是在.xml文件(安卓Manifestr和佈局文件)你沒有寫開頭行:

<?xml version="1.0" encoding="utf-8"?> 

將此行添加到以.xml結尾的所有文件。 我希望我能夠幫助你。

2

karte.xml您使用的是SupportMapFragment,但在Karte.java中,您沒有使用SupportFragmentManager

爲了得到一個手柄上SupportMapFragment您使用試試這個在onCreate()

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