我创建了一个活动,包含2个seprat 碎片。在运行时中,错误如下:
以下是代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment
android:name="com.example.lesson1.Fragment1"
android:id="@+id/activity_fragment1"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent"
/>
<fragment
android:name="com.example.lesson1.Fragment2"
android:id="@+id/activity_fragment2"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent"
/>
</LinearLayout>
public class Myfragments extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myfragments);
}
}
错误是:
03-26 11:26:27.808: E/Trace(5610): error opening trace file: No such file or directory (2)
03-26 11:26:31.780: E/AndroidRuntime(5610): FATAL EXCEPTION: main
03-26 11:26:31.780: E/AndroidRuntime(5610): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lesson1/com.example.lesson1.Myfragments}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
03-26 11:26:31.780: E/AndroidRuntime(5610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-26 11:26:31.780: E/AndroidRuntime(5610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-26 11:26:31.780: E/AndroidRuntime(5610): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-26 11:26:31.780: E/AndroidRuntime(5610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-26 11:26:31.780: E/AndroidRuntime(5610): at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 11:26:31.780: E/AndroidRuntime(5610): at android.os.Looper.loop(Looper.java:137)
03-26 11:26:31.780: E/AndroidRuntime(5610): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-26 11:26:31.780: E/AndroidRuntime(5610): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 11:26:31.780: E/AndroidRuntime(5610): at java.lang.reflect.Method.invoke(Method.java:511)
03-26 11:26:31.780: E/AndroidRuntime(5610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-26 11:26:31.780: E/AndroidRuntime(5610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-26 11:26:31.780: E/AndroidRuntime(5610): at dalvik.system.NativeStart.main(Native Method)
03-26 11:26:31.780: E/AndroidRuntime(5610): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
03-26 11:26:31.780: E/AndroidRuntime(5610): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-26 11:26:31.780: E/AndroidRuntime(5610): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-26 11:26:31.780: E/AndroidRuntime(5610): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-26 11:26:31.780: E/AndroidRuntime(5610): at
.
.
.
答案 0 :(得分:1)
我的添加是导入'android.support.v4.app.Fragment
您扩展了正常活动。所以你需要使用import android.app.Fragment
。
如果您想使用支持库中的片段,请展开FragmentActivity
并使用import android.support.v4.app.Fragment
。
Fragment Activity是SupportBased Fragments的基类。