thousight thousight - 1 month ago 25
Android Question

setOnItemSelectedListener() for spinner in Fragment

I'm trying to add a spinner and a listener in my fragment. Android studio did not report any error to me, but when I run it, it crashes with this FATAL EXCEPTION:

14267-14267/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: space.markwen.www.units, PID: 14267
java.lang.RuntimeException: Unable to start activity ComponentInfo{space.markwen.www.units/space.markwen.www.units.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2456)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539)
at android.app.ActivityThread.access$900(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5507)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at space.markwen.www.units.LengthActivity.onCreate(LengthActivity.java:40)
at android.app.Fragment.performCreate(Fragment.java:2198)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6320)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539) 
at android.app.ActivityThread.access$900(ActivityThread.java:159) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:152) 
at android.app.ActivityThread.main(ActivityThread.java:5507) 
at java.lang.reflect.Method.invoke(Native Method) 


Fragment class:

public class LengthActivity extends Fragment {

View lengthView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Change FrameLayout to content_length
lengthView = inflater.inflate(R.layout.content_length, container, false);
// Change the title on titlebar to Length
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Length");
return lengthView;
}

Spinner spinner;
String[] lengthUnits = {
"Kilometer", "Meter", "Centimeter", "Millimeter", "Micrometer", "Nanometer", "Mile", "Yard", "Foot", "Inch", "Nautical Mile"};

@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
spinner = (Spinner) getView().findViewById(R.id.lengthSpinner);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(), android.R.layout.simple_spinner_item, lengthUnits);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}

}


And my view just in case you can find something out of it:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:weightSum="1">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="30dp"
android:layout_weight="1.01" />

<Spinner
android:layout_width="112dp"
android:layout_height="57dp"
android:id="@+id/lengthSpinner" />
<!--android:layout_below="@+id/editText"-->
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp" />
</LinearLayout>


Thanks

Answer

Move all of your onCreate() code — starting with the line that is crashing — into onViewCreated(). You cannot execute any of that until onCreateView() has been called, and onCreate() will be called before onCreateView(). onViewCreated() is called after onCreateView().