我正在尝试使用RecyclerViews的第一步。我走得很远,我理解一切。但是,在尝试运行代码时,我收到“找不到id ...的错误视图”错误。
这是DictionaryFragment的onCreateView方法:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dictionary_list, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.dictionaryList);
adapter = new DictionaryAdapter(getActivity(), getDictionaryData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}
dictionary_list.xml如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/grey"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/rounded_corners"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/dictionaryList"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
这是错误消息:
04-16 16:15:15.119: E/AndroidRuntime(15843): FATAL EXCEPTION: main
04-16 16:15:15.119: E/AndroidRuntime(15843): Process: com.tonestar.dict, PID: 15843
04-16 16:15:15.119: E/AndroidRuntime(15843): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tonestar.dict/com.tonestar.dict.view.dictionary.DictionaryActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0a003f (com.tonestar.dict:id/container) for fragment DictionaryFragment{3b7c893c #0 id=0x7f0a003f}
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.app.ActivityThread.access$800(ActivityThread.java:151)
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.os.Handler.dispatchMessage(Handler.java:102)
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.os.Looper.loop(Looper.java:135)
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.app.ActivityThread.main(ActivityThread.java:5254)
04-16 16:15:15.119: E/AndroidRuntime(15843): at java.lang.reflect.Method.invoke(Native Method)
04-16 16:15:15.119: E/AndroidRuntime(15843): at java.lang.reflect.Method.invoke(Method.java:372)
04-16 16:15:15.119: E/AndroidRuntime(15843): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-16 16:15:15.119: E/AndroidRuntime(15843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-16 16:15:15.119: E/AndroidRuntime(15843): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0a003f (com.tonestar.dict:id/container) for fragment DictionaryFragment{3b7c893c #0 id=0x7f0a003f}
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:886)
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.app.BackStackRecord.run(BackStackRecord.java:834)
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.app.Activity.performStart(Activity.java:6005)
04-16 16:15:15.119: E/AndroidRuntime(15843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
04-16 16:15:15.119: E/AndroidRuntime(15843): ... 10 more
答案 0 :(得分:0)
您需要在onViewCreated方法
中设置RecyclerView@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = (RecyclerView) rootView.findViewById(R.id.dictionaryList);
adapter = new DictionaryAdapter(getActivity(), getDictionaryData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
并在onCreateView中扩充视图
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.dictionary_list, container, false);
}
这种行为的原因是inflater.inflate实际上并没有实际膨胀视图以避免干扰ui线程并且稍后会异步运行。当inflater完成时onViewCreated被调用。在onCreateView buzt创建视图或调用inflater时执行任何操作通常都是不好的做法。