0
在啓動時嘗試加載我的片段時出現錯誤。片段佈局XML異常
我的錯誤日誌是在這裏:
08-07 00:02:31.918: E/AndroidRuntime(2652): FATAL EXCEPTION: main
08-07 00:02:31.918: E/AndroidRuntime(2652): java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.ioto.atractr_v2/ca.ioto.atractr_v2.Atractr}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.os.Looper.loop(Looper.java:137)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-07 00:02:31.918: E/AndroidRuntime(2652): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 00:02:31.918: E/AndroidRuntime(2652): at java.lang.reflect.Method.invoke(Method.java:511)
08-07 00:02:31.918: E/AndroidRuntime(2652): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-07 00:02:31.918: E/AndroidRuntime(2652): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-07 00:02:31.918: E/AndroidRuntime(2652): at dalvik.system.NativeStart.main(Native Method)
08-07 00:02:31.918: E/AndroidRuntime(2652): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-07 00:02:31.918: E/AndroidRuntime(2652): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.Activity.setContentView(Activity.java:1881)
08-07 00:02:31.918: E/AndroidRuntime(2652): at ca.ioto.atractr_v2.Atractr.onCreate(Atractr.java:37)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.Activity.performCreate(Activity.java:5104)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-07 00:02:31.918: E/AndroidRuntime(2652): ... 11 more
08-07 00:02:31.918: E/AndroidRuntime(2652): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f080002 type #0x12 is not valid
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2144)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.content.res.Resources.getLayout(Resources.java:853)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-07 00:02:31.918: E/AndroidRuntime(2652): at ca.ioto.atractr_v2.ChatFragment.onCreateView(ChatFragment.java:30)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.Fragment.performCreateView(Fragment.java:1695)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.app.Activity.onCreateView(Activity.java:4717)
08-07 00:02:31.918: E/AndroidRuntime(2652): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
08-07 00:02:31.918: E/AndroidRuntime(2652): ... 21 more
唯一的例外是在setContentView
行我OnCreate
方法存在的:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_atractr);
newFragment = (FindNearestDevicesFragment)getFragmentManager().findFragmentById(R.id.nearest_fragment);
chatFragment = (ChatFragment)getFragmentManager().findFragmentById(R.id.chat_fragment);
if (!chordStarted) {
initializeChord();
}
newFragment.setListener(this);
而對於我的活動XML是在這裏:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Atractr" >
<fragment
android:id="@+id/fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class ="ca.ioto.atractr_v2.FindNearestDevicesFragment"
tools:layout="@layout/findnearestdevices_fragment" />
<fragment
android:id="@+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fragment1"
android:layout_alignTop="@+id/fragment1"
class="ca.ioto.atractr_v2.ChatFragment"
tools:layout="@layout/chat_fragment" />
這很令人沮喪,因爲我試圖做的只是在fragment1
和fragment2
之間切換,現在我甚至無法啓動我的應用程序。
我覺得我錯過了一件非常簡單的事情,我現在看不到它。
好吧,這是什麼導致我的錯誤。 我在'ChatFragment'類的'OnCreateView'方法中使用'R.id.chat_fragment'而不是'R.layout.chat_fragment' – docaholic