我有一个可点击的TextView和一个Spinner,其可见性设置为GONE
。
这是我的布局代码段:
<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)
我错过了什么?
答案 0 :(得分:0)
因为您已经将旋转器可见性视为已经消失,所以它如何能够显示您的微调器。
所以请致电sortSpin.SetVisibility(View.VISIBLE);
答案 1 :(得分:0)
好的,首先,您的xml布局文件
<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"/>
<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"/>
在你的xml文件中你的TextView有android:layout_weight="1"
所以它覆盖了所有的布局,没有空间用于Spinner(这也不会导致NullPointerException因此它没有必要),
现在来自你的java类文件,
private void spinner_show()
{
String[] str=new String[2]; // problem on declaring String array
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) {
}
});
sortSpin.setVisibility(View.VISIBLE); // also you don't Visiable your spinner
}
主要的是它在我的eclipse中工作正常,我刚从xml文件中删除了android:prompt="@string/prompt"
属性,因为我没有字符串提示符..