2016-04-18 59 views
2

我使用AppCompatActivity擴展的類與谷歌地圖,我有下面的代碼與類和創建方法的GoogleMap與AppCompatActivity類

import android.support.v7.app.AppCompatActivity; 
 

 
public class MainHistoryActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener,OnMapReadyCallback { 
 

 
private GoogleMap mMap; 
 

 

 

 
    protected void onCreate(Bundle savedInstanceState) { 
 
      super.onCreate(savedInstanceState); 
 
      setContentView(R.layout.activity_main_history); 
 
      
 

 

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

 
    } 
 

 
    @Override 
 
     public void onMapReady(GoogleMap googleMap) { 
 
      mMap = googleMap; 
 

 
      // Add a marker in Sydney and move the camera 
 
      LatLng sydney = new LatLng(-34, 151); 
 
      mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
 
      mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
 
     }

代碼失敗,下面的錯誤

04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime: Process: com.dbprox.selfpatrol, PID: 14938 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dbprox.selfpatrol/com.dbprox.selfpatrol.MainHistoryActivity}: java.lang.NullPointerException 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2380) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2432) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.app.ActivityThread.access$800(ActivityThread.java:154) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:110) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:193) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5347) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at java.lang.reflect.Method.invokeNative(Native Method) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:515) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at dalvik.system.NativeStart.main(Native Method) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime: Caused by: java.lang.NullPointerException 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at com.dbprox.selfpatrol.MainHistoryActivity.onCreate(MainHistoryActivity.java:59) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.app.Activity.performCreate(Activity.java:5360) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2334) 
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2432)  
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.app.ActivityThread.access$800(ActivityThread.java:154)  
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)  
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:110)  
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:193)  
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5347)  
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at java.lang.reflect.Method.invokeNative(Native Method)  
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:515)  
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835)  
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)  
 
04-19 00:16:00.719 14938-14938/com.dbprox.selfpatrol E/AndroidRuntime:  at dalvik.system.NativeStart.main(Native Method) 

我有以下的的build.gradle聲明的依賴性

compile fileTree(include: ['*.jar'], dir: 'libs') 
 
testCompile 'junit:junit:4.12' 
 
compile 'com.android.support:design:23.1.1' 
 
compile 'com.google.android.gms:play-services:8.4.0' 
 
compile 'com.google.android.gms:play-services-ads:8.3.0' 
 
compile 'com.google.android.gms:play-services-identity:8.3.0' 
 
compile 'com.google.android.gms:play-services-gcm:8.3.0' 
 
compile 'com.android.support:appcompat-v7:23.1.1' 
 
compile 'org.apache.httpcomponents:httpclient:4.5'

+0

顯示您的activity_main_history.xml文件 –

+0

具有相同的問題您是否找到解決方案? –

回答

0

基本上NullPointerExceptionUnable to start activity當有問題的活動onCreate()方法進行。如果我們沒有在我們的活動中正確映射xml UI,並且最後嘗試訪問另一個佈局文件中的UI,則會更改與此活動相關的任何xml佈局值。檢查所有映射的元素並給出一個唯一的名稱。檢查你的內容視圖聲明和mapFragment的值。

+1

如果我刪除他地圖相關的命令一切正常。小號 –