我只是想使用简单的片段,但它显示了一个我无法弄清楚的问题。
我已经尝试了很长时间了。请帮我。它给出了以下错误:
android.view.inflateException:二进制xml文件行#11错误
这是我的代码:
package com.example.myfragmentqadvance;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity implements Fragment1.Communicator {
Fragment1 f1;
Fragment2 f2;
FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager= getFragmentManager();
f1=(Fragment1) manager.findFragmentById(R.id.fragment1);
f1.setCommunicator(this);
}
@Override
public void respond(int index) {
f2=(Fragment2) manager.findFragmentById(R.id.fragment2);
if(f2!=null & f2.isVisible()){
f2.changedData(index);
}else {
Intent intent = new Intent(this,AnotherAcitivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}
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/fragment1"
android:name="com.example.myfragmentqadvance.Fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp" />
</RelativeLayout>
Fragment1.java
package com.example.myfragmentqadvance;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Fragment1 extends Fragment implements OnItemClickListener {
ListView list;
Communicator communicator;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.activity_fragment1, container, false);
list=(ListView)getActivity().findViewById(R.id.listView1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.chapter, android.R.layout.simple_list_item_1);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
return view;
}
public void setCommunicator(Communicator communicator) {
this.communicator = communicator;
}
public interface Communicator{
public void respond(int index);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int i, long arg3) {
communicator.respond(i);
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfragmentqadvance"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myfragmentqadvance.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfragmentqadvance.Fragment1"
android:label="@string/title_activity_fragment1" >
</activity>
<activity
android:name="com.example.myfragmentqadvance.Fragment2"
android:label="@string/title_activity_fragment2" >
</activity>
<activity
android:name="com.example.myfragmentqadvance.AnotherAcitivity"
android:label="@string/title_activity_another_acitivity" >
</activity>
</application>
</manifest>
Logcat 如果有帮助
05-27 06:56:51.768: D/dalvikvm(857): GC_FOR_ALLOC freed 39K, 6% free 2558K/2720K, paused 35ms, total 38ms
05-27 06:56:51.778: I/dalvikvm-heap(857): Grow heap (frag case) to 3.220MB for 635812-byte allocation
05-27 06:56:51.828: D/dalvikvm(857): GC_FOR_ALLOC freed 2K, 5% free 3177K/3344K, paused 47ms, total 47ms
05-27 06:56:51.879: D/dalvikvm(857): GC_CONCURRENT freed <1K, 5% free 3179K/3344K, paused 4ms+4ms, total 57ms
05-27 06:56:51.918: D/AndroidRuntime(857): Shutting down VM
05-27 06:56:51.918: W/dalvikvm(857): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-27 06:56:51.938: E/AndroidRuntime(857): FATAL EXCEPTION: main
05-27 06:56:51.938: E/AndroidRuntime(857): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfragmentqadvance/com.example.
myfragmentqadvance.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
05-27 06:56:51.938: E/AndroidRuntime(857): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-27 06:56:51.938: E/AndroidRuntime(857): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-27 06:56:51.938: E/AndroidRuntime(857): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-27 06:56:51.938: E/AndroidRuntime(857): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-27 06:56:51.938: E/AndroidRuntime(857): at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 06:56:51.938: E/AndroidRuntime(857): at android.os.Looper.loop(Looper.java:137)
05-27 06:56:51.938: E/AndroidRuntime(857): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-27 06:56:51.938: E/AndroidRuntime(857): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 06:56:51.938: E/AndroidRuntime(857): at java.lang.reflect.Method.invoke(Method.java:511)
05-27 06:56:51.938: E/AndroidRuntime(857): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-27 06:56:51.938: E/AndroidRuntime(857): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-27 06:56:51.938: E/AndroidRuntime(857): at dalvik.system.NativeStart.main(Native Method)
05-27 06:56:51.938: E/AndroidRuntime(857): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
05-27 06:56:51.938: E/AndroidRuntime(857): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-27 06:56:51.938: E/AndroidRuntime(857): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
答案 0 :(得分:0)
您可能在.xml文件中有一些小错误。请检查一下这些文件。
解决方案:
您可以将片段添加到相对布局中,但为了正确地执行此操作,您需要为其分配适当的布局参数,以便可以放置它。在包含其内容的相对布局中创建一个FrameLayout会更容易,然后在那里添加片段。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/FragmentContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/B1"/>
</RelativeLayout>
然后
fragmentTransaction.add(R.id.FragmentContainer, fragment);
无需在Manifest文件中将fragments
声明为活动
<activity
android:name="com.example.myfragmentqadvance.Fragment1"
android:label="@string/title_activity_fragment1" >
</activity>
<activity
android:name="com.example.myfragmentqadvance.Fragment2"
android:label="@string/title_activity_fragment2" >
</activity>
删除这部分代码。不需要。
答案 1 :(得分:0)
片段无效,您似乎也想拥有Fragment1。将片段更改为com.example.myfragmentqadvance.Fragment1
答案 2 :(得分:0)
我得到了解决方案。当我使用onActivityCreated它工作...
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.activity_fragment1, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
list=(ListView)getActivity().findViewById(R.id.listView1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.chapter, android.R.layout.simple_list_item_1);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
}
它工作正常,但提供空指针执行