0

I have a problem that i couldn't resolve. When i store many data in the same bundle my navigation drawer (items matching the location of bundle) crash.

This is the example:

public void SelectItem(int possition) {

      Fragment fragment = null;
      Bundle args = new Bundle();
      switch (possition) {

      case 2:
            fragment = new search();
            //args.putString(FragmentThree.ITEM_NAME, dataList.get(possition).getItemName());
            //args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
            break;
      case 3:
            fragment = new com.tutecentral.navigationdrawer.Menu();
            String DishText = ((Spinner) findViewById(R.id.spinnerSetting)). getSelectedItem().toString();
            args.putString("DishType", DishText);
            TextView text=(TextView)findViewById(R.id.price);
            int priceDish=Integer.parseInt(text.getText().toString());
            args.putInt("price", priceDish);
            break;
      case 4:
            fragment = new bill_send();
            TextView textprice=(TextView)findViewById(R.id.TotalFieldMenu);
            int priceTotal=Integer.parseInt(textprice.getText().toString());
            args.putInt("Total_price", priceTotal);
            //args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition).getItemName());
            //args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
            break;
      case 5:
            fragment = new tables();
            //args.putString(FragmentThree.ITEM_NAME, dataList.get(possition).getItemName());
            //args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
            break;
      case 7:
            fragment = new about();
            //args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition).getItemName());
            //args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
            break;
      case 8:
            fragment = new setting();
            //args.putString(FragmentThree.ITEM_NAME, dataList.get(possition).getItemName());
            //args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
            break;
      case 9:
            fragment = new FragmentOne();
            //args.putString(FragmentOne.ITEM_NAME, dataList.get(possition).getItemName());
            //args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
            break;
      case 10:
            fragment = new FragmentTwo();
            //args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition).getItemName());
            //args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
            break;
      case 11:
            fragment = new FragmentThree();
            //args.putString(FragmentThree.ITEM_NAME, dataList.get(possition).getItemName());
            //args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
            break;
      case 12:
            fragment = new FragmentOne();
            //args.putString(FragmentOne.ITEM_NAME, dataList.get(possition).getItemName());
            //args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
            break;
      case 13:
            fragment = new FragmentThree();
            //args.putString(FragmentThree.ITEM_NAME, dataList.get(possition).getItemName());
            //args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
            break;
      default:
            break;
      }

      if(com.tutecentral.navigationdrawer.Menu.actionBar!=null){
          if (com.tutecentral.navigationdrawer.Menu.actionBar.isShowing()){
              com.tutecentral.navigationdrawer.Menu.actionBar.removeAllTabs();
          }

      }

        fragment.setArguments(args);
        FragmentManager frgManager = getFragmentManager();
        frgManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        mDrawerList.setItemChecked(possition, true);
        setTitle(dataList.get(possition).getItemName());
        mDrawerLayout.closeDrawer(mDrawerList);

  }

Notice that when i delete the bundle in case4 or 3 (use 1 of them) the app works.!!! and the error says that : Navigation Drawer on item click null pointer exception.

The two fragments where i recieve data are :

public class bill_send extends Fragment {

public bill_send(){
    // TODO Auto-generated constructor stub
}
TextView textViewPrice;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view=inflater.inflate(R.layout.bill_send, container, false);
    textViewPrice=(TextView) view.findViewById(R.id.produitPriceBill);
    int priceTotal=getArguments().getInt("Total_price");
    textViewPrice.setText(String.valueOf(priceTotal));
    return view;
}

}

And the second (the Menu()):

final int PriceEdited = getArguments().getInt("price");
    final String NameDish=getArguments().getString("DishType");
    textViewPrice.setText(String.valueOf(PriceEdited));
    price=Integer.parseInt(textViewPrice.getText().toString());

Can anyone help? Thank you.

Here is the Logcat :

04-11 18:24:39.102: D/dalvikvm(1069): GC_FOR_ALLOC freed 39K, 5% free 2889K/3016K, paused 53ms, total 56ms
04-11 18:24:39.322: I/dalvikvm-heap(1069): Grow heap (frag case) to 13.447MB for 11059216-byte allocation
04-11 18:24:39.382: D/dalvikvm(1069): GC_FOR_ALLOC freed 2K, 1% free 13686K/13820K, paused 55ms, total 55ms
04-11 18:24:40.992: D/gralloc_goldfish(1069): Emulator without GPU emulation detected.
04-11 18:24:49.632: I/Choreographer(1069): Skipped 873 frames!  The application may be doing too much work on its main thread.
04-11 18:24:49.982: I/Choreographer(1069): Skipped 40 frames!  The application may be doing too much work on its main thread.
04-11 18:25:12.822: I/Choreographer(1069): Skipped 32 frames!  The application may be doing too much work on its main thread.
04-11 18:25:13.032: I/Choreographer(1069): Skipped 30 frames!  The application may be doing too much work on its main thread.
04-11 18:25:13.242: I/Choreographer(1069): Skipped 39 frames!  The application may be doing too much work on its main thread.
04-11 18:25:13.462: I/Choreographer(1069): Skipped 47 frames!  The application may be doing too much work on its main thread.
04-11 18:25:15.622: D/AndroidRuntime(1069): Shutting down VM
04-11 18:25:15.622: W/dalvikvm(1069): threadid=1: thread exiting with uncaught exception (group=0xb3af2b90)
04-11 18:25:15.632: E/AndroidRuntime(1069): FATAL EXCEPTION: main
04-11 18:25:15.632: E/AndroidRuntime(1069): Process: com.tutecentral.navigationdrawer, PID: 1069
04-11 18:25:15.632: E/AndroidRuntime(1069): java.lang.NullPointerException
04-11 18:25:15.632: E/AndroidRuntime(1069):     at com.tutecentral.navigationdrawer.MainActivity.SelectItem(MainActivity.java:130)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at com.tutecentral.navigationdrawer.MainActivity$DrawerItemClickListener.onItemClick(MainActivity.java:241)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at android.widget.AdapterView.performItemClick(AdapterView.java:299)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at android.widget.AbsListView$3.run(AbsListView.java:3638)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at android.os.Handler.handleCallback(Handler.java:733)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at android.os.Looper.loop(Looper.java:137)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at android.app.ActivityThread.main(ActivityThread.java:4998)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at java.lang.reflect.Method.invokeNative(Native Method)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at java.lang.reflect.Method.invoke(Method.java:515)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-11 18:25:15.632: E/AndroidRuntime(1069):     at dalvik.system.NativeStart.main(Native Method)
Amine
  • 225
  • 3
  • 13
  • Please include your LogCat Stacktrace. (The whole error shown in "LogCat view"). The error you provide us does not tell if it is the Bundle or some of the other variables causing the NullPointerException. – chriskvik Apr 11 '14 at 22:09
  • Which is 130th line of your **SelectItem()** – vjdhama Apr 11 '14 at 23:20
  • its : String DishText = ((Spinner) findViewById(R.id.spinnerSetting)). getSelectedItem().toString(); – Amine Apr 11 '14 at 23:22

2 Answers2

2

I was writing a comment but it seems that it's too short to explain the problems. So I'll try to help you, however it will difficult if I don't know the design of your app, but let's try.

You are doing to much work on your activity and your fragments - you are using too much resources in your case: that causes a Shutting Down.

First, you should create an instance for each fragment. This allows you to have: a single place where all the arguments used by the fragment could be bundled up and you don't have to write the code (with bundles) everytime you instantiate a new fragment.
Related to: Best practice for instantiating a new Android Fragment
For each fragment you need to create an Instance, for example in Menu fragment do as follows:

public static Menu newInstance(int price, String type) {
    Menu frag = new Menu();
    Bundle args = new Bundle();
    args.putInt("PRICE", price);
    args.putString("TYPE", type);
    frag.setArguments(args);
    return frag;
}   

This avoid you to create a Bundle inside your Activity. To pass the data from your activity, you should do:

...
case 3:
    String dish_type = ((Spinner) findViewById(R.id.spinnerSetting)).getSelectedItem().toString();
    TextView text_price = (TextView) findViewById(R.id.price);
    int priceDish = Integer.parseInt(text.getText().toString());
    fragment = new Menu.newInstance(dish_type, text_price);
    break;
...    

In Google Documentation, you have a great example of this behaviour and also on this SO's post.

Second, keep in mind that Bundle have a limit: the limit is 500kb. However, I think this is not your case here just for an Integer and a String, but it's good to know that.
Related to: Is there some limits in android' bundle?

Third, do a check condition before replacing your fragment with a Log statement to see if your Spinner is null. To avoid the NullPointerException on this you need to create a background thread. I think this is related to the error: "The application may be doing too much work on its main thread". Perhaps, rewrite your app more lighter: use the (default) options menu in ActionBar for secondary items as "Other options", "Settings", "Help" and "About". You can avoid to create multiple items and make your NavigationDrawer more lighter.
You must to check your layout, to see if the spinner has the right id and in your activity if it's the same. I'm really not sure but you can try to do a new thread to display your fragment (I have not tested that, so I really don't know if you can):

final Runnable changeFrag = new Runnable() {
    public void run() {
        // launch your fragment here or call a method to do so
        handler.postDelayed(this, timeout);
    }
};
handler.postDelayed(changeFrag, timeout);  

However, I think the right solution it's to make your activity lighter.

I really hope it will help you.
Good luck and good dev.

NOTE: It will be better for next questions (if you'll have) to well write the context of your app: what you going to do, what you tried, what is the app. Because I cannot figure it out how you built this. However, you don't have to tell all the context (sometimes projects have secrets ^^) but just a bit to understand what is going wrong. Anyway...

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
2

With the help of Fllo i could solve my problem. Effectively the problem was in the main awtivity which was doing too much work, this is way the solution of threads work like a charm :

in the send_bill fragment :
public void setTotal(final String text){ textViewPrice.setText(text); }

public void sentTotal(){
    new MyThread().execute();
}

public class MyThread extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... strings) {
        // TODO Auto-generated method stub
        Bundle b=getArguments();
        totalValue=b.getString("Total");
        return null;
    }

    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        setTotal(totalValue);
    }

}

Then in the Menu Fragment :

1 Declaring the interface :

FragSendTotal mCallback;
public interface FragSendTotal{
    public Bundle sendTotal(String text);
}

2 The button that send data from fragment menu to fragment bill_dend :

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.commander:

        tt =totalPrice.getText().toString();
        mCallback.sendTotal(tt);
        break;

And finally in the main Activity :

1 get the bundle :

@Override
public Bundle sendTotal(String total) {
    // TODO Auto-generated method stub
        Bundle bundleFrag=new Bundle();
        bundleFrag.putString("Total", total);
        getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null).commit();
        return bundleFrag;
    }

2 Use it when instantiate and showing the send_bill fragment :

fragment.setArguments(sendTotal(com.tutecentral.navigationdrawer.Menu.tt));
            frgManager.beginTransaction().add(R.id.content_frame, new bill_send(), "fragBill").commit();
            frgManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null).commit();
            ((bill_send) fragment).sentTotal();

Thanks Fllo for help and i wish this question will help others.

Amine
  • 225
  • 3
  • 13
  • Hi Amine, nice! Really glad to help you. You should accept your own answer because this resolved your issue. Happy coding :) – Blo Apr 16 '14 at 14:20
  • Fllo, Can you Thanks please take a look to my new question, i'will appreciate your help. Thank you again and excuse me for for the inconvenience. – Amine Apr 16 '14 at 16:11