简体   繁体   中英

App crashing when passing bundle value between activity and fragment

I'm trying to send some data from my ListView to another activity through a single option list view fragment. Here is how flow of data is supposed to be:

ListView (upon choosing an option) -> Single option list view fragment (again, upon choosing an option) -> AnotherActivity.

Code which sends the data from ListView to 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");

Code fragment class which receives the data and then sends it to the other activity:

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();
}
}

Code in the OtherActivity which receives the data passed by the fragment:

@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();
}

The problem: When I select an option from the ListView, the app crashes. If I remove the code which deals with passing of the data, the app works fine. That is, if you remove the following code in the ListView:

/*Bundle b = new Bundle();
      b.putString("id", str_projectid);
      Projectviewoptions pv = new Projectviewoptions();
      pv.setArguments(b);*/

and the following, in fragment class:

//Bundle idbundle = getArguments(); 
//String myid= idbundle.getString("id"); 

Here is the 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)

You cannot access the Fragment's arguments outside of any method, simply move this code into onCreateDialog() :

Bundle idbundle; 
String myid; 

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

The problem is that getArguments(); returns null when you try to access it before the Fragment's lifecycle begins, so idbundle.getString() is an NPE.


Addition
You are creating two different copies of the same fragment named pv and newFragment , you should show pv and remove newFragment entirely:

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");
 
  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM