Action Bar中的微调器返回Null Pointer Exception

时间:2013-07-11 01:48:23

标签: android null spinner

我正在尝试在操作栏中实现微调器,以在活动中的两个不同片段之间切换。为了实现它,我在我的布局中使用XML添加了微调器,然后尝试在基本活动类中设置其适配器。这是我的活动课,

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Log.i(debug,"OnCreate");

setContentView(R.layout.add_new_course);

Intent intent = getIntent();
semesterID = intent.getIntExtra("Semester ID", 0);

    Log.i(TAG,"Add New Course created");

    spinner = (Spinner)findViewById(R.id.courseSpinner);
    FragmentManager fragMan = getFragmentManager();


    if(savedInstanceState == null)
    {
        addDetailsFrag = new addCourseDetailsFragment();
        addWeightsFrag = new addCourseWeightsFragment();

        fragMan.beginTransaction().add(R.id.courseFragmentContainer, addDetailsFrag);

    }


}

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    Log.i(debug,"onCreateOptionsMenu");

    getMenuInflater().inflate(R.menu.courses_menu, menu);


    spinner = (Spinner)findViewById(R.id.courseSpinner);


    if(spinner == null)
        Log.i(debug,"spinner == null");
              //this always prints, which is causing a problem.   


    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.courseSpinner,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> adapter, View view, int position, long id)
        {

            //dostuff               

        }

        public void onNothingSelected(AdapterView<?> adapter)
        {

                            //domorestuff
        }


    });

    Log.i(TAG, "Menu Inflated");
    return true;
}

这是我的logcat,虽然它非常简单,它暗示了一个空指针:

 07-11 01:29:15.703: I/Debug Problem(6036): spinner == null
07-11 01:29:15.723: D/AndroidRuntime(6036): Shutting down VM
07-11 01:29:15.723: W/dalvikvm(6036): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-11 01:29:15.793: E/AndroidRuntime(6036): FATAL EXCEPTION: main
07-11 01:29:15.793: E/AndroidRuntime(6036): java.lang.NullPointerException
07-11 01:29:15.793: E/AndroidRuntime(6036):     at com.photoniccannon.passbook.AddNewCourse.onCreateOptionsMenu(AddNewCourse.java:77)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at android.app.Activity.onCreatePanelMenu(Activity.java:2490)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:407)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:769)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:201)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at android.view.Choreographer.doFrame(Choreographer.java:531)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at android.os.Handler.handleCallback(Handler.java:725)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at android.os.Looper.loop(Looper.java:137)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at java.lang.reflect.Method.invoke(Method.java:511)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at com.android.internal.os.ZygoteInit.main        (ZygoteInit.java:560)
07-11 01:29:15.793: E/AndroidRuntime(6036):     at dalvik.system.NativeStart.main(Native Method)
07-11 01:34:15.983: I/Process(6036): Sending signal. PID: 6036 SIG: 9
07-11 01:34:17.253: E/Trace(6137): error opening trace file: No such file or directory (2)

编辑:courses_menu.xml

<?xml version="1.0" encoding="utf-8"?>

<Spinner
    android:id="@+id/courseSpinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

我不知道为什么它是空的。我是否必须将微调器实例化为微调器类?

感谢。

4 个答案:

答案 0 :(得分:1)

为什么你在onCreateOptionsMenu方法中获得微调器?您已经从onCreate方法的xml中获取了微调器。您可以尝试在onCreateOptionsMenu方法中删除此行:

spinner = (Spinner)findViewById(R.id.courseSpinner);

答案 1 :(得分:1)

如果courseSpinner资源位于courses_menu xml内,请尝试:

MenuItem item = menu.findItem(R.id.courseSpinner);
spinner = (Spinner)item.getActionView();

这是因为您必须从菜单中获取微调器资源。

答案 2 :(得分:0)

我试试这个 Spinner spinner =(Spinner)new Spinner(getActivity()); 在ListFragment中效果很好。

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(getActivity(), R.array.showmessages,R.layout.spinner_item);
        list.setDropDownViewResource(R.layout.spinner_dropdown_item);
        inflater.inflate(R.menu.message, menu);
        MenuItem item = (MenuItem) menu.findItem(R.id.menu_mode);
        Spinner spinner = (Spinner) new Spinner(getActivity());
        spinner.setId(R.id.spinner1);
        item.setActionView(spinner);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);



message.xml 

<item android:id="@+id/menu_mode"
    android:showAsAction="always"
    android:title="@string/action_bar_spinner"
    android:actionViewClass="android.widget.spinner" />

答案 3 :(得分:0)

如果有人遇到与我相同的微小愚蠢错误:

我不得不改变

android:actionViewClass="android.widget.spinner"

app:actionViewClass="android.widget.spinner"

不知道为什么,如果有人知道,请启发我;)