我有两个片段。(其中一个有一个编辑文本和一个按钮,另一个只有一个列表视图)我想使用片段事务动态创建它们。但是当我尝试这样做时,我的应用程序无法启动。我的代码做错了什么?
AddToDoFragment.java
public class AddToDoFragment extends Fragment {
public interface OnToDoAddedListener {
public void OnToDoAdded (String newToDo);
}
private OnToDoAddedListener onToDoAddedListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_to_do_fragment, container, false);
final EditText edittext = (EditText) view.findViewById(R.id.editText);
Button button = (Button) view.findViewById(R.id.addButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String text = edittext.getText().toString();
onToDoAddedListener.OnToDoAdded(text);
edittext.setText("");
}
});
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnToDoAddedListener) {
onToDoAddedListener = (OnToDoAddedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implemenet AddToDoFragment.OnToDoAddedListener");
}
}
}
MainActivity.java
public class MainActivity extends Activity implements AddToDoFragment.OnToDoAddedListener {
private ArrayList<String> todoItems;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null) {
todoItems = new ArrayList<String>();
} else {
todoItems = savedInstanceState.getStringArrayList("todoItemTag");//the tag must match what the variable was saved with in onSaveInstanceState
}
FragmentManager fm = getFragmentManager();
ToDoListFragment listToDo = new ToDoListFragment();
listToDo = (ToDoListFragment) fm.findFragmentById(R.id.list_view_fragment);
if(listToDo == null)
{
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.add_to_do_fragment, new AddToDoFragment());
ft.add(R.id.list_view_fragment, new ToDoListFragment());
ft.commit();
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
listToDo.setListAdapter(adapter);
}
public void OnToDoAdded(String newToDo) {
todoItems.add(newToDo);
adapter.notifyDataSetChanged();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putStringArrayList("todoItemTag", todoItems);
}
}
ToDoListFragment.java
public class ToDoListFragment extends ListFragment{
}
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">
<FrameLayout
android:name="com.example.todolistwithconfiguration.AddToDoFragment"
android:id="@+id/add_to_do_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<FrameLayout
android:name="com.example.todolistwithconfiguration.ToDoListFragment"
android:id="@+id/list_view_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:visibility="gone"/>
日志猫
- 10-29 17:21:59.783: D/AndroidRuntime(788): 关闭虚拟机
- 10-29 17:21:59.783: W/dalvikvm(788): threadid=1: 线程以未捕获的异常退出 (group=0x414c4700)
- 10-29 17:21:59.863:E/AndroidRuntime(788):致命异常:主要
- 10-29 17:21:59.863: E/AndroidRuntime(788): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.todolistwithdynamicfragments/com.example.todolistwithdynamicfragments.MainActivity}: java.lang.NullPointerException
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 android.app.ActivityThread.access$600(ActivityThread.java:141)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 android.os.Handler.dispatchMessage(Handler.java:99)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 android.os.Looper.loop(Looper.java:137)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 android.app.ActivityThread.main(ActivityThread.java:5103)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 java.lang.reflect.Method.invokeNative(Native Method)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 java.lang.reflect.Method.invoke(Method.java:525)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
- 10-29 17:21:59.863: E/AndroidRuntime(788): at dalvik.system.NativeStart.main(Native Method)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 由: java.lang.NullPointerException 引起
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 com.example.todolistwithdynamicfragments.MainActivity.onCreate(MainActivity.java:40)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 android.app.Activity.performCreate(Activity.java:5133)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
- 10-29 17:21:59.863: E/AndroidRuntime(788): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
- 10-29 17:21:59.863: E/AndroidRuntime(788): ... 11 更多
- 10-29 17:22:27.032: I/Process(788): 发送信号。PID:788 SIG:9