自定义可搜索微调器在单击微调器时引发空指针异常

Posted

技术标签:

【中文标题】自定义可搜索微调器在单击微调器时引发空指针异常【英文标题】:Custom searchable spinner throws null pointer exception while clicking on the spinner 【发布时间】:2021-12-12 10:45:26 【问题描述】:

主要活动

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
    

CustomSpinner 适配器:

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

【问题讨论】:

【参考方案1】:

我认为是因为排序

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) 
    

    
);

spinner_text.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_
    android:layout_
    android:ellipsize="marquee"
    android:singleLine="true"
    tools:text="hello" />

simple_spinner_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_
    android:layout_
    android:ellipsize="marquee"
    android:paddingBottom="5dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    android:singleLine="true" />

【讨论】:

我是否必须单独在我的活动中添加此代码并且不需要自定义适配器。我说得对吗兄弟? 你试过这个 onItemSelected 并检查它是否工作 spin.setOnItemSelectedListener(this);删除它并调用 spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener( ) 是的,兄弟,我试过你的代码,它只适用于微调器中的一列,但我需要显示所有三列,并且可以按所有三列过滤它。 我按照你的说法更改了代码( spin.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener( ) )并尝试了,但结果与我在原帖中提到的相同

以上是关于自定义可搜索微调器在单击微调器时引发空指针异常的主要内容,如果未能解决你的问题,请参考以下文章

是否可以使用微调器在相同的 android 活动中添加列表视图

Android-XML 自定义微调器

Bash:如何在使用微调器时获取命令的退出代码?

空指针异常 - 片段活动

Android:减小微调器下拉框的宽度

微调器值过大,并非全部可见,并且没有滚动条