Issue
I am trying to write a program so that whenever the button called 'front' is pressed all other buttons will change to gray except the button that equals GreenButton. I am struggling with accessing the buttons in the for loop, and keep getting a null pointer exception.
The full logcat is shown below, showing that the NPE is occurring when I am trying to access the array in the loop
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)
CODE:
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;
}
});
}
}
Solution
You can try using new Button[size], something like this:
// allocates memory for 6 button.
Button[] buttons = new Button[6];
Then, add the buttons to it:
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);
buttons[0] = front;
buttons[1] = a;
buttons[2] = b1;
buttons[3] = b2;
buttons[4] = b3;
buttons[5] = b4;
Explanation:
When you creating the array with:
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};
You're creating an array with null object elements, which is like
Button[] buttons = {null,null,null,null,null,null};
Then when you assign the Button to one of the array member with:
front = (Button) findViewById(R.id.front);
You're assigning an object to front
not to buttons[0]
.
You can try the following code to understand what the above explanation means:
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
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);
// Check address:
Log.d(TAG, "Before initializing..");
for(int i = 0; i < buttons.length; i++) {
Log.d(TAG, "i " + buttons[i]);
}
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);
Log.d(TAG, "After initializing..");
for(int i = 0; i < buttons.length; i++) {
Log.d(TAG, "i " + buttons[i]);
}
Log.d(TAG, "After assigning..");
Log.d(TAG, "front " + front);
Log.d(TAG, "a " + a);
Log.d(TAG, "b1 " + b1);
Log.d(TAG, "b2 " + b2);
Log.d(TAG, "b3 " + b3);
Log.d(TAG, "b4 " + b4);
}
}
Answered By - ישו אוהב אותך
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.