我正在尝试在 Android 中学习片段沟通。
为此,我按照教程编写了一个示例应用程序。
现在,在启动我的示例片段时,我会遇到异常,如下所示。
我将我的总java代码附加在异常下面。
请帮我解决这个问题。
05-14 16:10:04.023: E/AndroidRuntime(1361): FATAL EXCEPTION: main
05-14 16:10:04.023: E/AndroidRuntime(1361): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragment_flexibleui/com.example.fragment_flexibleui.MainActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.os.Looper.loop(Looper.java:137)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-14 16:10:04.023: E/AndroidRuntime(1361): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 16:10:04.023: E/AndroidRuntime(1361): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 16:10:04.023: E/AndroidRuntime(1361): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-14 16:10:04.023: E/AndroidRuntime(1361): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-14 16:10:04.023: E/AndroidRuntime(1361): at dalvik.system.NativeStart.main(Native Method)
05-14 16:10:04.023: E/AndroidRuntime(1361): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-14 16:10:04.023: E/AndroidRuntime(1361): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.app.Activity.setContentView(Activity.java:1867)
05-14 16:10:04.023: E/AndroidRuntime(1361): at com.example.fragment_flexibleui.MainActivity.onCreate(MainActivity.java:13)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.app.Activity.performCreate(Activity.java:5008)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-14 16:10:04.023: E/AndroidRuntime(1361): ... 11 more
05-14 16:10:04.023: E/AndroidRuntime(1361): Caused by: java.lang.IllegalStateException: Fragment com.example.fragment_flexibleui.FragmentA did not create a view.
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.app.Activity.onCreateView(Activity.java:4687)
05-14 16:10:04.023: E/AndroidRuntime(1361): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-14 16:10:04.023: E/AndroidRuntime(1361): ... 21 more
MainActivity.java
public class MainActivity extends Activity implements Communicator{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void respond(int i) {
// TODO Auto-generated method stub
FragmentManager manager = getFragmentManager();
FragmentB f2 = (FragmentB) manager.findFragmentById(R.id.fragment2);
f2.changeData(i);
}
}
FragmentA.java
public class FragmentA extends Fragment implements OnItemClickListener{
ListView lv;
Communicator communicator;
public View OnCreateView(LayoutInflater inflater,ViewGroup container,Bundle bundle)
{
View view = inflater.inflate(R.layout.fragment_a, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
communicator = (Communicator)getActivity();
super.onActivityCreated(savedInstanceState);
lv = (ListView)getActivity().findViewById(R.id.listView1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.titles, android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
// TODO Auto-generated method stub
communicator.respond(index);
}
}
FragmentB.java
public class FragmentB extends Fragment{
TextView textview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_b, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
textview = (TextView)getActivity().findViewById(R.id.textView1);
}
public void changeData(int i)
{
Resources res = getResources();
String[] descrStrings = res.getStringArray(R.array.descriptions);
textview.setText(descrStrings[i]);
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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:background="#F00">
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragment_flexibleui.FragmentA"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragment_flexibleui.FragmentB"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
答案 0 :(得分:0)
更改此
View view = inflater.inflate(R.layout.fragment_a, container, false);
这个
View view = inflater.inflate(R.layout.fragment_a, null);
检查此演员表是否有效,因为我对此表示怀疑。
communicator = (Communicator)getActivity();
新编辑:
public class FragmentB extends Fragment{
TextView textview;
View view; // change this
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.fragment_b, container, false);//change this
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
textview = (TextView)view.findViewById(R.id.textView1);//Change this
}
public void changeData(int i)
{
Resources res = getResources();
String[] descrStrings = res.getStringArray(R.array.descriptions);
textview.setText(descrStrings[i]);
}
}
新编辑:
public class MainActivity extends FragmentActivity {//version support 4
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
replaceFragment(new FragmentA());
}
private void replaceFragment(Fragment fragment) {
FragmentTransaction frgTransaction = getSupportFragmentManager().beginTransaction();
frgTransaction.replace(R.id.frame, fragment);
frgTransaction.commit();
}
}
//Your main XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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:background="#F00">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>