通过片段通信使用片段变量时出现空指针异常

时间:2017-05-05 11:19:19

标签: android android-fragments nullpointerexception fragment

编辑:我已经解决了关于空指针异常的问题。这个问题完全不同。在这里,我不知道为什么slot3被称为空指针,即使我在oncrrateview中声明它。另一点需要注意的是,当我尝试从oncrrateview本身访问变量时,没有错误。只有当我进行片段通信时才会出现错误。那个问题对我的问题没有任何答案。所以请删除重复的标记。

这是我的代码。这是我在stackoverflow中的第一个问题。请忍受我的询问方式。

public class HeaderScrollFragment extends Fragmen {

OnModeSelectedListener modeCallBack;
public View myFragmentView;
Button slot1,slot2,slot3,slot4,slot5;
int selectedMode;


public interface OnModeSelectedListener{
    public void OnModeChangedCallBack(int x);
    public void inputDataCallback(int y);
}


@Override
public void onAttach(Context context) {
    super.onAttach(context);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        modeCallBack = (HeaderScrollFragment.OnModeSelectedListener) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString()
                + " must implement OnHeadlineSelectedListener");
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myFragmentView = inflater.inflate(R.layout.activity_header_scroll,container,false);
    manyScreens();

    slot1 = (Button) myFragmentView.findViewById(R.id.header_slot1);
    slot2 = (Button) myFragmentView.findViewById(R.id.header_slot2);
    slot3 = (Button) myFragmentView.findViewById(R.id.header_slot3);
    slot4 = (Button) myFragmentView.findViewById(R.id.header_slot4);
    slot5 = (Button) myFragmentView.findViewById(R.id.header_slot5);
    slot1.setOnClickListener(this);
    slot2.setOnClickListener(this);
    slot3.setOnClickListener(this);
    slot4.setOnClickListener(this);
    slot5.setOnClickListener(this);
    selectedMode = 3;
    return myFragmentView;
}



private void manyScreens() {

}

public void onTopicChanged(int x){
    switch (x){
        case 1:
            slot3.setText(R.string.next_m);
            slot5.setText(R.string.mg);
            break;
        case 2:
            slot3.setText(R.string.next_e);
            slot5.setText(R.string.tg);
            break;
        case 3:
            slot3.setText(R.string.next_v);
            slot5.setText(R.string.y_g);
            break;
    }
    onModeChanged(3);
}

public void onModeChanged(int x){

    slot1.setSelected(false);
    slot2.setSelected(false);
    slot3.setSelected(false);
    slot4.setSelected(false);
    slot5.setSelected(false);
    switch (x){
        case 1:
            slot1.setSelected(true);
            break;
        case 2:
            slot2.setSelected(true);
            break;
        case 3:
            slot3.setSelected(true);
            break;
        case 4:
            slot4.setSelected(true);
            break;
        case 5:
            slot5.setSelected(true);
            break;
    }
    modeCallBack.OnModeChangedCallBack(x);
}

}

我的代码中有另一个片段是BottomMenuFragment。当我尝试从Bottom菜单片段与此片段进行通信时,它显示空指针异常。不要查看日志中的行号。基本上它显示了这条线          slot3.setText(R.string.next_episode);         带有空指针异常

FATAL EXCEPTION: main                                                                        
Process: com.batmansves.www.nextepisode, PID: 18087

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.batmansves.www.nextepisode/com.batmansves.www.nextepisode.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
                                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
                                                                                at com.batmansves.www.nextepisode.HeaderScrollFragment.onTopicChanged(HeaderScrollFragment.java:138)
                                                                                at com.batmansves.www.nextepisode.MainActivity.TopicChangeCallback(MainActivity.java:69)
                                                                                at com.batmansves.www.nextepisode.BottomMenuFragment.onTopicChanged(BottomMenuFragment.java:99)
                                                                                at com.batmansves.www.nextepisode.BottomMenuFragment.onCreateView(BottomMenuFragment.java:74)
                                                                                at android.app.Fragment.performCreateView(Fragment.java:2220)
                                                                                at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
                                                                                at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
                                                                                at android.app.BackStackRecord.run(BackStackRecord.java:793)
                                                                                at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1537)
                                                                                at android.app.FragmentController.execPendingActions(FragmentController.java:325)
                                                                                at android.app.Activity.performStart(Activity.java:6275)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
                                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                                                at java.lang.reflect.Method.invoke(Native Method)

我认为正在发生的是将slot3作为空指针。但是我已经在onCreateView()中定义了slot3。所以我实际上并不了解问题所在?

0 个答案:

没有答案