下拉微调器只加载List中的第一项,然后在按下时返回NullPointerException

时间:2016-10-31 23:54:44

标签: android android-layout nullpointerexception android-spinner

这是我第一次在我的Android应用程序中构建一个微调器,我搜索了不同的网站,为同一目的发布了不同的教程,我发现我正在实现我的微调器,就像他们一样。

我的应用程序成功运行,按下手机屏幕上第一个出现的项目后返回空指针异常,

这是我在logcat上的例外

11-01 04:15:40.669 23134-23134/com.example.prof_mohamedatef.listview E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
   at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
   at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:416)
   at android.support.v7.widget.AppCompatSpinner$DropDownAdapter.getDropDownView(AppCompatSpinner.java:636)
   at android.support.v7.widget.AppCompatSpinner$DropDownAdapter.getView(AppCompatSpinner.java:631)
   at android.support.v7.widget.AppCompatSpinner.compatMeasureContentWidth(AppCompatSpinner.java:558)
   at android.support.v7.widget.AppCompatSpinner.access$500(AppCompatSpinner.java:65)
   at android.support.v7.widget.AppCompatSpinner$DropdownPopup.computeContentWidth(AppCompatSpinner.java:749)
   at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:774)
   at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:432)
   at android.view.View$PerformClick.run(View.java:18339)
   at android.os.Handler.handleCallback(Handler.java:725)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5283)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
   at dalvik.system.NativeStart.main(Native Method)

我的代码如下:

将列表分配给数组适配器

public class Home extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    Spinner spinner=(Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(this);

    //Spinner Drop Down elements
    List<String> rooms_num=new ArrayList<String>();
    rooms_num.add("1 room");
    rooms_num.add("2 rooms");
    rooms_num.add("3 rooms");
    rooms_num.add("4 rooms");
    rooms_num.add("5 rooms");
    rooms_num.add("6 rooms");

    //Adapter for Spinner
    ArrayAdapter<String> roomsNumAdapter=new ArrayAdapter<String>(this, R.layout.spinner_layout_text, R.id.spinnerItem, rooms_num);
    roomsNumAdapter.setDropDownViewResource(R.layout.spinner_layout_row);
    spinner.setAdapter(roomsNumAdapter);

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    String item = parent.getItemAtPosition(position).toString();
    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
}

我在我的微调器上使用两个xml布局文件,就像我在不同的网站上找到的那样, 第一个是:

spinner_layout_row.xml文件

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

    <ListView
        android:id="@+id/spinner_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </ListView>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

,第二个是:

spinner_layout_text.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="1dip" >
<TextView
    android:id="@+id/spinnerItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:textSize="12sp" >
</TextView>
</RelativeLayout>

最后,这是我的活动xml布局文件:

activity_home.xml文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout 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"
    tools:context=".Home"
    android:background="#03122F"
    android:padding="10dp"
    android:orientation="vertical">
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/txt_LoggedInUser_Title"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:textColor="#FFF"
            android:padding="5dp"
            android:textStyle="bold"
            android:layout_height="wrap_content" android:text="Welcome" />
        <TextView
            android:id="@+id/txt_LoggedInUser_TYPE"
            android:layout_width="wrap_content"
            android:layout_weight="2"
            android:textColor="#D4D4D8"
            android:padding="5dp"
            android:textStyle="bold"
            android:layout_height="wrap_content"  />
        <TextView
            android:id="@+id/txt_LoggedInUser"
            android:layout_width="wrap_content"
            android:layout_weight="2"
            android:textColor="#FFF"
            android:padding="5dp"
            android:textStyle="bold"
            android:layout_height="wrap_content" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6">

        <Spinner
            android:background="#FFF"
            android:layout_margin="5dp"
            android:id="@+id/spinner"
            android:textStyle="bold"
            android:textSize="15sp"
            android:layout_gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"

            />

    </LinearLayout>
</LinearLayout>
</ScrollView>

您的帮助将不胜感激。 感谢

4 个答案:

答案 0 :(得分:0)

尝试按照

编辑 spinner_layout_text.xml
<TextView
android:id="@+id/spinnerItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dip" 
android:padding="2dp"
android:textSize="12sp">
</TextView>

我认为问题是你给relativeLayout 方向,只适用于LinearLayout

答案 1 :(得分:0)

ArrayAdapter<String> roomsNumAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2,rooms_num);

或使用customadapter

答案 2 :(得分:0)

尝试使用parent.getAdapter().getItem(position)代替parent.getItemAtPosition(position)。这会将你的onItemSelected()方法改为:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String item = parent.getAdapter().getItem(position).toString();
    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}

修改

替换:

ArrayAdapter<String> roomsNumAdapter=new ArrayAdapter<String>(this,R.layout.spinner_layout_text,rooms_num);
roomsNumAdapter.setDropDownViewResource(R.layout.spinner_layout_row);
spinner.setAdapter(roomsNumAdapter);

使用:

ArrayAdapter<String> roomsNumAdapter=new ArrayAdapter<String>(this, R.layout.spinner_layout_text, R.id.spinnerItem, rooms_num);
spinner.setAdapter(roomsNumAdapter);

答案 3 :(得分:0)

初始化Spinner后:

spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
//Spinner Drop Down elements
ArrayList<String> rooms_num = new ArrayList<String>();
rooms_num.add("1");
rooms_num.add("2");
rooms_num.add("3");
rooms_num.add("4");
rooms_num.add("5");
rooms_num.add("6");

CustomSpinnerAdapter customSpinnerAdapter=new CustomSpinnerAdapter(getApplicationContext(),rooms_num);
spinner.setAdapter(customSpinnerAdapter);

使用名为 CustomSpinnerAdapter 的微调器的自定义适配器,如下所示:

package mo.ed.prof_mohamedatef.yourbestonlinetv;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Prof-Mohamed Atef on 11/1/2016.
 */
public class CustomSpinnerAdapter extends BaseAdapter {

    private final Context mContext;
    private ArrayList<String> SpinnerFeedItemList;

    public CustomSpinnerAdapter(Context mContext, ArrayList<String> spinnerFeedItemList) {
    this.mContext= mContext;
    SpinnerFeedItemList = spinnerFeedItemList;
    }

    @Override
    public int getCount() {
        return SpinnerFeedItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return SpinnerFeedItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return (long) position;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        TextView txt = new TextView(mContext);
        txt.setPadding(16, 16, 16, 16);
        txt.setTextSize(18);
        txt.setGravity(Gravity.CENTER_VERTICAL);
        txt.setText(SpinnerFeedItemList.get(position));
        txt.setTextColor(Color.parseColor("#000000"));
        return  txt;
    }
    TextView txt;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        txt = new TextView(mContext);
        txt.setGravity(Gravity.CENTER);
        txt.setPadding(10, 10, 10, 10);
        txt.setTextSize(16);
        txt.setText(SpinnerFeedItemList.get(position));
        txt.setTextColor(Color.parseColor("#000000"));
        txt.setBackgroundResource(R.color.yellow);
        return  txt;
    }
}

最后,它成功运作。感谢所有回复。