初始化reycler视图时出现NullPointerException

时间:2015-03-19 18:59:44

标签: java android nullpointerexception

活动在启动时会在recyclerview上抛出nullPointerException。但我已在onCreate()初始化它

这是logcat输出

FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1457)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:723)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:583)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1457)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:723)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:583)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1457)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:723)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:583)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1858)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1110)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1283)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1008)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4222)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:525)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4793)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
        at dalvik.system.NativeStart.main(Native Method)

onCreate

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

    toolbar = (Toolbar) findViewById(R.id.toolBar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("xContacts");

    ArrayList<String> temp = new ArrayList<>();
    temp.add("one");
    temp.add("two");
    temp.add("three");
    temp.add("four");
    temp.add("five");

    mRecyclerView = (RecyclerView) findViewById(R.id.myRecyler);
    mAdapter = new ContactsListAdapter(MainActivity.this,temp);
    mContactsRecyclerView.setAdapter(mAdapter);
    mContactsRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
}

任何人都可以告诉我为什么它会给出nullpointerexception

1 个答案:

答案 0 :(得分:2)

您必须在适配器之前设置LayoutManager或RecyclerView。