gpt4 book ai didi

android - 从按钮数组修改按钮

转载 作者:行者123 更新时间:2023-11-29 16:51:22 26 4
gpt4 key购买 nike

我正在尝试编写一个程序,以便每当按下名为“front”的按钮时,除等于 GreenButton 的按钮外,所有其他按钮都将变为灰色。我正在努力访问 for 循环中的按钮,并不断收到空指针异常。

完整的 logcat 如下所示,表明当我试图在循环中访问数组时发生了 NPE

09-20 11:50:54.727 5302-5302/com.example.kshah.movingbutton E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kshah.movingbutton, PID: 5302
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setBackgroundColor(int)' on a null object reference
at com.example.kshah.movingbutton.MainActivity$1.onTouch(MainActivity.java:108)
at android.view.View.dispatchTouchEvent(View.java:9296)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
at android.app.Activity.dispatchTouchEvent(Activity.java:2771)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
at android.view.View.dispatchPointerEvent(View.java:9520)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
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:135)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

代码:

public class MainActivity extends AppCompatActivity {

private Button front;
private Button a;
private Button b1;
private Button b2;
private Button b3;
private Button b4;

Button[] buttons = {front,a,b1,b2,b3,b4};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

front = (Button) findViewById(R.id.front);
a = (Button) findViewById(R.id.a);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);

front.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Button GreenButton = b3;
for(int i = 0; i <= buttons.length; i++) {
if (buttons[i] == b3) {
buttons[i].setBackgroundColor(Color.GREEN);
}
else
buttons[i].setBackgroundColor(Color.GRAY);
}
return false;
}
});
}
}

最佳答案

您不能像这样将引用存储在初始化数组中

Button[] buttons = {front,a,b1,b2,b3,b4};

但是为什么?放手Array Initializers

The length of the array to be constructed is equal to the number of variable initializers immediately enclosed by the braces of the array initializer. Space is allocated for a new array of that length. If there is insufficient space to allocate the array, evaluation of the array initializer completes abruptly by throwing an OutOfMemoryError. Otherwise, a one-dimensional array is created of the specified length, and each component of the array is initialized to its default value

所以这里的引用将被替换为值 mean null 因此它们无法再跟踪实际引用值的变化

所以解决方案是声明具有所需长度的 arrayArrayList 并添加对它的引用。

//                  {null,null,null,....}
//Button[] buttons = {front,a,b1,b2,b3,b4};
Button[] buttons = new Button[6];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

buttons[0] = (Button) findViewById(R.id.front);
buttons[1] = (Button) findViewById(R.id.a);
buttons[2] = (Button) findViewById(R.id.b1);
buttons[3] = (Button) findViewById(R.id.b2);
buttons[4] = (Button) findViewById(R.id.b3);
buttons[5] = (Button) findViewById(R.id.b4);

buttons[0].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Button GreenButton = b3;
// note < instead of <=
for(int i = 0; i < buttons.length; i++) {
if (buttons[i] == buttons[4]) {
buttons[i].setBackgroundColor(Color.GREEN);
}
else
buttons[i].setBackgroundColor(Color.GRAY);
}
return false;
}
});

关于android - 从按钮数组修改按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46327879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com