FET
FET

Reputation: 942

java.lang.NullPointerException: (ListFragment)

Got this issue on a ListFragment, I think it's adapter's fault, but can't figure out why this is happening:

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

CODE which probably contains mistakes:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getActivity().getIntent();

    ArrayList<String> history = intent.getStringArrayListExtra("HistoryList");
    ListView historyList = getListView();
    ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, history);
    historyList.setAdapter(listAdapter);
}

Fragment which launches History one:

public class PrimaryFragment extends Fragment {

    @Nullable
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.primary_layout, null);
    }


    ArrayList<String> history = new ArrayList<String>();
    //START//

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NumberFormat nf = new DecimalFormat("##.###");
        SharedPreferences mDefaultPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        Set<String> set = mDefaultPreferences.getStringSet("key", null);
        if(set != null){
            history = new ArrayList<String>(set);
        }
        TextView money = (TextView) getActivity().findViewById(R.id.textCool);
        String storedValue = mDefaultPreferences.getString("last known value", String.valueOf(0));
        money.setText(storedValue);

    }

+

public void history(View view){

    Intent intent = new Intent(getActivity(), History.class);
    intent.putStringArrayListExtra("HistoryList", history);
    startActivity(intent);

}

Most relevant part of logcat:

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 10-23 21:01:32.268 12036-12036/com.androidbelieve.drawerwithswipetabs E/AndroidRuntime: at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:337) 10-23 21:01:32.268 12036-12036/com.androidbelieve.drawerwithswipetabs E/AndroidRuntime: at android.widget.ListView.setAdapter(ListView.java:491) 10-23 21:01:32.268 12036-12036/com.androidbelieve.drawerwithswipetabs E/AndroidRuntime: at com.androidbelieve.drawerwithswipetabs.History.onActivityCreated(History.java:26)

EDIT:

I think I'm starting to understand what the error is:

I'm populating a ListFragment with an ArrayList which got its value in another Fragment, the Primary one, but this code which was first used on an Activities based App, used a Button to pass those values to the other Activity. But now these are both fragments and are in the same ViewPager, then I've no Buttons to launch the History Fragment, I just have to swipe. So my new question is:

How do I pass that ArrayString value to the History ListFragment knowing I can't launch it through a Button but I do it with a Swipe? I've tried to study the LifeCycle but couldn't come up with anything, let me know what you'll figure out!

Upvotes: 2

Views: 285

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191743

I recommend using Bundle's with your Intent and sticking the ArrayList in the Bundle via Bundle.putParcelableArrayList().

Intent intent = new Intent(getActivity(), History.class);
Bundle extras = new Bundle();
extras.putParcelableArrayList("HistoryList", history);
// intent.putExtras(extras);
startActivity(intent, extras); // can put as above, or do this instead

+

Intent intent = getActivity().getIntent();
Bundle extras = intent.getExtras();
ArrayList<String> history = extras.getParcelableArrayList("HistoryList");

Since you are using a Fragment, you should be able do it this way. Or if you prefer not to deal with making a Bundle object in your Activity, you could do it from within the Fragment.

Bundle extras = new Bundle();
extras.putParcelableArrayList("HistoryList", history);
MyFragment frag = MyFragment.getInstance();
frag.setArguments(extras);
/* Fragment Transition code */

+

public class MyFragment {
    public MyFragment() { ... } 

    public MyFragment getInstance( ) { ... } 

    public void onCreate( ... ) {
        this.history = new ArrayList<String>();
    }

    public View onCreateView( ... ) {
        Bundle args = getArguments();
        if (args != null && args.containsKey("History")) {
            List<String> temp = getArguments().getParcelableArrayList("History");
            this.history.addAll(temp);
            getListAdapter().notifyDataSetChanged();
        }
    }
}

Upvotes: 0

Cory Roy
Cory Roy

Reputation: 5609

You need a package prefix when using putStringArrayListExtra

public Intent putStringArrayListExtra (String name, ArrayList value)

Added in API level 1 Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

Parameters name The name of the extra data, with package prefix. value The ArrayList data value.

putStringArrayListExtra docs

You'll need to add the package name to the string you are using to store the array list extra with.

public void history(View view){
  Intent intent = new Intent(getActivity(), History.class);
  intent.putStringArrayListExtra("com.androidbelieve.drawerwithswipetabs.historylist", history);
  startActivity(intent);
}

Upvotes: 2

Related Questions