带有AppCompatActivity类的GoogleMap

时间:2016-04-18 16:34:23

标签: android google-maps appcompatactivity

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

1 个答案:

答案 0 :(得分:0)

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