Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

Error when clicking on listview Android (Java)

I get an error whenever I click on an item in a listview from the 8th, the previous ones work perfectly

Details: when clicking on the list, I call the method with the number of the item clicked, with that number instantiating a RelativeLayout with the child of the listview with the item number, then, with the layout, instantiating a TextView and a CheckBox *Sorry for bad English

listview code:

@Override
public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
{
    removerDaLista(p3);
} });

method code:

public void removerDaLista(int index){
    paraTachar.add(Integer.valueOf(index));
    RelativeLayout rl = (RelativeLayout)listView.getChildAt(index);
    TextView txt = (TextView) rl.findViewById(R.id.text);
    CheckBox cb = (CheckBox) rl.findViewById(R.id.cb);
        
    desativarLista(txt,cb);
}

Error:

FATAL EXCEPTION: main
Process: com.mycompany.myapp, PID: 611
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
    at com.mycompany.myapp.MainActivity.removerDaLista(MainActivity.java:88)
    at com.mycompany.myapp.MainActivity$100000001.onItemClick(MainActivity.java:71)
    at android.widget.AdapterView.performItemClick(AdapterView.java:310)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1164)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3132)
    at android.widget.AbsListView.onTouchUp(AbsListView.java:4059)
    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3818)
    at android.view.View.dispatchTouchEvent(View.java:10023)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2626)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2307)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
    at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:416)
    at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1808)
    at android.app.Activity.dispatchTouchEvent(Activity.java:3124)
    at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:378)
    at android.view.View.dispatchPointerEvent(View.java:10243)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4438)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4306)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6247)
    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6221)
    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6182)
    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6350)
    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:323)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:6152)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:890)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:780)

question from:https://stackoverflow.com/questions/65862756/error-when-clicking-on-listview-android-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason for this is most probably the view could not be found. In your case it's the view R.id.text or R.id.cb or maybe both. Make sure those TextView and CheckBox exist in the layout or make sure you used their correct id.

EDIT: It may also be an index problem, the view at that index could not be found. In your code:

rl = (RelativeLayout)listView.getChildAt(index);

Replace it with:

 rl = (RelativeLayout)listView.getChildAt(index-1);

Maybe the index is starting from 1 instead of 0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...