我是开发 Android 程序的新手,一直坚持尝试完成介绍片段的 Professional Android 4 书中的练习。我收到以下错误:
InflateException: Binary XML file line #6: Error inflating class fragment
以下是我的主要活动 java 程序:
package com.example.todolist;
import java.util.ArrayList;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity
implements NewItemFragment.OnNewItemAddedListener {
private ArrayAdapter<String> aa;
private ArrayList<String> todoItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get references to the Fragments
FragmentManager fm = getFragmentManager();
ToDoListFragment todoListFragment =
(ToDoListFragment)fm.findFragmentById(R.id.ToDoListFragment);
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
todoListFragment.setListAdapter(aa);
@Override
public void onNewItemAdded(String newItem) {
// TODO Auto-generated method stub
todoItems.add(newItem);
aa.notifyDataSetChanged();
}
}
ToDoListFragment 类如下所示:
package com.example.todolist;
import android.app.ListFragment;
public class ToDoListFragment extends ListFragment{
}
下面是我的 NewItemFragment 类。
package com.example.todolist;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class NewItemFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_item_fragment, container, false);
final EditText myEditText = (EditText)view.findViewById(R.id.myEditText);
myEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN)
if((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String newItem = myEditText.getText().toString();
onNewItemAddedListener.onNewItemAdded(newItem);
myEditText.setText("");
return true;
}
return false;
}
});
return view;
}
public interface OnNewItemAddedListener {
public void onNewItemAdded(String newItem);
}
private OnNewItemAddedListener onNewItemAddedListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
onNewItemAddedListener = (OnNewItemAddedListener)activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
}
下面是我的 activity_main.xml 文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.paad.todolist.NewItemFragment"
android:id="@+id/NewItemFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/addItemHint"
/>
<fragment android:name="com.paad.todolist.ToDoListFragment"
android:id="@+id/ToDoListFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
最后,这是我的 new_itemFragment xml 文件:
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/addItemHint"
/>
如果有人能告诉我为什么会出现膨胀片段错误,我将不胜感激。我尝试使用 import android.support.v4.app.FragmentManager 但没有奏效并尝试使用 android.support.v4.app.ListFragment 也没有奏效。多年来我一直在使用其他语言进行编程,但到目前为止,使用 Eclipse 对我来说,Android 确实是一个挑战。他们有更好的 IDE 来开发 Android 项目吗?
我尝试导入 android.support.v4.app 片段类,但是当我这样做时 getFragmentManager 函数未解决。下面是 logcat 的转储。
02-21 20:53:27.769: E/AndroidRuntime(1280): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todolist/com.example.todolist.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.os.Looper.loop(Looper.java:137)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-21 20:53:27.769: E/AndroidRuntime(1280): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 20:53:27.769: E/AndroidRuntime(1280): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 20:53:27.769: E/AndroidRuntime(1280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-21 20:53:27.769: E/AndroidRuntime(1280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-21 20:53:27.769: E/AndroidRuntime(1280): at dalvik.system.NativeStart.main(Native Method)
02-21 20:53:27.769: E/AndroidRuntime(1280): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-21 20:53:27.769: E/AndroidRuntime(1280): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.Activity.setContentView(Activity.java:1881)
02-21 20:53:27.769: E/AndroidRuntime(1280): at com.example.todolist.MainActivity.onCreate(MainActivity.java:20)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.Activity.performCreate(Activity.java:5104)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-21 20:53:27.769: E/AndroidRuntime(1280): ... 11 more
02-21 20:53:27.769: E/AndroidRuntime(1280): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.paad.todolist.NewItemFragment: make sure class name exists, is public, and has an empty constructor that is public
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.Fragment.instantiate(Fragment.java:592)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.Fragment.instantiate(Fragment.java:560)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.app.Activity.onCreateView(Activity.java:4709)
02-21 20:53:27.769: E/AndroidRuntime(1280): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
02-21 20:53:27.769: E/AndroidRuntime(1280): ... 21 more
02-21 20:53:27.769: E/AndroidRuntime(1280): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.paad.todolist.NewItemFragment" on path: /data/app/com.example.todolist-1.apk
02-21 20:53:27.769: E/AndroidRuntime(1280): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
02-21 20:53:27.769: E/AndroidRuntime(1280): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-21 20:53:27.769: E/AndroidRuntime(1280): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
运行调试器,我发现在这些指令中抛出了异常:
aa = new ArrayAdapter(this, android.R.layout.simple_list_item_1, todoItems);
todoListFragment.setListAdapter(aa);
我注意到 todoItems 的值为 null 所以我想知道该元素是否必须用一个值初始化。