我試圖在我的ViewPagers片段之一中使用RecyclerView
來獲得交錯網格佈局。當我啓動應用程序,它會立即崩潰,並拋出這個錯誤:NullPointerException,在空對象上設置適配器
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference at xxx.HomeActivity.onCreate(HomeActivity.java:39) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
我首先想到的是,這可能是由其中的觀點被加載,但我不知道該命令所造成的。我有一個ViewPager
與2片段。我在第一個片段XML聲明的RecyclerView
,像這樣:
RES /佈局/ fragment_home.xml:
<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/home_grid"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
我的繼承人活動的相對部分:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
setupToolbar();
setupTabs();
HomeGridAdapter adapter = new HomeGridAdapter(Contacts.createFakeContacts(20));
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.home_grid);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
}
這裏第一個片段Java文件:
public class HomeFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static HomeFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
HomeFragment fragment = new HomeFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
我不認爲我需要發佈RecyclerViews適配器代碼,因爲我不認爲這是問題的一部分,但如果需要我可以。
對這個問題的任何意見,表示讚賞!
編輯:按照要求,我的繼承人activitys XML文件:
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HomeActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="75dp"
style="@style/ToolbarStyle"
>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabMode="scrollable"/>
</android.support.v7.widget.Toolbar>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
你在哪裏實例化的ViewPager?你可以發佈你的活動佈局xml嗎?看起來你的Activity沒有加載ViewPager,所以ViewPager中的RecyclerView將不會在onCreate()中存在,導致null。 – JTY
添加了我的活動xml文件。 viewpager用butterknife綁定更高,'setupTabs()'方法設置TabLayout和ViewPager。我沒有包括它,因爲我不認爲它是問題的一部分。 – Orbit