滚动后单击项目时listview中onitemclicklistener的Nullpointerexception

时间:2014-09-28 10:01:44

标签: android listview android-listview

我有ListView CustomAdapter onclick,现在listview可以通过滚动nullpointerexception我可以访问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); } 来获取 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 。此外,当我点击任何适合屏幕尺寸的项目时,滚动后特定元素也会被选中..如果我选择第4个位置,并且一次显示8个项目,那么第12个项目也将被选中..

{{1}}

}

logcat的:

{{1}}

3 个答案:

答案 0 :(得分:0)

可能存在问题:

 View v = checkpoints_list.getChildAt(position);

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

答案 1 :(得分:0)

试试这个:

@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);

}

答案 2 :(得分:0)

我的答案为时已晚,但如果可以对他人有所帮助,请给出答案。

首先,我尝试通过AdapterView<?> parent的{​​{1}}参数获取元素。与提问者一样使用OnItemClickListener参数也是一样的结果。

我的代码:

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 } }); 代替get参数解决了问题。像这样:

adpater

这对我很有帮助:))