更新
当我使用此代码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(); 。错误莫名其妙地消失了。但是,当我尝试运行该应用程序并单击Lesson111时。它迫使应用程序关闭。我错过了什么吗?
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) at 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(本地方法)
答案 0 :(得分:1)
似乎错误来自第22行
radioGroup.clearCheck();
你能检查一下twopointthree的布局是否有一个带有id的RadioGroup radioGroup中