谁能告诉我这只logcat想要什么?android中的碎片
现在我正在尝试在android中创建一个示例片段程序,到目前为止,我一直在关注这个示例以供参考,我从这个网站本身获得了这个项目。 导入此项目后,我无法运行此项目,我遇到了如下logcat之类的问题 谁能告诉我下面的logcat要求什么?在android中 LOGCAT谁能告诉我这只logcat想要什么?android中的碎片,android,android-fragments,Android,Android Fragments,现在我正在尝试在android中创建一个示例片段程序,到目前为止,我一直在关注这个示例以供参考,我从这个网站本身获得了这个项目。 导入此项目后,我无法运行此项目,我遇到了如下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): jav
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
布局土地/活动\u 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>
<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>
<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>
<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>
<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>
布局端口/活动\u 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>
<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>
<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>
<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>
<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>
布局端口/linklist\u layout.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>
<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>
<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>
<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>
<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>
布局端口/row\u layout.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>
<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>
<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>
<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>
<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>
布局端口/web\u layout.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>
<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>
<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>
<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>
<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>
您需要为片段添加一个name属性,以便Android知道要加载哪个片段。应该包含片段类的全名:
android:name="com.example.SomeFragment"
有关详细信息,请参见。您可以这样称呼片段:
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 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" >
在布局代码中使用
片段
布局时,需要提供一些类以加载该片段
布局,如下所示:
<fragment android:name="com.example.android.fragmentclass"
android:id="@+id/listFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
中的android:name
属性指定要在布局中实例化的片段
类。当系统创建此活动布局时,它实例化布局中指定的每个片段,并为每个片段调用onCreateView()
方法,以检索每个片段的布局
- 另一种方法是以编程方式定义并将片段添加到现有视图组。
add()
方法添加片段,指定要添加的片段以及插入片段的视图
例如:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
xml文件中有错误…发布您的xml布局和代码。最可能的情况是,
标记内没有android:name=“com.survivingwithandroid.fragment.YourFragmentHere”
。显示布局代码。@AndrewT@GrlsHu请再次访问我的问题,我已经发布了我的布局。请命名您的框架(给它一个ID)布局!调用它来更改片段时,您需要它。@GrlsHu我正在布局端口和布局区域上使用片段控件,我是否必须为这两个布局提及“android:name”呢?是的,您必须为这两个布局提及名称。另外,在第二种方法中,您可以定义一个标记来表示片段。稍后,您可以使用这些标记进行大量处理。@AstralProjection是的,您是对的。谢谢分享信息。欢迎@prabu:)