java.lang.RuntimeException:无法启动活动ComponentInfo:原因:java.lang.NullPointerException

时间:2018-09-13 07:28:27

标签: android

这个问题被问了很多次了,我已经尝试了所有的解决方案,没有任何帮助。帮我弄清楚我的代码中缺少的内容

1)我的MapsActivity oncreate()方法,其中运行时错误指向

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    //iniatilization of views
    location_switch = (MaterialAnimatedSwitch) findViewById(R.id.location_switch);
    location_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(boolean isOnline) {
            if (isOnline) {
                startLocationUpdates();
                displayLocation();
                Snackbar.make(Objects.requireNonNull(mapFragment.getView()), "You are Online", Snackbar.LENGTH_SHORT).show();
            } else {
                stopLocationUpdates();
                mCurrent.remove();
                mMap.clear();
                handler.removeCallbacks(drawpathRunnable);
                Snackbar.make(Objects.requireNonNull(mapFragment.getView()), "You are Offline", Snackbar.LENGTH_SHORT).show();
            }

        }
    });

2。我的活动地图

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/layout_panel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/edtPlace"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="5"
            android:baselineAligned="false"
            android:hint="@string/enter_pickup_location"
            android:inputType="" />

        <Button
            android:id="@+id/btnGo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:baselineAligned="false"
            android:text="@string/go" />
    </LinearLayout>

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/layout_panel"
        tools:context=".MapsActivity" />

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        app:cardElevation="10dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:orientation="horizontal"
            android:weightSum="10">

            <com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitch
                android.id="@+id/location_switch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:ball_press_color="@android:color/white"
                app:ball_release_color="@color/ballReleaseColor"
                app:base_press_color="@color/basePressColor"
                app:base_release_color="@color/baseReleaseColor"
                app:icon_press="@drawable/ic_location_on"
                app:icon_release="@drawable/ic_location_off" />

            <Button

                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="7"
                android:text="@string/find_user" />
        </LinearLayout>
    </android.support.v7.widget.CardView>

</RelativeLayout>`

Logcat错误

 09-13 10:11:30.308 4098-4098/com.example.lucy.first E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lucy.first/com.example.lucy.first.MapsActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5299)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at com.example.lucy.first.MapsActivity.onCreate(MapsActivity.java:185)
        at android.app.Activity.performCreate(Activity.java:5264)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
        at android.os.Handler.dispatchMessage(Handler.java:110) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:5299) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641) 
        at dalvik.system.NativeStart.main(Native Method) 
09-13 10:11:30.578 4098-4098/com.example.lucy.first E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.lucy.first, PID: 4098
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lucy.first/com.example.lucy.first.MapsActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5299)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at com.example.lucy.first.MapsActivity.onCreate(MapsActivity.java:185)
        at android.app.Activity.performCreate(Activity.java:5264)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
        at android.os.Handler.dispatchMessage(Handler.java:110) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:5299) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641) 
        at dalvik.system.NativeStart.main(Native Method) 


`

2 个答案:

答案 0 :(得分:1)

我认为问题出在Objects.requireNonNull(mapFragment.getView()中,如果在该方法内部作为参数传递的对象为null,则此方法将引发NullPointerException

请在这里查看:https://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#requireNonNull(T)

因此,解决方法如下:调用onMapReady()方法后map不为null。因此,请在onMapReady()

中执行此逻辑

更新:按如下所示编写您的onMapReady()

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMap.setTrafficEnabled(false);
    mMap.setIndoorEnabled(false);
    mMap.setBuildingsEnabled(false);
    mMap.getUiSettings().setZoomControlsEnabled(true);

    location_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(boolean isOnline) {
            if (isOnline) {
                startLocationUpdates();
                displayLocation();

                if (mapFragment.getView() != null)
                    Snackbar.make(mapFragment.getView(), "You are Online", Snackbar.LENGTH_SHORT).show();
            } else {
                stopLocationUpdates();
                mCurrent.remove();
                mMap.clear();
                handler.removeCallbacks(drawpathRunnable);

                if (mapFragment.getView() != null)
                    Snackbar.make(mapFragment.getView(), "You are Offline", Snackbar.LENGTH_SHORT).show();
        }

    }
});
}

答案 1 :(得分:0)

这是我的onMapReady()

const data = [
 {prop1: false, {prop2: 'someValue'},
 {prop1: true, {prop2: 'someValue'},
 {prop1: false, {prop2: 'someValue'}
]

const isSomeProp1EqualToTrue = data.forEach(availability => {
  if (availability.prop1 === true) {
    return true
  }

  return false
})

// expected: isSomeProp1EqualToTrue = true (that's what I nedd)
// current: isSomeProp1EqualToTrue = undefined (wrong)