2017-08-26 88 views
0

我爲我的微調控制器定製了微調控制器適配器。這是我的適配器類:android-自定義微調控制器陣列適配器getDropDownView返回null

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

@RonakJoshi是的,我有字體文件 –

+0

@jjones你可以用R.array.NearAxL內容更新你的問題。 – waleedsarwar86

回答

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))); 
相關問題