本文介绍了自定义可搜索微调控件在单击微调控件时抛出空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要活动

public class CustomSpinnerActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {


    ArrayList<String> data = new ArrayList<String>();
    ArrayList<String> data1 = new ArrayList<String>();
    ArrayList<String> data2 = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chit_spinner);
        //Getting the instance of Spinner and applying OnItemSelectedListener on it

        SearchableSpinner spin = (SearchableSpinner) findViewById(R.id.simpleSpinner) ;
        spin.setOnItemSelectedListener(this);

        //spinner variyable
        CloudDBConnection connect = new CloudDBConnection();
        Connection con = connect.CONN();
        if (con != null) {
            try {
                Statement statement = con.createStatement();
                String query = "select * from chitCardDet;";
                ResultSet rs = statement.executeQuery(query);
                //Toast.makeText(getApplicationContext(), "You are out.", Toast.LENGTH_SHORT).show();

                if (rs.next()) {

                    String custName = rs.getString(3);
                    if(custName.equals(null)){
                        custName = "NA";
                    }
                    String cell = rs.getString(8);
                    if(cell.equals(null)){
                        cell = "NA";
                    }
                    String city = rs.getString(9);
                    if(city.equals(null)){
                        city = "NA";
                    }
                    data.add(custName);
                    data1.add(cell);
                    data2.add(city);
                   // Toast.makeText(getApplicationContext(), custName+cell+city, Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getApplicationContext(), "You are out.....", Toast.LENGTH_SHORT).show();
                }

                CustomSpinnerAdapter customAdapter=new CustomSpinnerAdapter(getApplicationContext(),data,data1,data2);
                spin.setAdapter(customAdapter);

                rs.close();
                statement.close();
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } else {
            Toast.makeText(getApplicationContext(), "Server Unable......", Toast.LENGTH_SHORT).show();
        }

    }


    //Performing action onItemSelected and onNothing selected
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
        Toast.makeText(getApplicationContext(), data.get(position), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
}

CustomSpiner适配器:

public class CustomSpinnerAdapter extends ArrayAdapter {
    Context context;
    ArrayList<String> data;
    ArrayList<String> data1;
    ArrayList<String> data2;
    LayoutInflater inflter;

    public CustomSpinnerAdapter(Context applicationContext, ArrayList<String> data, ArrayList<String> data1, ArrayList<String> data2) {
        super(applicationContext,custom_spinner_items);
        this.context = applicationContext;
        this.data = data;
        this.data1 = data1;
        this.data2 = data2;
        inflter = (LayoutInflater.from(applicationContext));
    }


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

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflter.inflate(custom_spinner_items, null);
        TextView cust = (TextView) view.findViewById(R.id.custName);
        TextView mobile = (TextView) view.findViewById(R.id.mobileNumber);
        TextView cityname = (TextView) view.findViewById(R.id.city);
        cust.setText(data.get(i));
        mobile.setText(data1.get(i));
        cityname.setText(data2.get(i));
        return view;
    }
}

单击the spinner时抛出以下error message

NullpointerException:Attempt to invoke virtual method java.lang.String java.lang.Object.toString() on a null object reference

仅供参考:它在已加载时工作,但不能在单击微调控件时工作

请谁帮帮我……我正在尝试解决它,但我解决不了问题。

enter image description here

推荐答案

我想是因为订购的原因

Spinner sItems = (Spinner) findViewById(R.id.select_size_spinner);


  ArrayList<String> options = new ArrayList<>();
    options.add("custName");
    options.add("cell");
    options.add("city");

ArrayAdapter<String> langAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_text, options);
    langAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown);
    sItems.setAdapter(langAdapter);

sItems.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener( )
{
    @Override
    public void onItemSelected( AdapterView< ? > parent, View view, int pos, long id )
    {
        String selected = String.valueOf(adapterView.getItemAtPosition(position));

        int  Select = Integer.parseInt(selected);

        Toast.makeText(getApplicationContext(), Select, Toast.LENGTH_SHORT).show();


    }

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

    }
});

spner_ext.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/text1"
    style="?attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="?dropdownListPreferredItemHeight"
    android:ellipsize="marquee"
    android:singleLine="true"
    tools:text="hello" />

Simple_spner_dropdown.xml文件

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="@style/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:paddingBottom="5dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    android:singleLine="true" />

这篇关于自定义可搜索微调控件在单击微调控件时抛出空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 08:20