在活动和片段之间传递bundle值时应用程序崩溃

时间:2012-12-30 18:04:55

标签: android android-intent android-fragments

我正在尝试通过单个选项列表视图片段将一些数据从我的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)

1 个答案:

答案 0 :(得分:3)

您无法在任何方法之外访问Fragment的参数,只需将此代码移至onCreateDialog()

Bundle idbundle; 
String myid; 

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    idbundle = getArguments();
    myid = idbundle.getString("id");
    ...

问题是,当您尝试在Fragment的生命周期开始之前访问它时getArguments();返回null,因此idbundle.getString()是NPE。


<强>加成
您正在创建名为pvnewFragment的同一片段的两个不同副本,您应该显示pv并完全删除newFragment

Bundle b = new Bundle();
b.putString("id", str_projectid);
Projectviewoptions pv = new Projectviewoptions();
pv.setArguments(b);
pv.show(getFragmentManager(), "projectoptions");

<击>

<击>
Projectviewoptions newFragment = new Projectviewoptions();
newFragment.show(getFragmentManager(), "projectoptions");

<击>