我正在尝试通过一本书及其示例来学习 Android。我在实现一个涉及片段的应用程序时遇到了困难。这个应用程序应该作为一个 Todo 列表工作,其中包含一个 EditText 来插入新项目和一个 List 来显示它们。
当我启动应用程序时,我在主 Activity 中的 setContentView() 上遇到问题,并且收到消息“不幸的是,TodoList2 已停止”。我尝试使用支持库并为 API 级别 11 或更高级别进行编译,但我得到了同样的错误。
下面是我在崩溃后看到的 logcat:
E/AndroidRuntime(4185): FATAL EXCEPTION: main
E/AndroidRuntime(4185): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.aledev.todolist2/it.aledev.todolist2.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
E/AndroidRuntime(4185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
E/AndroidRuntime(4185): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(4185): at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(4185): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime(4185): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(4185): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(4185): at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime(4185): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(4185): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(4185): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(4185): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(4185): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(4185): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
E/AndroidRuntime(4185): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
E/AndroidRuntime(4185): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime(4185): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(4185): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(4185): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime(4185): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
E/AndroidRuntime(4185): at android.app.Activity.setContentView(Activity.java:1881)
E/AndroidRuntime(4185): at it.aledev.todolist2.MainActivity.onCreate(MainActivity.java:18)
E/AndroidRuntime(4185): at android.app.Activity.performCreate(Activity.java:5104)
E/AndroidRuntime(4185): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
E/AndroidRuntime(4185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
E/AndroidRuntime(4185): ... 11 more
E/AndroidRuntime(4185): Caused by: java.lang.NullPointerException
E/AndroidRuntime(4185): at it.aledev.todolist2.NewItemFragment.onCreateView(NewItemFragment.java:19)
E/AndroidRuntime(4185): at android.app.Fragment.performCreateView(Fragment.java:1695)
E/AndroidRuntime(4185): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
E/AndroidRuntime(4185): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
E/AndroidRuntime(4185): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
E/AndroidRuntime(4185): at android.app.Activity.onCreateView(Activity.java:4717)
E/AndroidRuntime(4185): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
E/AndroidRuntime(4185): ... 21 more
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.aledev.todolist2"
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="it.aledev.todolist2.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>
</application>
</manifest>
这是我的主要活动 xml (activity_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="vertical"
>
<fragment
android:name="it.aledev.todolist2.NewItemFragment"
android:id="@+id/new_item_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<fragment
android:name="it.aledev.todolist2.ToDoListFragment"
android:id="@+id/todo_list_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
这是我的包含 EditText 的片段的 xml:
<?xml version="1.0" encoding="utf-8"?>
<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:contentDescription="@string/add_item_content_decription"
>
</EditText>
它包含一个警告,因为未指定提示属性,但放置它并不能解决我遇到的问题。
这是 MainActivity 代码:
package it.aledev.todolist2;
import java.util.ArrayList;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
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 fragment manager
FragmentManager fm=getFragmentManager();
ToDoListFragment todoListFragment=(ToDoListFragment)fm.findFragmentById(R.id.todo_list_fragment);
// Create the array list of to do items
todoItems=new ArrayList<String>();
// Create the array adapter to bind the array to the ListView
aa=new ArrayAdapter<String>(this,android.R.id.list,todoItems);
// Bind the ArrayAdapter to the ListView
todoListFragment.setListAdapter(aa);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onNewItemAdded(String newItem) {
// TODO Auto-generated method stub
todoItems.add(newItem);
aa.notifyDataSetChanged();
}
}
我可以在 onCreate() 中调用 setContentView(),但执行永远不会到达下一行代码。
这里有与NewFragmentItem相关的代码:
package it.aledev.todolist2;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class NewItemFragment extends Fragment {
private OnNewItemAddedListener onNewItemAddedListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
View view=inflater.inflate(R.layout.new_item_fragment, container, false);
final EditText myEditText=(EditText)getActivity().findViewById(R.id.myEditText);
myEditText.setOnKeyListener(new View.OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
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;
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
try{
onNewItemAddedListener=(OnNewItemAddedListener)activity;
}
catch(ClassCastException e){
throw new ClassCastException(activity.toString()+" must implement OnNewItemAddedListener");
}
}
public interface OnNewItemAddedListener{
public void onNewItemAdded(String newItem);
}
}
我在涉及谷歌地图的应用程序中发现了类似的问题,但我不使用地图。所有问题似乎都来自不连贯性,例如不使用支持库或在模拟器/设备上使用错误的 API。我正在为 API 级别 11 或更高级别进行编译,目标 SDK 设置为 17。我为模拟器设置了 API 级别 17。
我真的不明白我的错误在哪里。非常感谢您的帮助。