Jamie Smith
Jamie Smith

Reputation: 131

The variable I pass from my activity to my fragment becomes null?

I've been running some tests on each method using a toast to display the index of the ListItem that the user clicks on. This worked up until i tested the index value in the fragment which was meant to receive it, in which case i got this error: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference. So I don't completely understand what I am doing wrong here. Here's where the value is getting passed from and to...

MainActivity (Passes index value to Fragment)

@Override
public void onIndexSelected(int index) {

    fm.beginTransaction().replace(R.id.content_Frame, new PlayerFragment()).commit();
    player.setSong(index);
}

PlayerFragment (Receives index from Activity)

public void setSong(int songIndex){

    currentSongIndex = songIndex;
    Toast.makeText(getActivity(), "Index number : " + " " + currentSongIndex, Toast.LENGTH_SHORT).show();

    // Commented out for testing
    //playSong(currentSongIndex);
}

At this point, I am assuming the index value becomes null, hence forcing the app to crash. Any solutions? Thanks!

EDIT

LogCat

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                                                                          at android.widget.Toast.<init>(Toast.java:103)
                                                                          at android.widget.Toast.makeText(Toast.java:260)
                                                                          at mymusic.fragments.PlayerFragment.setSong(PlayerFragment.java:263)
                                                                          at com.music.mymusic.MainActivity.onIndexSelected(MainActivity.java:144)
                                                                          at mymusic.fragments.LibraryFragment$2.onItemClick(LibraryFragment.java:80)
                                                                          at android.widget.AdapterView.performItemClick(AdapterView.java:305)
                                                                          at android.widget.AbsListView.performItemClick(AbsListView.java:1201)
                                                                          at android.widget.AbsListView$PerformClick.run(AbsListView.java:3218)
                                                                          at android.widget.AbsListView$4.run(AbsListView.java:4032)
                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:135)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5289)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

Upvotes: 2

Views: 596

Answers (2)

Monika Aggarwal
Monika Aggarwal

Reputation: 49

Add the following as a first line in onCreateView

HomeActivity parent = (HomeActivity) getActivity();

and then use parent instead of getActivity in your toast like below.

Toast.makeText(parent, error, Toast.LENGTH_LONG).show();

Upvotes: 0

aelimill
aelimill

Reputation: 1015

Is player instance of PlayerFragment? If yes, then the error lies in

@Override
public void onIndexSelected(int index) {

    fm.beginTransaction().replace(R.id.content_Frame, new PlayerFragment()).commit();
    player.setSong(index);
}

You create new PlayerFragment, not using the old one.

The error caused not by parameter, but by getActivity() as fragment not attached to any activity

Upvotes: 1

Related Questions