我有一个带有recycleview的活动。当活动启动时,它会在此循环视图中抛出NPE。我无法理解为什么。
这是onCreate方法,它是init:
protected void onCreate (@Nullable Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.chat_window_activity);
recyclerView = (RecyclerView) findViewById (R.id.recycleview);//here is null
messagesList = Controllers.getMessagesForSingleNumberFromDB (tableName, getApplicationContext ());
recyclerView.setLayoutManager (new LinearLayoutManager (this)); //logs pointing to this line, but I think that problem not here.
RecyclerView.Adapter adapter = new ChatWindowRecycleAdapter ( new ChatWindow(), messagesList);
recyclerView.setAdapter (adapter);
}
也是使用它的xml。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="10dp"
android:orientation="vertical"
android:scrollbars="vertical"
android:layout_above="@+id/sendInputMessageLayout">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:id="@+id/sendInputMessageLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dp"
>
//Some views
</LinearLayout>
</RelativeLayout>
调试器说recyclerView = (RecyclerView) findViewById (R.id.recycleview);
为空,当我通过调用recyclerView == null
检查它是否为null时,它返回false。如何解决这个问题?