Spinner使用ArrayAdapter,使应用程序崩溃

时间:2014-07-21 10:02:07

标签: crash spinner android-arrayadapter

我正在尝试使用ArrayAdapter创建一个微调器,但是应用程序崩溃了,我没有在其他帖子上找到解决方案,所以我希望你能帮助我:)。

我的字符串数组在res / values / strings.xml

 <string-array name="functionF">
    <item>Lucas</item>
    <item>Paul</item>
</string-array>

res / layout / spinner_layout中的spinner_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Spinner
android:id="@+id/function_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" 
/>

</LinearLayout>

我的代码

        private Spinner spinnerFunction;
        spinnerFunction = (Spinner) findViewById(R.id.function_spinner);
             ArrayAdapter<CharSequence> adapter1 =          ArrayAdapter.createFromResource(this,R.array.functionF, R.layout.spinner_layout);  
             adapter1.setDropDownViewResource(R.layout.spinner_layout);
             spinnerFunction.setAdapter(adapter1);

这是崩溃错误:

07-21 05:58:55.296: E/AndroidRuntime(2544): FATAL EXCEPTION: main
07-21 05:58:55.296: E/AndroidRuntime(2544): java.lang.RuntimeException: Unable to start      activity ComponentInfo{com.ben.wizzer/com.ben.wizzer.Friends}:  java.lang.NullPointerException
07-21 05:58:55.296: E/AndroidRuntime(2544):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at android.os.Looper.loop(Looper.java:137)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at android.app.ActivityThread.main(ActivityThread.java:5103)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at java.lang.reflect.Method.invoke(Method.java:525)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at dalvik.system.NativeStart.main(Native Method)
07-21 05:58:55.296: E/AndroidRuntime(2544): Caused by: java.lang.NullPointerException
07-21 05:58:55.296: E/AndroidRuntime(2544):     at android.widget.ArrayAdapter.createFromResource(ArrayAdapter.java:430)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at com.ben.wizzer.Friends.onCreate(Friends.java:174)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at android.app.Activity.performCreate(Activity.java:5133)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-21 05:58:55.296: E/AndroidRuntime(2544):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-21 05:58:55.296: E/AndroidRuntime(2544):     ... 11 more

1 个答案:

答案 0 :(得分:0)

你的微调器布局应该只是这样的TextView:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp" 
    android:padding="10dp"/>

TextView布局它是字符串数组但你可以使用默认的android layou:

String [] strArr = getResources().getStringArray(R.array.functionF);
ArrayAdapter<String> adapter1 = new ArrayAdapter<Categories>(getActivity(),android.R.layout.simple_list_item_1,strArr);
spinnerFunction.setAdapter(categoriesAdapter);

如果您在片段中,请使用getActivity().getResources().getStringArray(R.array.functionF);