0

I got a nasty NullPointerException in Fragment while trying to access spinner in another Fragment. The code, which causes 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);
            }
        });
    }
}

I know, that the Spinner itself is there, I can see it, but if I try to change its visibility with the code above i get the NPE.

@Edit:

The XML code:

<?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)
user3212350
  • 401
  • 1
  • 6
  • 18

3 Answers3

0

Use setVisibility(View.INVISIBLE); instead of setVisibility(View.GONE) if you intend to make it visible again later.

Woodstown
  • 26
  • 5
0

Try this:

rg=(RadioGroup) view.findViewById(R.id.RadioGroup2)

instead of this

rg=(RadioGroup) getView().findViewById(R.id.RadioGroup2)

The parameter view of onViewCreated is The View returned by onCreateView

Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
0

What is name of xml you posted? What is code for first_first_fragment layout. If you don't have spinner with id spinner1 in it, then spinner is null.

Zoran
  • 1,484
  • 1
  • 10
  • 13