why TaskFragment.task is not yet initialized when called from within OnItemSelectedListener?

时间:2015-11-12 11:48:36

标签: android android-fragments spinner

I am trying to populate a spinner in a fragment based on the item selected in a spinner in another fragment. Below are my both fragments:

BaseDataFragment.java

public class BaseDataFragment extends Fragment {

View view;
Spinner taskSpinner, regionSpinner;
private GoogleMap googleMap;


public BaseDataFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    view = inflater.inflate(R.layout.fragment_base_data, container, false);
    init();
    return view;
}

public void init(){

    taskSpinner = (Spinner)view.findViewById(R.id.taskTypeSpinner);
    ArrayAdapter<CharSequence> taskAdapter = ArrayAdapter.createFromResource(this.getActivity(), R.array.mtaskTypeArray, android.R.layout.simple_spinner_item);
    taskAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    taskSpinner.setAdapter(taskAdapter);
    taskSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            parent.getContext();
            if (parent.getSelectedItem().toString().equals("AC Maintenance")) {

                TaskFragment.task.setText(parent.getItemAtPosition(position).toString());

                ArrayAdapter<CharSequence> taskAdapter = ArrayAdapter.createFromResource(TaskFragment.view.getContext(), R.array.ACMaintenanceArray, android.R.layout.simple_spinner_item);
                taskAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                TaskFragment.maintenanceTypeSpinner.setAdapter(taskAdapter);

            }

        }

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

        }
    });

    regionSpinner = (Spinner)view.findViewById(R.id.regionSpinner);
    ArrayAdapter<CharSequence> regionAdapter = ArrayAdapter.createFromResource(this.getActivity(), R.array.regionArray, android.R.layout.simple_spinner_item);
    regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    regionSpinner.setAdapter(regionAdapter);


}


}

I created a class for the listener and I kept getting the null value when I debug. So I decided to copy and paste it in the BaseData Fragment to see the result and I still get null.

TaskFragment.java

public class TaskFragment extends Fragment {

public static View view;
public static Spinner maintenanceTypeSpinner;
public static TextView task;


public TaskFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_task, container, false);
    init();
    return view;
}

private void init() {
    task = (TextView)view.findViewById(R.id.taskTextView);
    maintenanceTypeSpinner = (Spinner) view.findViewById(R.id.maintenanceSpinner);
}


}

These two lines return null pointer exception. Can you please help me out?

TaskFragment.task.setText(parent.getItemAtPosition(position).toString());

ArrayAdapter<CharSequence> taskAdapter = ArrayAdapter.createFromResource(TaskFragment.view.getContext(), R.array.ACMaintenanceArray, android.R.layout.simple_spinner_item);

Full Error log below:

11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance        E/AndroidRuntime: FATAL EXCEPTION: main
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime: Process: com.application.sweetiean.stlmaintenance, PID: 23697
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime: java.lang.NullPointerException
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at com.application.sweetiean.stlmaintenance.BaseDataFragment$1.onItemSelected(BaseDataFragment.java:58)
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at android.widget.AdapterView.fireOnSelected(AdapterView.java:964)
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at android.widget.AdapterView.access$200(AdapterView.java:49)
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:928)
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:146)
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5602)
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
11-12 11:36:31.361 23697-23697/com.application.sweetiean.stlmaintenance E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

NOTE: This question has been flagged as a duplicate. I would like to state clearly that I know exactly what a null pointer exception is. I know I may be referencing something that is non-existent and that is why I am getting this bug. In the time that I have been programming, I realise that sometimes you need to seek a second eye to help identify the fault in your code and that is why I am requesting help here.


I just tried something else. By default, AC Maintenance is the first item on the list and so it is already selected. For this reason the app crashed on start-up. I decided to change up my code to populate the task fragment spinner when UPS Maintenance is selected and the app starts correctly and does not crash.

if (parent.getSelectedItem().toString().equals("UPS Maintenance")) {

                TaskFragment.task.setText(parent.getItemAtPosition(position).toString());

                ArrayAdapter<CharSequence> taskAdapter = ArrayAdapter.createFromResource(TaskFragment.view.getContext(), R.array.UPSMaintenanceArray, android.R.layout.simple_spinner_item);
                taskAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                TaskFragment.maintenanceTypeSpinner.setAdapter(taskAdapter);

            }

Any ideas on how I can work with the default selection(AC Maintenance) without crashing my app?


So I have decided to improvise. Since I cant seem to answer my own question, I am adding my improvised solution as an edit. The app is crashing because there is a default selected item. For this reason, in my string array I have decided to add <item>None</item>so that NONE becomes the first item and I can peacefully run my app without anymore null pointers.

0 个答案:

没有答案