Android应用程序在单击单选按钮时崩溃,抛出NullPointerException

时间:2017-03-04 15:33:28

标签: java android android-fragments nullpointerexception

好的,所以我是Android开发新手,几天后我一直在自学。我正在关注youtube上的一些教程,但我坚持其中一个,即这一个:

片段活动沟通:https://www.youtube.com/watch?v=2c0Iog5rfxo&t=820s

本教程是关于从片段上的单选按钮选择更改主要活动的背景颜色。 我完成了他在教程中所做的工作,但我不得不使用onAttach方法,因为他在视频中使用了弃用的方式,即onAttach(活动活动)。所以我用onAttach(上下文上下文)(见下面的代码)

但是当我选择单选按钮时,我收到错误,应用程序崩溃并且错误日志显示:

致命的例外:主要                   处理:fragmentapplication2,PID:4318                   java.lang。 NullPointerException 尝试调用接口方法' void com.example.name.fragmentapplication2.ColorFragment $ OnColorChangeListener.colorChanged(java.lang.String) ' 对空对象引用                       在com.example.name.fragmentapplication2.ColorFragment $ 1.onCheckedChanged(ColorFragment.java:36)

由此我知道我在Fragment类中创建的接口实例为null,这导致了这个异常。 因为我正在使用它的实例在RadioGroup的OnCheckedChangeListener中调用它的函数。下面是片段类:

package com.example.name.fragmentapplication2;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

public class ColorFragment extends Fragment{
RadioGroup colorGroup;
OnColorChangeListener onColorChangeListener;//interface instance initiated

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view;
    view=inflater.inflate(R.layout.color_frag_layout,container,false);
    colorGroup=(RadioGroup)view.findViewById(R.id.color_radio_group);
    colorGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
        @Override
        public  void onCheckedChanged(RadioGroup group, int CheckedId){
            switch (CheckedId)
            {
                case R.id.Red:
                    onColorChangeListener.colorChanged("#CF000F");
                    break;
                case R.id.black:
                    onColorChangeListener.colorChanged("#000000");
                    break;
                case R.id.gray:
                    onColorChangeListener.colorChanged("#6C7A89");
                    break;
            }
        }
    });
    return view;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    Activity activity;
    if(context instanceof Activity)
    {
        activity = (Activity)context;
        try{
            onColorChangeListener = (OnColorChangeListener)activity;
        }catch (ClassCastException e){
            throw new ClassCastException(activity.toString()
                    + " must implement OnColorChangeListener");

        }
    }
}


public interface OnColorChangeListener
{
    public void colorChanged(String ColorName);
}

}

logcat的:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.name.fragmentapplication2, PID: 4318
              java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.name.fragmentapplication2.ColorFragment$OnColorChangeListener.colorChanged(java.lang.String)' on a null object reference
                  at com.example.name.fragmentapplication2.ColorFragment$1.onCheckedChanged(ColorFragment.java:36)
                  at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
                  at android.widget.RadioGroup.access$600(RadioGroup.java:54)
                  at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)
                  at android.widget.CompoundButton.setChecked(CompoundButton.java:157)
                  at android.widget.CompoundButton.toggle(CompoundButton.java:113)
                  at android.widget.RadioButton.toggle(RadioButton.java:78)
                  at android.widget.CompoundButton.performClick(CompoundButton.java:118)
                  at android.view.View$PerformClick.run(View.java:19866)
                  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:5254)
                  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:903)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

那么我该如何解决这个问题呢?如何启动接口实例以使其不包含null?

1 个答案:

答案 0 :(得分:0)

删除额外的条件。

Activity activity;
if(context instanceof Activity) 
{
    activity = (Activity)context;

只需将Context转发给侦听器即可。您不应该首先转换为Activity。

context instanceof Activity为false时,您将获得空侦听器。

如果您真的想要一个Activity,那么如果您使用了Support Fragments类

,则应该onAttach(Activity activity)