在尝试访问另一个片段中的微调器时,我在Fragment中遇到了令人讨厌的NullPointerException。 导致NPE的代码:
public class FirstFirst extends Fragment{
Spinner spinner;
RadioGroup rg;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.first_first_fragment, container, false);
spinner=(Spinner)rootView.findViewById(R.id.spinner1);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
rg=(RadioGroup) getView().findViewById(R.id.RadioGroup2);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
spinner.setVisibility(View.GONE);
if(arg1==0){
spinner.setVisibility(View.VISIBLE);
}
});
}
}
我知道,Spinner本身就在那里,我可以看到它,但如果我尝试使用上面的代码改变其可见性,我就会得到NPE。
@Edit:
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/RadioGroup2" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pow" />
</RadioGroup>
@LogCat
05-28 13:54:52.681: E/AndroidRuntime(12841): FATAL EXCEPTION: main
05-28 13:54:52.681: E/AndroidRuntime(12841): Process: com.example.kuwa, PID: 12841
05-28 13:54:52.681: E/AndroidRuntime(12841): java.lang.NullPointerException
05-28 13:54:52.681: E/AndroidRuntime(12841): at com.example.kuwa.FirstFirst$1.onCheckedChanged(FirstFirst.java:26)
05-28 13:54:52.681: E/AndroidRuntime(12841): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
05-28 13:54:52.681: E/AndroidRuntime(12841): at android.widget.RadioGroup.access$600(RadioGroup.java:54)
05-28 13:54:52.681: E/AndroidRuntime(12841): at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)
05-28 13:54:52.681: E/AndroidRuntime(12841): at android.widget.CompoundButton.setChecked(CompoundButton.java:130)
05-28 13:54:52.681: E/AndroidRuntime(12841): at android.widget.CompoundButton.toggle(CompoundButton.java:87)
05-28 13:54:52.681: E/AndroidRuntime(12841): at android.widget.RadioButton.toggle(RadioButton.java:76)
05-28 13:54:52.681: E/AndroidRuntime(12841): at android.widget.CompoundButton.performClick(CompoundButton.java:99)
05-28 13:54:52.681: E/AndroidRuntime(12841): at android.view.View$PerformClick.run(View.java:18462)
05-28 13:54:52.681: E/AndroidRuntime(12841): at android.os.Handler.handleCallback(Handler.java:733)
05-28 13:54:52.681: E/AndroidRuntime(12841): at android.os.Handler.dispatchMessage(Handler.java:95)
05-28 13:54:52.681: E/AndroidRuntime(12841): at android.os.Looper.loop(Looper.java:136)
05-28 13:54:52.681: E/AndroidRuntime(12841): at android.app.ActivityThread.main(ActivityThread.java:5102)
05-28 13:54:52.681: E/AndroidRuntime(12841): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 13:54:52.681: E/AndroidRuntime(12841): at java.lang.reflect.Method.invoke(Method.java:515)
05-28 13:54:52.681: E/AndroidRuntime(12841): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-28 13:54:52.681: E/AndroidRuntime(12841): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-28 13:54:52.681: E/AndroidRuntime(12841): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
使用setVisibility(View.INVISIBLE);如果您打算稍后再次显示它,请改为使用setVisibility(View.GONE)。
答案 1 :(得分:0)
试试这个:
rg=(RadioGroup) view.findViewById(R.id.RadioGroup2)
而不是
rg=(RadioGroup) getView().findViewById(R.id.RadioGroup2)
onViewCreated
的参数view
是 onCreateView
返回的视图
答案 2 :(得分:0)
您发布的xml名称是什么?什么是first_first_fragment布局的代码。如果你没有包含id spinner1的微调器,则spinner为null。