0

I tried to send String arr to Fragment from Activity. Because I have to show listView in fragment by that String arr. But there are still null exception error.. I searched many times to solve this error, but there are still same error. I really don't know what should I do. Please give me some solution.

Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tabmenu_activity);
    final Toolbar toolbar = (android.support.v7.widget.Toolbar) this.findViewById(R.id.toolbar);
    toolbar.setTitleTextColor(Color.parseColor("#000000"));

    backPressCloseHandle = new BackPressCloseHandler(this);

    setting = getSharedPreferences("setting", MODE_PRIVATE);
    editor= setting.edit();
    loginid = setting.getString("Id", "");

    Driver.getInstance().setloginid(setting.getString("Id", ""));

    ApiRequester.getInstance().getDriver(Driver.getInstance().getLoginid(), new ApiRequester.UserCallback<Driver>() {
        @Override
        public void onSuccess(Driver result) {
            toolbar.setTitle(result.getname());
        }
        @Override
        public void onFail() {
        }
    });

    ApiRequester.getInstance().getList(new ApiRequester.UserCallback<List<Case_List>>() {
        @Override
        public void onSuccess(List<Case_List> result) {
                if(result==null)
                {
                    Toast.makeText(TabMenuActivity.this, "정보가 존재하지 않습니다.", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    int size = result.size();
                    list_arr = new String[size];
                    int count = 0;

                    for(Case_List list : result)
                    {
                        list_arr[count] = list.gethabbitname();
                        count++;
                    }
                            for(int i=0; i<list_arr.length; i++)
                                {
                                           System.out.println(i+"number"+list_arr[i]);
                                }
                    Bundle bundle = new Bundle();
                    bundle.putStringArray("list_arr", list_arr);
                    // set Fragmentclass Arguments
                    Frag_ListActivity fragobj = new Frag_ListActivity();
                    fragobj.setArguments(bundle);
                }
        }
        @Override
        public void onFail() {
        }
    });

Fragment

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.activity_frag_list, container, false);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);

    String[] list_arr = getArguments().getStringArray("list_arr");// error is here.

    for(int i=0; i<list_arr.length; i++)
    {
        System.out.println(list_arr[i]);
    }


    return view;


}                             

java.lang.NullPointerException: Attempt to invoke virtual method java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null.

How can I solve this? I don't have enough time:^(

UltimateDevil
  • 2,641
  • 2
  • 15
  • 30
Mark
  • 29
  • 4
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Nongthonbam Tonthoi Nov 05 '17 at 14:54

2 Answers2

0

You have passed the array only if result not null : if it has null value you have not added a default valut :

I think it should be like this :

    ApiRequester.getInstance().getList(new ApiRequester.UserCallback<List<Case_List>>() {
    @Override
    public void onSuccess(List<Case_List> result) {
            if(result==null)
            {
                Toast.makeText(TabMenuActivity.this, "정보가 존재하지 않습니다.", Toast.LENGTH_SHORT).show();
            }
            else
            {
                int size = result.size();
                list_arr = new String[size];
                int count = 0;

                for(Case_List list : result)
                {
                    list_arr[count] = list.gethabbitname();
                    count++;
                }
                        for(int i=0; i<list_arr.length; i++)
                            {
                                       System.out.println(i+"number"+list_arr[i]);
                            }

            }

                Bundle bundle = new Bundle();
                bundle.putStringArray("list_arr", list_arr);
                // set Fragmentclass Arguments
                Frag_ListActivity fragobj = new Frag_ListActivity();
                fragobj.setArguments(bundle);
    }
    @Override
    public void onFail() {
    }
});
Nawrez
  • 3,113
  • 8
  • 25
  • 39
0

A good practice here would be using an interface for communication between the activity and its fragments.

In your Activity class create an interface and pass the data as parameters. Make your fragment class implement it and do your business with the received data in the methods implementation.

AndroidSmoker74
  • 288
  • 4
  • 19