我正在尝试通过单个选项列表视图片段将一些数据从我的 ListView 发送到另一个活动。数据流应该是这样的:
ListView(在选择一个选项时)-> 单个选项列表视图片段(再次,在选择一个选项时)-> AnotherActivity。
将数据从 ListView 发送到 Fragment 的代码:
Bundle b = new Bundle();
b.putString("id", str_projectid);
Projectviewoptions pv = new Projectviewoptions();
pv.setArguments(b);
Projectviewoptions newFragment = new Projectviewoptions();
newFragment.show(getFragmentManager(), "projectoptions");
接收数据然后将其发送到其他活动的代码片段类:
public class Projectviewoptions extends DialogFragment {
String[] option_array = new String[] {"Project details","Edit","Delete","Mark Done"};
Bundle idbundle = getArguments();
String myid= idbundle.getString("id");
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Options")
.setItems(option_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch(which) {
case 0: Intent intent = new Intent(getActivity(),ViewProjectDetails.class);
intent.putExtra("id", myid);
startActivity(intent);
break;
}
}
});
return builder.create();
}
}
接收片段传递的数据的 OtherActivity 中的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_project_details);
String id = getIntent().getExtras().getString("id");
Toast.makeText(this, id, Toast.LENGTH_LONG).show();
}
问题: 当我从 ListView 中选择一个选项时,应用程序崩溃。如果我删除处理数据传递的代码,则该应用程序可以正常工作。也就是说,如果您在 ListView 中删除以下代码:
/*Bundle b = new Bundle();
b.putString("id", str_projectid);
Projectviewoptions pv = new Projectviewoptions();
pv.setArguments(b);*/
以及以下,在片段类中:
//Bundle idbundle = getArguments();
//String myid= idbundle.getString("id");
这是LogCat:
12-30 23:21:09.620: W/dalvikvm(7406): threadid=1: thread exiting with uncaught
exception (group=0x409c01f8)
12-30 23:21:09.653: E/AndroidRuntime(7406): FATAL EXCEPTION: main
12-30 23:21:09.653: E/AndroidRuntime(7406): java.lang.NullPointerException
12-30 23:21:09.653: E/AndroidRuntime(7406): at
com.kk.project.Projectviewoptions.<init>(Projectviewoptions.java:14)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
com.kk.project.ProjectExplorer.onListItemClick(ProjectExplorer.java:53)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
android.app.ListActivity$2.onItemClick(ListActivity.java:319)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
android.widget.AdapterView.performItemClick(AdapterView.java:292)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
android.widget.AbsListView.performItemClick(AbsListView.java:1058)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
android.widget.AbsListView$1.run(AbsListView.java:3168)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
android.os.Handler.handleCallback(Handler.java:605)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
android.os.Handler.dispatchMessage(Handler.java:92)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
android.os.Looper.loop(Looper.java:137)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
android.app.ActivityThread.main(ActivityThread.java:4424)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
java.lang.reflect.Method.invokeNative(Native Method)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
java.lang.reflect.Method.invoke(Method.java:511)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-30 23:21:09.653: E/AndroidRuntime(7406): at
dalvik.system.NativeStart.main(Native Method)