您好我有一个片段回调接口方法,在Android 7.1.0上运行良好,但它会在Android 5.0.0中抛出此null异常。我该如何解决?我很感激帮助。
代码
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId)
{
case R.id.radio0:
answer = radio0.getText().toString();
break;
case R.id.radio1:
answer = radio1.getText().toString();
break;
case R.id.radio2:
answer = radio2.getText().toString();
break;
}
}
});
@OnClick(R.id.button_submit)
public void submitAnswer()
{
if(radioGroup.getCheckedRadioButtonId() != -1)
{
datapass(true, answer.toString());
}
radioGroup.clearCheck();
}
public void datapass(Boolean hasAnswer, String answer)
{
fragmentCallBackInterface.passAnswer(hasAnswer, answer);
}
FragmentCallBackInterface
public interface FragmentCallBackInterface
{
public void passAnswer(boolean answerBool, String answerString);
}
实施界面的活动
public class QuizActivity extends BaseActivity implements
FragmentCallBackInterface
{
boolean answerBool;
String answerString;
@Override
public void passAnswer(boolean answerBool, String answerString)
{
this.answerBool = answerBool;
this.answerString = answerString;
}
}
答案 0 :(得分:1)
成员变量fragmentCallBackInterface是否已初始化,在哪里? 您可以在onAttach中初始化它。
注意在android 6.0或API 23之前它是onAttach(Activity)。然而,之后它被改为onAttach(Context)。
您仍然应该使用onAttach(活动),否则它当然无法在Android 5上运行。