滚动后单击项目时列表视图中 onitemclicklistener 上的 Nullpointerexception

Posted

技术标签:

【中文标题】滚动后单击项目时列表视图中 onitemclicklistener 上的 Nullpointerexception【英文标题】:Nullpointerexception on onitemclicklistener in listview while clicking on item after scrolling 【发布时间】:2014-11-22 21:26:09 【问题描述】:

我有ListViewCustomAdapter,现在onclick 可以通过滚动listview 访问的任何项目我得到nullpointerexception。此外,当我单击任何适合屏幕尺寸的项目时,在滚动特定元素后也会被选中..如果我选择第 4 个位置并且一次显示 8 个项目,那么第 12 个项目也将被选中..

public class SelectCheckPoints extends ListActivity implements OnItemClickListener
String s1[];
ListView checkpoints_list;

@Override
protected void onCreate(Bundle savedInstanceState) 
    // TODO Auto-generated method stub

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

    Bundle b = this.getIntent().getExtras();
    s1 = b.getStringArray("array_checkpoints");
    checkpoints_list=getListView();

    checkpoints_list.setAdapter(new ItemAdapter());

    checkpoints_list.setOnItemClickListener(this);




public class ItemAdapter extends BaseAdapter 

    // private ImageLoadingListener animateFirstListener = new
    // AnimateFirstDisplayListener();

    @Override
    public int getCount() 
        return s1.length;
    

    @Override
    public Object getItem(int position) 
        return s1[position];
    

    @Override
    public long getItemId(int position) 
        Log.d("test id",position+"");
        return position;
    

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 

        if(convertView == null)
            // LayoutInflater class is used to instantiate layout XML file into its corresponding View objects.
            LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.checkpoint_listitem, null);
        

        TextView checkpoints_name = (TextView) convertView.findViewById(R.id.checkpoints_name);         
        checkpoints_name.setText(s1[position]);

        return convertView;

    



@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) 
    // TODO Auto-generated method stub

    Log.d("check","in");
    View v=checkpoints_list.getChildAt(position);
    CheckBox chk1=(CheckBox)v.findViewById(R.id.check);
    TextView title = (TextView) v.findViewById(R.id.checkpoints_name);
    if(chk1.isChecked())
    
        chk1.setChecked(false);
        Log.e("check or not","not");
    
    else
    
        chk1.setChecked(true);

        Log.e("check or not","not");
    
    Log.d("check or not",title.getText().toString());
    Log.d("check",position+" "+id);


LOGCAT:

    09-28 15:37:36.445: E/androidRuntime(2672): FATAL EXCEPTION: main
09-28 15:37:36.445: E/AndroidRuntime(2672): java.lang.NullPointerException
09-28 15:37:36.445: E/AndroidRuntime(2672):     at com.example.shareryde.book_share.SelectCheckPoints.onItemClick(SelectCheckPoints.java:89)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at android.os.Handler.handleCallback(Handler.java:725)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at android.os.Looper.loop(Looper.java:137)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at java.lang.reflect.Method.invokeNative(Native Method)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at java.lang.reflect.Method.invoke(Method.java:511)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-28 15:37:36.445: E/AndroidRuntime(2672):     at dalvik.system.NativeStart.main(Native Method)
09-28 15:37:38.125: I/Process(2672): Sending signal. PID: 2672 SIG: 9
09-28 15:37:38.325: E/Trace(2704): error opening trace file: No such file or directory (2)
09-28 15:37:38.569: D/(2704): HostConnection::get() New Host Connection established 0xb798d068, tid 2704

【问题讨论】:

你试过是:CheckBox chk1=(CheckBox)view.findViewById(R.id.check); 吗? 你能发布 Logcat.. 是的。但它也给了我同样的错误 我已经发布了LOGCAT输出@Dcoder 你确定 R.id.check 和 R.id.checkpoints_name 是在 R.layout.checkpoint_listitem 中定义的吗? 【参考方案1】:

问题大概出在这里:

 View v = checkpoints_list.getChildAt(position);

可变位置指定可见列表视图项中的位置。使用方法参数View view 或按照说明here。

【讨论】:

【参考方案2】:

试试这个:

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

    Log.d("check","in");

    CheckBox chk1=(CheckBox)view.findViewById(R.id.check);
    TextView title = (TextView) view.findViewById(R.id.checkpoints_name);
    if(chk1.isChecked())
    
        chk1.setChecked(false);
        Log.e("check or not","not");
    
    else
    
        chk1.setChecked(true);
        Log.e("check or not","not");
    
    Log.d("check or not",title.getText().toString());
    Log.d("check",position+" "+id);


【讨论】:

【参考方案3】:

我的回答来不及了,但如果对他人有帮助,请给出答案。

起初,我尝试通过OnItemClickListenerAdapterView&lt;?&gt; parent 参数获取元素。像提问者一样使用View view 参数的结果是一样的。

我的代码:

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
           ListView lv = (ListView) parent;
           TextView tv = (TextView) lv.getChildAt(position); //tv gets null 

           Toast.makeText(getActivity(), tv.getText().toString(), Toast.LENGTH_SHORT).show(); //NullPointerException
       
    );

通过使用adpater而不是get参数来解决。像这样:

    //this is my adapter
    mAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice, mListArray);
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
         @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                Toast.makeText(getActivity(), mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
         
    );

这对我真的很有帮助:)

【讨论】:

以上是关于滚动后单击项目时列表视图中 onitemclicklistener 上的 Nullpointerexception的主要内容,如果未能解决你的问题,请参考以下文章

OnItemClick 在列表视图中无法正常工作

OnItemClick 侦听器和单击的 View 项的可见性

Android - onClick Listview项目显示描述和窗口/视图/页面中的链接

如何使用滚动视图单击列表部分内的项目以导航到详细信息页面 SwiftUI

按钮 onClick 和 ListView onItemClick

SQLite 查询错误,onItemClick 导致应用崩溃