0

我正在尝试在我的操作栏中实现一个微调器,以在活动中的两个不同片段之间切换。为了实现它,我在布局中使用 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

4 回答 4

1

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

spinner = (Spinner)findViewById(R.id.courseSpinner);
于 2013-07-11T02:03:46.843 回答
1

如果 courseSpinner 资源位于 courses_menu xml 中,请尝试以下操作:

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

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

于 2013-07-11T02:44:38.230 回答
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" />

于 2014-01-27T15:23:44.357 回答
0

如果有人遇到和我一样的小错误:

我不得不改变

android:actionViewClass="android.widget.spinner"

app:actionViewClass="android.widget.spinner"

不知道为什么,如果有人知道,请赐教;)

于 2016-12-02T16:17:17.763 回答