现在我正在尝试在android中创建一个示例片段程序,到目前为止我正在关注此link以供参考,我从该站点本身获得了该项目。 导入这个项目后,我无法运行这个项目,我遇到了类似下面的logcat。
任何人都可以告诉我下面的logcat要求什么?在android。
logcat的
12-30 10:54:05.660: E/AndroidRuntime(22318): FATAL EXCEPTION: main
12-30 10:54:05.660: E/AndroidRuntime(22318): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.survivingwithandroid.fragment/com.survivingwithandroid.fragment.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.app.ActivityThread.access$700(ActivityThread.java:140)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.os.Looper.loop(Looper.java:137)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.app.ActivityThread.main(ActivityThread.java:4921)
12-30 10:54:05.660: E/AndroidRuntime(22318): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 10:54:05.660: E/AndroidRuntime(22318): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 10:54:05.660: E/AndroidRuntime(22318): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-30 10:54:05.660: E/AndroidRuntime(22318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-30 10:54:05.660: E/AndroidRuntime(22318): at dalvik.system.NativeStart.main(Native Method)
12-30 10:54:05.660: E/AndroidRuntime(22318): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-30 10:54:05.660: E/AndroidRuntime(22318): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.app.Activity.setContentView(Activity.java:1924)
12-30 10:54:05.660: E/AndroidRuntime(22318): at com.survivingwithandroid.fragment.MainActivity.onCreate(MainActivity.java:32)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.app.Activity.performCreate(Activity.java:5188)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
12-30 10:54:05.660: E/AndroidRuntime(22318): ... 11 more
12-30 10:54:05.660: E/AndroidRuntime(22318): Caused by: java.lang.NullPointerException: name == null
12-30 10:54:05.660: E/AndroidRuntime(22318): at java.lang.VMClassLoader.findLoadedClass(Native Method)
12-30 10:54:05.660: E/AndroidRuntime(22318): at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:354)
12-30 10:54:05.660: E/AndroidRuntime(22318): at java.lang.ClassLoader.loadClass(ClassLoader.java:491)
12-30 10:54:05.660: E/AndroidRuntime(22318): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.app.Fragment.instantiate(Fragment.java:574)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.app.Fragment.instantiate(Fragment.java:552)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.app.Activity.onCreateView(Activity.java:4831)
12-30 10:54:05.660: E/AndroidRuntime(22318): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
12-30 10:54:05.660: E/AndroidRuntime(22318): ... 21 more
布局脊/ activity_main.xml中
<LinearLayout 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"
android:orientation="horizontal" >
<fragment android:id="@+id/listFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<FrameLayout android:id="@+id/fragPage"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
/>
</LinearLayout>
布局端口/ activity_main.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=".MainActivity" >
<fragment android:id="@+id/listFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
布局端口/ linklist_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="@+id/urls"
android:layout_height="match_parent"
android:layout_width="wrap_content"/>
</LinearLayout>
布局端口/ row_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
布局端口/ web_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView android:id="@+id/webPage"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
答案 0 :(得分:2)
在您使用Fragment
布局的布局代码中,您需要提供一些类来加载Fragement
布局,如下所示:
<fragment android:name="com.example.android.fragmentclass"
android:id="@+id/listFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
由于有两种方法可以实现Fragment
在这种情况下,您可以为片段指定布局属性,就像它是视图一样。例如,这是包含两个片段的活动的布局文件:
<fragment android:name="com.example.android.fragmentclass"
android:id="@+id/listFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:name
中的<fragment>
属性指定要在布局中实例化的Fragment
类。当系统创建此活动布局时,它会实例化布局中指定的每个片段,并为每个片段调用onCreateView()
方法,以检索每个片段的布局。
然后,您可以使用add()
方法添加片段,指定要添加的片段以及要插入的视图。
例如:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
答案 1 :(得分:0)
您需要为片段添加name属性,以便Android知道要加载哪个片段。那应该包含片段类的全名:
android:name="com.example.SomeFragment"
有关Android developer website的更多信息,请参阅
答案 2 :(得分:0)
你调用这样的片段:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment1_3 fragment1_3 = new Fragment1_3();
fragmentTransaction.replace(R.id.fragemnt1_2, fragment1_3, "Fragment1_3");
fragmentTransaction.addToBackStack("Fragment1_3");
fragmentTransaction.commit();
R.id.fragemnt1_2
是当前片段的帧布局的ID。用那个!
你的FrameLayout应该有:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragemnt1_2" // This is referenced in the call fragmentTransaction.replace(R.id.fragemnt1_2, fragment1_3, "Fragment1_3");
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >