Java OnTouchListener无法获取数组项

时间:2018-07-30 21:40:16

标签: java android

我在数组列表中的按钮上设置了侦听器时遇到了一个很奇怪的问题。 我有一个似乎不起作用的for循环。 我按下的任何按钮都会使应用崩溃。 下面的代码仅定义了一个按钮,尽管如此,它仍无法完成工作... 我究竟做错了什么 ?

XML布局

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="20"
    android:columnCount="9">

    <Button
        android:id="@+id/jeton1"
        android:layout_column="0"
        android:layout_row="0"
        android:text="@string/boutona"
        android:layout_height="88dp"
        android:layout_width="88dp"
        android:textSize="40sp"
        android:backgroundTint="#eeceac"
        android:textStyle="bold" />

Java主类

public class MainActivity extends AppCompatActivity {

// Step 1 Array creation
private static final int[] idArray = {R.id.jeton1};

// Step 2 Set
private Button[] jeton = new Button[idArray.length];

int i;

@SuppressLint("ClickableViewAccessibility")
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Step 3 Assign array objects to buttons

    for (i=0; i <idArray.length ; i++){
        jeton[i] = (Button)findViewById(idArray[i]);

        // Step 4 Action listener

        jeton[i].setOnTouchListener(
                new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent event) {
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_UP:
                                jeton[i].getBackground().setAlpha(0); // Crashes the app
                                jeton[i].setTextColor(Color.parseColor("#2aa17b"));
                                break;
                                                       }
                        return true;
                    }

    });

}

}}

该行:jeton [i] .getBackground()。setAlpha(0);导致应用崩溃

(java.lang.ArrayIndexOutOfBoundsException:length = 1; index = 1)

Logcat日志:

07-30 16:39:41.686 3964-3964/com.example.namour.combiendejetons W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView

07-30 16:39:41.714 3964-3964 / com.example.namour.combiendejetons I /时间轴:时间轴:Activity_idle ID:android.os.BinderProxy@25c2780e时间:76491516 07-30 16:39:44.172 3964-3964 / com.example.namour.combiendejetons I / ViewRootImpl:ViewRoot的Touch事件:ACTION_DOWN 07-30 16:39:44.201 3964-3964 / com.example.namour.combiendejetons I / ViewRootImpl:ViewRoot的Touch事件:ACTION_POINTER_DOWN(1) 07-30 16:39:44.228 3964-3964 / com.example.namour.combiendejetons I / ViewRootImpl:ViewRoot的Touch事件:ACTION_POINTER_UP(1) 07-30 16:39:44.273 3964-3964 / com.example.namour.combiendejetons I / ViewRootImpl:ViewRoot的Touch事件:ACTION_UP 07-30 16:39:44.291 3964-3964 / com.example.namour.combiendejetons E / InputEventReceiver:调度输入事件的异常。 07-30 16:39:44.292 3964-3964 / com.example.namour.combiendejetons E / MessageQueue-JNI:MessageQueue回调中的异常:handleReceiveCallback 07-30 16:39:44.302 3964-3964 / com.example.namour.combiendejetons E / MessageQueue-JNI:java.lang.ArrayIndexOutOfBoundsException:length = 1;索引= 1         在com.example.namour.combiendejetons.MainActivity $ 1.onTouch(MainActivity.java:45)         在android.view.View.dispatchTouchEvent(View.java:8389)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2376)         在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1727)         在android.app.Activity.dispatchTouchEvent(Activity.java:2783)         在android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)         在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2337)         在android.view.View.dispatchPointerEvent(View.java:8585)         在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4074)         在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:3940)         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3485)         在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3538)         在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3504)         在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3512)         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3485)         在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3538)         在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3504)         在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3614)         在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3512)         在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3671)         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3485)         在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3538)         在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3504)         在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3512)         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3485)         在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5759)         在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5733)         在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5704)         在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5878)         在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)         在android.os.MessageQueue.nativePollOnce(本地方法)         在android.os.MessageQueue.next(MessageQueue.java:143)         在android.os.Looper.loop(Looper.java:122)         在android.app.ActivityThread.main(ActivityThread.java:5272)         在java.lang.reflect.Method.invoke(本机方法)         在java.lang.reflect.Method.invoke(Method.java:372)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:909)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 07-30 16:39:44.302 3964-3964 / com.example.namour.combiendejetons D / AndroidRuntime:关闭VM 07-30 16:39:44.304 3964-3964 / com.example.namour.combiendejetons E / AndroidRuntime:FATAL EXCEPTION:main     流程:com.example.namour.combiendejetons,PID:3964      java.lang.ArrayIndexOutOfBoundsException:length = 1;索引= 1         在com.example.namour.combiendejetons.MainActivity $ 1.onTouch(MainActivity.java:45 )         在android.view.View.dispatchTouchEvent(View.java:8389)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)

非常感谢您的答复

0 个答案:

没有答案