【发布时间】:2016-03-26 21:53:37
【问题描述】:
更新
当我使用此代码时 Toast.makeText(Lesson111.this, rb.getText(), Toast.LENGTH_SHORT).show();我收到错误消息说无法解析方法 maketext。然后我看到了这个 (The method makeText in the type Toast is not applicable for the arguments) 我应用了代码 Toast.makeText(Lesson111.this.getActivity(), rb.getText(), Toast.LENGTH_SHORT).show();而不是 Toast.makeText(Lesson111.this, rb.getText(), Toast.LENGTH_SHORT).show(); .错误不知何故消失了。但是,当我尝试运行应用程序并单击第 111 课时。它迫使应用程序关闭。我错过了什么吗?
package com.android.pet.view;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.doepiccoding.navigationdrawer.R;
public class Lesson111 extends Fragment {
private RadioGroup radioGroup;
public View onCreateView(LayoutInflater Inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = Inflater.inflate(R.layout.twopointthree, null);
/* Initialize Radio Group and attach click handler */
radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup);
radioGroup.clearCheck();
/* Attach CheckedChangeListener to radio group */
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) group.findViewById(checkedId);
if(null!=rb && checkedId > -1){
Toast.makeText(Lesson111.this.getActivity(), rb.getText(), Toast.LENGTH_SHORT).show();
}
}
});
return rootView;
}
public void onClear(View v) {
/* Clears all selected radio buttons to default */
radioGroup.clearCheck();
}
public void onSubmit(View v) {
RadioButton rb = (RadioButton) radioGroup.findViewById(radioGroup.getCheckedRadioButtonId());
Toast.makeText(Lesson111.this.getActivity(), rb.getText(), Toast.LENGTH_SHORT).show();
}
}
这是 logcat 上显示的内容,我使用 Bluestacks 作为模拟器
03-27 06:04:19.703 7458-7458/? E/AndroidRuntime:致命异常:主要 进程:com.doepiccoding.navigationdrawer,PID:7458 java.lang.NullPointerException 在 com.android.pet.view.Lesson111.onCreateView(Lesson111.java:22) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:1478) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 在 android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 在 android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) 在 android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) 在 android.os.Handler.handleCallback(Handler.java:733) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:136) 在 android.app.ActivityThread.main(ActivityThread.java:5021) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 在 dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
-
你能发布 logcat 吗?因此,当您尝试显示 Toast 时,我们可以看到抛出了什么异常。我在想 Lesson111.this.getActivity() 可能会返回 null 但没有日志就很难确认
-
@KevinLEGOFF 我已经更新了帖子,另外,我正在使用 bluestacks 作为模拟器