我试图从我的
java文件中更改按钮上显示的文本,并没有太多运气.在各种论坛上搜索一段时间,并进行谷歌搜索后,我学会了创建一个新按钮,然后使用button.setText.但是,在我的应用程序突然结束后,显示一条关于被迫退出的消息,我决定做一些调试.我发现问题出在这个
Java类中.
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();
}
}
}
使用断点后,我想我找到了有问题的源代码.
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]);
}
});
}
更具体地说,这一行
nextplayerButton.setText(playArray[Showrole.idx]);
当我按下继续时,会发生一系列事情.
>打开一个名为的文件
ZygoteInit$MethodAndArgsCaller.run()
>我的手机振动一次
>大概3秒钟过去了
>我的手机快速振动三次
>该计划结束
这是我的logcat日志
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)
注意这个错误
11-30 12:47:28.770: E/AndroidRuntime(9770): at com.KenanDeHart.thebasics.Showrole$1.onClick(Showrole.java:51)
我真的不确定这里发生了什么.这是我第一次使用Java,而且我只用了两天.我希望这个问题足够解释,我向你保证我做了我的研究,但无济于事.我非常感谢有人能给我的任何帮助.谢谢.