问题
UPDATE
When I use this code Toast.makeText(Lesson111.this, rb.getText(), Toast.LENGTH_SHORT).show(); Im recieving the error message saying cannot resolve method maketext. Then I saw this (The method makeText in the type Toast is not applicable for the arguments) I applied the code Toast.makeText(Lesson111.this.getActivity(), rb.getText(), Toast.LENGTH_SHORT).show(); instead of Toast.makeText(Lesson111.this, rb.getText(), Toast.LENGTH_SHORT).show(); . the error somehow went away. However, when I try to run the application and click the Lesson111. Its forcing the application to close. Am I missing something?
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();
}
}
Here what shows on the logcat, Im using Bluestacks as an Emulator
03-27 06:04:19.703 7458-7458/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.doepiccoding.navigationdrawer, PID: 7458 java.lang.NullPointerException at com.android.pet.view.Lesson111.onCreateView(Lesson111.java:22) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5021) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) at dalvik.system.NativeStart.main(Native Method)
回答1:
It seems the error come from line 22
radioGroup.clearCheck();
Can you check that the layout twopointthree has a RadioGroup with the id radioGroup
来源:https://stackoverflow.com/questions/36241327/cannot-resolve-method-maketext