我正在学习如何使用Android中的片段,我在谷歌片段示例中构建了一个样本片段。但是它一直给我这个错误
08-08 02:31:31.887: E/AndroidRuntime(2073): FATAL EXCEPTION: main
08-08 02:31:31.887: E/AndroidRuntime(2073): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tesingfragments/com.example.tesingfragments.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class Fragment
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.os.Looper.loop(Looper.java:137)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-08 02:31:31.887: E/AndroidRuntime(2073): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 02:31:31.887: E/AndroidRuntime(2073): at java.lang.reflect.Method.invoke(Method.java:525)
08-08 02:31:31.887: E/AndroidRuntime(2073): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-08 02:31:31.887: E/AndroidRuntime(2073): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-08 02:31:31.887: E/AndroidRuntime(2073): at dalvik.system.NativeStart.main(Native Method)
08-08 02:31:31.887: E/AndroidRuntime(2073): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class Fragment
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-08 02:31:31.887: E/AndroidRuntime(2073): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.app.Activity.setContentView(Activity.java:1895)
08-08 02:31:31.887: E/AndroidRuntime(2073): at com.example.tesingfragments.MainActivity.onCreate(MainActivity.java:14)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.app.Activity.performCreate(Activity.java:5133)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-08 02:31:31.887: E/AndroidRuntime(2073): ... 11 more
08-08 02:31:31.887: E/AndroidRuntime(2073): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.Fragment" on path: DexPathList[[zip file "/data/app/com.example.tesingfragments-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.tesingfragments-1, /system/lib]]
08-08 02:31:31.887: E/AndroidRuntime(2073): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
08-08 02:31:31.887: E/AndroidRuntime(2073): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
08-08 02:31:31.887: E/AndroidRuntime(2073): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.view.LayoutInflater.createView(LayoutInflater.java:559)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
08-08 02:31:31.887: E/AndroidRuntime(2073): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
08-08 02:31:31.887: E/AndroidRuntime(2073): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
08-08 02:31:31.887: E/AndroidRuntime(2073): ... 21 more
这就是我所做的。
MainActivity
package com.example.tesingfragments;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MainActivity布局
<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=".MainActivity" >
<fragment
android:name="com.example.tesingfragments.Fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<fragment
android:name="com.example.tesingfragments.Fragment2"
android:id="@+id/fragment2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<!-- <TextView
android:id="@+id/texter1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"
/>-->
</RelativeLayout>
片段1
package com.example.tesingfragments;
import android.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragmenter, container, false);
}
}
片段1布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"
/>
</LinearLayout>
片段2
package com.example.tesingfragments;
import android.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment, container, false);
}
}
片段2布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2"
/>
</LinearLayout>
这是我的资源管理器视图,因此您可以看到我正确地命名了这些类。
答案 0 :(得分:5)
将<Fragment
替换为xml中的&lt; fragment
。
查看文档以获取更多信息
http://developer.android.com/guide/components/fragments.html
编辑:
同时扩展Activity
而不是FragmentActivity
。