从我的活动传递到我的片段的变量变为空?

时间:2016-03-28 12:39:37

标签: android android-fragments android-activity

我一直在使用toast对每个方法运行一些测试,以显示用户点击的ListItem索引。这直到我测试了要接收它的片段中的索引值,在这种情况下我得到了这个错误:Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference。所以我不完全明白我在这里做错了什么。这里的价值从......传递到......

MainActivity (将索引值传递给Fragment)

@Override
public void onIndexSelected(int index) {

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

PlayerFragment (从活动中接收索引)

public void setSong(int songIndex){

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

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

此时,我假设索引值变为null,因此强制应用程序崩溃。有解决方案吗谢谢!

修改

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)

2 个答案:

答案 0 :(得分:1)

PlayerFragment的玩家实例?如果是,则错误在于

@Override
public void onIndexSelected(int index) {

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

您创建新的PlayerFragment,而不使用旧的。> / / p>

错误不是由参数引起的,而是由getActivity()引起的,因为片段没有附加到任何活动

答案 1 :(得分:0)

在onCreateView

中添加以下内容作为第一行
HomeActivity parent = (HomeActivity) getActivity();

然后在下面的toast中使用parent而不是getActivity。

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