gpt4 book ai didi

crash - 使用ArrayAdapter的微调器,使应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 17:29:22 25 4
gpt4 key购买 nike

我正在尝试使用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

最佳答案

您的微调器布局应仅为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);

关于crash - 使用ArrayAdapter的微调器,使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24862411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com