为什么我的ArrayAdapter出现为null?

时间:2014-05-27 13:58:24

标签: android spinner android-arrayadapter

我正在尝试使用从Web中提取的数据来填充微调器,我已将其存储在ArrayList中。麻烦的是,我试图将数据放在ArrayAdapter中,然后使用它来填充微调器。但是我的应用程序一直在崩溃,而logcat告诉我在company.setAdapter(适配器)上有一个空指针异常;我无法弄清楚适配器为何为空。

cList = new ArrayList<Company>();
    getCompanies();
    for(int i = 0; i < cList.size(); i++){
        nameList.add(cList.get(i).getNameAndCountry());
    }

    orChoice = (RadioGroup) findViewById(R.id.radioGroup1);
    company = (Spinner) findViewById(R.id.spinner1);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_add_new, nameList);
    //ArrayAdapter.createFromResource(this,R.array.product_types, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    company.setAdapter(adapter);

这里是活动布局xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AddNew" >

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="25dp"
    android:layout_marginTop="25dp" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/offer" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/request" />
</RadioGroup>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup1"
    android:layout_below="@+id/radioGroup1"
    android:layout_marginTop="25dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/company" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="5dp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup1"
    android:layout_below="@+id/spinner1"
    android:layout_marginTop="25dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/model" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup1"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="5dp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup1"
    android:layout_below="@+id/spinner2"
    android:layout_marginTop="25dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/quantity2" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup1"
    android:layout_centerVertical="true"
    android:ems="10"
    android:inputType="number"
    android:layout_marginTop="5dp"
    android:layout_below="@+id/textView3" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="25dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/currency2" />

<Spinner
    android:id="@+id/spinner3"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup1"
    android:layout_below="@+id/textView4"
    android:layout_marginTop="5dp" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="25dp"
    android:layout_marginLeft="175dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/price" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView5"
    android:layout_alignLeft="@+id/textView5"
    android:layout_marginTop="5dp"
    android:ems="10"
    android:inputType="number" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup1"
    android:layout_below="@+id/spinner3"
    android:layout_marginTop="25dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/delivery_date" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup1"
    android:layout_below="@+id/textView6"
    android:layout_marginTop="5dp"
    android:ems="10"
    android:inputType="date" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup1"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="25dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/specs" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignLeft="@+id/textView7"
    android:layout_below="@+id/textView7"
    android:layout_marginTop="5dp"
    android:ems="10"
    android:inputType="text" />

 <EditText
    android:id="@+id/editText5"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignLeft="@+id/textView8"
    android:layout_below="@+id/textView8"
    android:layout_marginTop="5dp"
    android:ems="10"
    android:inputType="text" />

 <TextView
     android:id="@+id/textView8"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_above="@+id/editText4"
     android:layout_toRightOf="@+id/editText4"
     android:text="@string/comments"
     android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
     android:id="@+id/button1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentRight="true"
     android:text="@string/submit" />

这里是Logcat:

05-27 17:07:16.968: E/AndroidRuntime(5586): FATAL EXCEPTION: main
05-27 17:07:16.968: E/AndroidRuntime(5586): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.onegdd.orbit/com.onegdd.orbit.AddNew}: java.lang.NullPointerException
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.os.Looper.loop(Looper.java:137)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.app.ActivityThread.main(ActivityThread.java:4895)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at java.lang.reflect.Method.invokeNative(Native Method)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at java.lang.reflect.Method.invoke(Method.java:511)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at dalvik.system.NativeStart.main(Native Method)
05-27 17:07:16.968: E/AndroidRuntime(5586): Caused by: java.lang.NullPointerException
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.widget.AbsSpinner.setAdapter(AbsSpinner.java:114)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.widget.Spinner.setAdapter(Spinner.java:380)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at com.onegdd.orbit.AddNew.onCreate(AddNew.java:68)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.app.Activity.performCreate(Activity.java:5163)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-27 17:07:16.968: E/AndroidRuntime(5586):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
05-27 17:07:16.968: E/AndroidRuntime(5586):     ... 11 more

3 个答案:

答案 0 :(得分:0)

用下面的行替换你的Arraylist

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, nameList);

答案 1 :(得分:0)

我多次遇到这个问题,这是由于Json数据(在我的情况下)对某些值为null。尝试设置你的arraylist代码:

 nameList.add(cList.get(i).getNameAndCountry()+"");

我在你的代码中只添加了“”,所以如果getNameAndCountry()返回任何空值,它将被转换为空字符串。如果你正在检查,这非常有用。

答案 2 :(得分:0)

请确保以下代码在适配器设置之前。

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

您的代码