使用 String Array:NullPointer 异常中的 Spinner

Using Spinner from String Array:NullPointer Exception

我是Android的新手,了解不多。请帮我解决这个问题

I have a spinner which goes to another class when an option is selected and then clicked on a button.我在 strings.xmlfile.

中给出了字符串数组
<string-array name="month_arrays">
        <item>2015 June</item>
        <item>2015 July</item>
        <item>2015 August</item>
        <item>2015 September</item>
        <item>2015 October</item>
        <item>2015 November</item>
        <item>2015 December</item>
        <item>2016 January</item>
        <item>2016 February</item>
        <item>2016 March</item>
    </string-array>

我在片段 class 中使用了它,在 ArrayList 中使用了 getStringArrays(),然后将其存储在 ArrayAdapter 中。

public class CalendarFragment extends Fragment {

public CalendarFragment(){}
TextView t1;
TextView t2;
Spinner spinner1;
Button btnSubmit;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);
     t1=(TextView)rootView.findViewById(R.id.textView1);
     t2=(TextView)rootView.findViewById(R.id.textView2);
     Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "font.otf");
     t1.setTypeface(font); 
     t2.setTypeface(font);


     String[] regionsArray = getResources().getStringArray(R.array.month_arrays);
     ArrayList<String> list = new ArrayList<String>(Arrays.asList(regionsArray));
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter);

        spinner1 = (Spinner)rootView.findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());


        btnSubmit = (Button)rootView.findViewById(R.id.btnSubmit);

        btnSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getActivity(),
                        "OnClickListener : " + 
                        "\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()), // This is Testing if it gets Selected
                        Toast.LENGTH_SHORT).show();
            }

        });

    return rootView;

}

}

但是当我进入这个菜单并且应用程序强制关闭时我得到了 NullPointer 异常。

LogCat

08-09 09:14:30.857: E/AndroidRuntime(1728): FATAL EXCEPTION: main 08-09 09:14:30.857: E/AndroidRuntime(1728): Process: com.naysotech.smartrajagiri, PID: 1728 08-09 09:14:30.857: E/AndroidRuntime(1728): java.lang.NullPointerException 08-09 09:14:30.857: E/AndroidRuntime(1728): at com.naysotech.smartrajagiri.CalendarFragment.onCreateView(CalendarFragment.java:58) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.Fragment.performCreateView(Fragment.java:1700) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.BackStackRecord.run(BackStackRecord.java:684) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.FragmentManagerImpl.run(FragmentManager.java:443) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.os.Handler.handleCallback(Handler.java:733) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.os.Handler.dispatchMessage(Handler.java:95) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.os.Looper.loop(Looper.java:136) 08-09 09:14:30.857: E/AndroidRuntime(1728): at android.app.ActivityThread.main(ActivityThread.java:5017) 08-09 09:14:30.857: E/AndroidRuntime(1728): at java.lang.reflect.Method.invokeNative(Native Method) 08-09 09:14:30.857: E/AndroidRuntime(1728): at java.lang.reflect.Method.invoke(Method.java:515) 08-09 09:14:30.857: E/AndroidRuntime(1728): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 08-09 09:14:30.857: E/AndroidRuntime(1728): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 08-09 09:14:30.857: E/AndroidRuntime(1728): at dalvik.system.NativeStart.main(Native Method)

您应该启动微调器:

spinner1 = (Spinner)rootView.findViewById(R.id.spinner1);

在尝试为其设置适配器之前:

spinner1.setAdapter(dataAdapter);