我正在尝试在我的操作栏中实现一个微调器,以在活动中的两个不同片段之间切换。为了实现它,我在布局中使用 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" />
我不知道为什么它是空的。我是否必须将微调器实例化为微调器类?
谢谢。