我为我的微调器设置了一个自定义微调器适配器。这是我的适配器类:
public class MySpinnerAdapter extends ArrayAdapter<String> {
// Initialise custom font, for example:
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "IRAN Sans Bold.ttf");
public MySpinnerAdapter(Context context, int resource, String[] items) {
super(context, resource, items);
}
// Affects default (closed) state of the spinner
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTypeface(font);
return view;
}
// Affects opened state of the spinner
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getDropDownView(position, convertView, parent);
view.setTypeface(font);
return view;
}
}
这是simple_spinner布局:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textColor="#000"
android:textSize="16dip" />
问题是,它使得微调器和我可以在微调器中看到第一个数组项,但是当我想打开下拉微调器时,它返回一个空错误:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:415)
at ir.babaarzooni.persiandesigners.MySpinnerAdapter.getDropDownView(MySpinnerAdapter.java:33)
at android.support.v7.widget.AppCompatSpinner$DropDownAdapter.getDropDownView(AppCompatSpinner.java:637)
at android.support.v7.widget.AppCompatSpinner$DropDownAdapter.getView(AppCompatSpinner.java:633)
at android.support.v7.widget.AppCompatSpinner.compatMeasureContentWidth(AppCompatSpinner.java:560)
at android.support.v7.widget.AppCompatSpinner.access$500(AppCompatSpinner.java:64)
at android.support.v7.widget.AppCompatSpinner$DropdownPopup.computeContentWidth(AppCompatSpinner.java:751)
at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:776)
at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:434)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
这是我的阵列:
<array name="NearAxL">
<item>انتخاب AX</item>
<item>0</item>
<item>0.25</item>
<item>0.5</item>
<item>0.75</item>
<item>1</item>
<item>1.25</item>
<item>1.5</item>
<item>1.75</item>
<item>2</item>
<item>2.25</item>
<item>2.5</item>
<item>2.75</item>
<item>3</item>
</array>
这是java代码:
NearAxR.setAdapter(new MySpinnerAdapter (this,R.layout.simple_spinner,
getResources().getStringArray(R.array.NearAxL)));
这段代码有什么问题?
答案 0 :(得分:0)
此:
<array name="NearAxL">
<item>انتخاب AX</item>
<item>0</item>
<item>0.25</item>
<item>0.5</item>
<item>0.75</item>
<item>1</item>
<item>1.25</item>
<item>1.5</item>
<item>1.75</item>
<item>2</item>
<item>2.25</item>
<item>2.5</item>
<item>2.75</item>
<item>3</item>
</array>
应该是:
<string-array name="NearAxL">
<item>انتخاب AX</item>
<item>0</item>
<item>0.25</item>
<item>0.5</item>
<item>0.75</item>
<item>1</item>
<item>1.25</item>
<item>1.5</item>
<item>1.75</item>
<item>2</item>
<item>2.25</item>
<item>2.5</item>
<item>2.75</item>
<item>3</item>
</string-array>
array
元素适用于Typed Arrays。字符串数组必须使用string-array
。
如果仍然无效,请尝试使用内置微调器布局,如下所示
NearAxR.setAdapter(new MySpinnerAdapter (this,android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.NearAxL)));