我使用 android 兼容性支持包在 android 中创建了以下内容
基本上我有一个显示两个选项卡的 FragmentAcitivity。一个 Tab 代表Fragment #1
,第二个是Fragment #2
。Fragment #2 有两个 ListFragment 即 ListFragment #1 和 ListFragment #2 ,但问题是当我尝试这样做时,我遇到了以下错误。
android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at com.myexample.template.TextVoiceFragment.onCreateView(TextVoiceFragment.java:19)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:837)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1359)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:422)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:86)
at android.support.v4.view.ViewPager.populate(ViewPager.java:453)
at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:563)
at android.view.View.dispatchAttachedToWindow(View.java:6015)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1124)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1129)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1129)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1129)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1129)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1129)
at android.view.ViewRoot.performTraversals(ViewRoot.java:731)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4914)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at com.myexample.template.VoiceTemplate.onActivityCreated(VoiceTemplate.java:82)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:858)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1020)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1099)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:263)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)
二进制 XML
<fragment class="com.myexample.template.VoiceTemplate"
android:id="@+id/voiceListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
第 19 行包含TextVoiceFragment.class 的 onCreateView 内部,在这种情况下是 Fragment #2
return inflater.inflate(R.layout.text_voice_fragment, container, false);
第 82 行包含在 VoiceTemplate.class 的 onActivityCreated 内的代码中,它扩展了 ListFragment 和 ListFragment #2
//
getListView().setOnItemClickListener( new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long arg3) {
// TODO Auto-generated method stub
}
});
我尝试用 FragmentActivity 替换 Fragment #2,如下所示
但在那种情况下,我会收到以下错误
java.lang.ClassCastException: com.myexample.template.TextVoiceFragment
这里 TextVoiceFragment 是 Tab 秒