在Android中膨胀类片段时出错
我是开发Android程序的新手,一直在努力完成专业Android 4书籍中介绍片段的练习。我得到以下错误:在Android中膨胀类片段时出错,android,Android,我是开发Android程序的新手,一直在努力完成专业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.a
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();
}
下面是我的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>
<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"
/>
运行调试器时,我发现在以下指令中引发了异常:
aa=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,todoItems)
todoListFragment.setListAdapter(aa)
我注意到todoItems的值为null,所以我想知道是否必须用值初始化该元素。它在这里查找片段:
com.paad.todolist.NewItemFragment
这就是xml文件中的内容
然而,实际实施的程序包就在这里:
package com.example.todolist;
因此,您需要将xml文件更改为:
<fragment android:name="com.example.todolist.NewItemFragment"
MainActivity必须扩展FragmentActivity类使用新的fragment类替换导入的包android.app.fragment\
到android.support.v4.app.Fragment,它将帮助您。我认为您需要使用FragmentActivity而不是activity扩展您的活动。还可以发布日志猫的堆栈跟踪吗?为什么在MainActivity中为ToDoListFragment设置ArrayAdapter?更改此行:视图=充气器。充气(R.layout.new_item_fragment,container,false);to:视图=充气机。充气(R.layout.new\u item\u fragment,null);请发布整个堆栈跟踪。下面是logcat的堆栈跟踪。如果我为任何片段类从android.support.v4.app导入,那么getFragmentManager将无法解析。那么这个函数有替代品吗?谢谢你指出包名错误。我做了更改以指向正确的包,现在我得到一个不同的错误,即02-21 21:17:45.319:E/AndroidRuntime(1752):java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.todolist/com.example.todolist.MainActivity}:java.lang.NullPointerException知道是什么原因导致的吗?看看NPE在Main活动中出现的行号。我不知道从LogCat哪里获取行号。我只能看到PID和TID的5134。我试图设置中断并运行调试器,但什么也没发生。我需要从Visual Studio进行转换,并学习如何运行调试器。运行调试器时,我发现异常是在指令aa=new ArrayAdapter(这是android.R.layout.simple_list_item_1,todoItems)处引发的; todoListFragment.setListAdapter(aa);我注意到todoItems的值为null,所以我想知道是否必须用值初始化该元素
package com.example.todolist;
<fragment android:name="com.example.todolist.NewItemFragment"