2

我正在使用带有谷歌地图的 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'

4

1 回答 1

0

基本上 NullPointerExceptionUnable to start activity发生在onCreate()活动方法出现问题时。当我们更改与此活动相关的布局的任何 xml 值时会发生此错误,如果我们没有在活动中正确映射 xml UI,最后尝试访问另一个布局文件中的 UI。请检查所有映射的元素并给出一个唯一的名称。检查您的内容视图声明和 mapFragment 的值。

于 2016-04-19T10:24:21.453 回答