![](/img/trans.png)
[英]How can I take XML attribute and find a file that matches the attribute value in a given directory java
[英]How can I change XML button attribute from a java file?
我试图从我的java文件中更改按钮上显示的文本,但运气不佳。 在各种论坛上搜索了一段时间之后,并进行了google搜索,我学会了创建一个新按钮,然后使用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()
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 com.KenanDeHart.thebasics.Showrole$1.onClick(Showrole.java:51)
我真的不确定这里发生了什么。 这是我第一次使用Java,而我只使用了两天。 我希望这个问题能引起足够的解释,并且向您保证我已经完成了研究但无济于事。 我真的很感谢别人能给我的帮助。 谢谢。
如果只是更改按钮上显示的文本。
将其添加为实例变量(在所有方法之外)
Button nextplayerButton =null;
在onCreate()方法中获取按钮的引用。
nextplayerButton = (Button) findViewById(R.id.nextplayer);
nextplayerButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nextplayerButton.setText(""+i);
}
});
根据LogCat,在第42行的display()方法中有一个NullpointerException。
引起原因:java.lang.NullPointerException 11-30 06:44:37.147:E / AndroidRuntime(6601):在com.KenanDeHart.thebasics.Showrole.display(Showrole.java:42)
只要检查是否有未初始化的东西。
通常,这应该起作用:
setContentView(R.layout.activity_mail_sender);
int i = 2;
btn = (Button) findViewById(R.id.autoSendBtn);
btn.setText(""+i);
如果XML文件中已经存在按钮,则无需在Java代码中创建按钮。 肯定还有其他问题。 请发布您的logcat错误
好吧,我知道发生了什么事。
数组“ playArray”没有从我的其他意图正确传递。 我查看了变量树,发现它实际上等于null。 我通过发送具有意图的区域大小(这是上一类中确定的变量)并在到达后创建数组来解决此问题。
感谢大家的帮助。 我对Stack Overflow的支持和帮助印象深刻。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.