LayoutManager - 旋转屏幕时出现空指针异常

时间:2017-08-06 01:12:33

标签: java android nullpointerexception android-recyclerview layout-manager

我正在使用一个Android应用程序,使用recyclerview和cardview,一切都很好,但是当我旋转屏幕时,应用程序停止,我在

中得到一个nullPointerException
recyclerView.setLayoutManager(mLayoutManager);

这是一个完整的例外:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.news.press/com.news.press.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3150)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5200)
                  at android.app.ActivityThread.access$1100(ActivityThread.java:218)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1740)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:145)
                  at android.app.ActivityThread.main(ActivityThread.java:6934)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                  at com.news.press.activities.MainActivity.onCreate(MainActivity.java:88)
                  at android.app.Activity.performCreate(Activity.java:6609)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3103)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260) 
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5200) 
                  at android.app.ActivityThread.access$1100(ActivityThread.java:218) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1740) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:145) 
                  at android.app.ActivityThread.main(ActivityThread.java:6934) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

这是我的xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.news.press.activities.MainActivity"
    android:layoutDirection="rtl"
    android:textDirection="rtl">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:scrollbars="none"
        />
</RelativeLayout>

更新

我的Java代码

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        /**
         * setting homepage Content
         */

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        articlesList = new ArrayList<>();
        adapter = new ArticleAdapter(this, articlesList);

        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this,1);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(1, dpToPx(1), true));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);

        prepareArticles();
    }

1 个答案:

答案 0 :(得分:-1)

对我来说,一切似乎都很清楚。

你能否检查一下你没有其他的陆地布局。 也许这个其他布局没有Recyclerview?