I am attempting to change the text displayed on a button from my java file, and am not having much luck. After searching for awhile on various forums, and doing google searches, I have learned to create a new button and then use button.setText. However, after my application would suddenly end, displaying a message about being forced to quit, I decided to do some debugging. I found the problem to be in this Java class.
package com.KenanDeHart.thebasics;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
public class Showrole extends Activity {
public static int players;
public static int idx;
public static int[] playArray;
public static Button nextplayerButton =null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_role);
Bundle extras = getIntent().getExtras();
Showrole.players = extras.getInt("PLAYERS");
for (idx = 0; idx < players ; ++idx){
display(idx);
++idx;
}
}
public void display(int idx){
Bundle extras = getIntent().getExtras();
playArray = extras.getIntArray("PLAYARRAY");
nextplayerButton = (Button) findViewById(R.id.nextplayer);
nextplayerButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nextplayerButton.setText(playArray[Showrole.idx]);
}
});
}
@Override
protected void onPause() {
super.onPause();
}
public void next(){
Bundle extras = getIntent().getExtras();
Button button = (Button)findViewById(R.id.nextplayer);
button.setText("Next Player");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
After using breakpoints, I think I have located the problematic source code.
public void display(int idx){
Bundle extras = getIntent().getExtras();
playArray = extras.getIntArray("PLAYARRAY");
nextplayerButton = (Button) findViewById(R.id.nextplayer);
nextplayerButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nextplayerButton.setText(playArray[Showrole.idx]);
}
});
}
More specifically, this line
nextplayerButton.setText(playArray[Showrole.idx]);
When I press continue, a string of things happen.
- A file opens called
ZygoteInit$MethodAndArgsCaller.run()
- My phone vibrates once
- About 3 seconds pass
- My phone vibrates three times quickly
- The program closes
11-30 12:47:28.770: E/AndroidRuntime(9770): FATAL EXCEPTION: main
11-30 12:47:28.770: E/AndroidRuntime(9770): java.lang.NullPointerException
11-30 12:47:28.770: E/AndroidRuntime(9770): at com.KenanDeHart.thebasics.Showrole$1.onClick(Showrole.java:51)
11-30 12:47:28.770: E/AndroidRuntime(9770): at android.view.View.performClick(View.java:2461)
11-30 12:47:28.770: E/AndroidRuntime(9770): at android.view.View$PerformClick.run(View.java:8888)
11-30 12:47:28.770: E/AndroidRuntime(9770): at android.os.Handler.handleCallback(Handler.java:587)
11-30 12:47:28.770: E/AndroidRuntime(9770): at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 12:47:28.770: E/AndroidRuntime(9770): at android.os.Looper.loop(Looper.java:123)
11-30 12:47:28.770: E/AndroidRuntime(9770): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-30 12:47:28.770: E/AndroidRuntime(9770): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 12:47:28.770: E/AndroidRuntime(9770): at java.lang.reflect.Method.invoke(Method.java:521)
11-30 12:47:28.770: E/AndroidRuntime(9770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-30 12:47:28.770: E/AndroidRuntime(9770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-30 12:47:28.770: E/AndroidRuntime(9770): at dalvik.system.NativeStart.main(Native Method)
Pay attention to this error
11-30 12:47:28.770: E/AndroidRuntime(9770): at com.KenanDeHart.thebasics.Showrole$1.onClick(Showrole.java:51)
I really am unsure what is happening here. This is my first time using Java, and I have only been using it for two days. I hope this question is explanatory enough, and I assure you I have done my research but to no avail. I really appreciate any help someone can give me. Thank you.
nextplayerButton.setText(playArray[Showrole.idx]);which is line number 51.