我有一個可點擊的TextView和一個微調器,其可見性設置爲GONE
。使用微調控制器時會引發神祕的NullPointerException
這是我的佈局片段:
<TextView
android:id="@+id/strip1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Order Name ID Description Type"
android:textSize="15dip"
android:textStyle="bold"
android:clickable="true"
android:gravity="left"
android:textColor="#ffffff"
android:layout_weight="1" />
<Spinner android:id="@+id/sortSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:visibility="gone"
android:clickable="true"
android:prompt="@string/prompt" />
在onCreate
我有這樣的:
TextView strip = (TextView)findViewById(R.id.strip1);
strip.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
spinner_show();
}
});
spinner_show()
看起來是這樣的:
private void spinner_show() {
String str = new String[2];
str[0] = "Ascending";
str[1] = "Descending";
Spinner sortSpin = (Spinner)findViewById(R.id.sortSpinner);
ArrayAdapter<String> sizeArrayAdapter =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, str);
sizeArrayAdapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
sortSpin.setAdapter(sizeArrayAdapter);
sortSpin.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View v, int pos, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
該應用程序是扔NullPointerException
和我無法弄清楚爲什麼。
這裏是logcat的片段:
12-14 14:57:31.388: W/dalvikvm(3794): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-14 14:57:31.468: E/AndroidRuntime(3794): FATAL EXCEPTION: main
12-14 14:57:31.468: E/AndroidRuntime(3794): java.lang.NullPointerException
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:332)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:328)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.AbsListView.obtainView(AbsListView.java:1409)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.ListView.onMeasure(ListView.java:1127)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.view.View.measure(View.java:8313)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.view.View.measure(View.java:8313)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
12-14 14:57:31.468: E/AndroidRuntime(3794): at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.view.View.measure(View.java:8313)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.view.View.measure(View.java:8313)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.view.View.measure(View.java:8313)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.os.Looper.loop(Looper.java:123)
12-14 14:57:31.468: E/AndroidRuntime(3794): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-14 14:57:31.468: E/AndroidRuntime(3794): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 14:57:31.468: E/AndroidRuntime(3794): at java.lang.reflect.Method.invoke(Method.java:507)
12-14 14:57:31.468: E/AndroidRuntime(3794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-14 14:57:31.468: E/AndroidRuntime(3794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-14 14:57:31.468: E/AndroidRuntime(3794): at dalvik.system.NativeStart.main(Native Method)
我缺少什麼?
請張貼錯誤日誌... – user370305 2011-12-14 09:09:24
運行在命令行中輸入「adb logcat AndroidRuntime:* *:S」,然後運行該應用程序。這將只顯示異常信息,然後在此處發佈。我認爲不應該發生的碰撞時,我做的一件事就是開始評論代碼,直到崩潰消失。至少你有和想法在哪裏看。從你的文章中,我們不知道它是在onCreate還是在你的spinner_show函數中崩潰。 – 2011-12-14 09:27:59
檢查堆棧跟蹤並查看引發異常的行號。 – 2011-12-14 09:31:12