简体   繁体   English

如何从Java文件更改XML按钮属性?

[英]How can I change XML button attribute from a java file?

I am attempting to change the text displayed on a button from my java file, and am not having much luck. 我试图从我的java文件中更改按钮上显示的文本,但运气不佳。 After searching for awhile on various forums, and doing google searches, I have learned to create a new button and then use button.setText. 在各种论坛上搜索了一段时间之后,并进行了google搜索,我学会了创建一个新按钮,然后使用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. 我发现问题出在这个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();
        }



    }
}

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. 当我按继续时,发生了很多事情。

  1. A file opens called 打开一个名为

    ZygoteInit$MethodAndArgsCaller.run() ZygoteInit $ MethodAndArgsCaller.run()

  2. My phone vibrates once 我的手机振动了一次
  3. About 3 seconds pass 大约3秒钟过去
  4. My phone vibrates three times quickly 我的手机快速震动三下
  5. The program closes 程序关闭
Here is my logcat log 这是我的logcat日志

11-30 12:47:28.770: E/AndroidRuntime(9770):     at com.KenanDeHart.thebasics.Showrole$1.onClick(Showrole.java:51)

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. 这是我第一次使用Java,而我只使用了两天。 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. 谢谢。

If its just changing the text displayed on button. 如果只是更改按钮上显示的文本。

add this as instance variable (outside all the methods) 将其添加为实例变量(在所有方法之外)

Button nextplayerButton =null;

get reference of the button in onCreate() method. 在onCreate()方法中获取按钮的引用。

nextplayerButton = (Button) findViewById(R.id.nextplayer);
nextplayerButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
nextplayerButton.setText(""+i);
}
});

According to the LogCat, you have a NullpointerException in your display() method at line 42. 根据LogCat,在第42行的display()方法中有一个NullpointerException。

Caused by: java.lang.NullPointerException 11-30 06:44:37.147: E/AndroidRuntime(6601): at com.KenanDeHart.thebasics.Showrole.display(Showrole.java:42) 引起原因:java.lang.NullPointerException 11-30 06:44:37.147:E / AndroidRuntime(6601):在com.KenanDeHart.thebasics.Showrole.display(Showrole.java:42)

Just check if there's something that's not initialized there. 只要检查是否有未初始化的东西。

Normally, this should work: 通常,这应该起作用:

setContentView(R.layout.activity_mail_sender);
        int i = 2;
        btn = (Button) findViewById(R.id.autoSendBtn);
        btn.setText(""+i);

You don't have to create button in java code if its already there in xml file. 如果XML文件中已经存在按钮,则无需在Java代码中创建按钮。 There must be some other issue. 肯定还有其他问题。 Please post your logcat error 请发布您的logcat错误

Alright, I figured out what was happening. 好吧,我知道发生了什么事。

The array "playArray" was not correctly being passed from my other intent. 数组“ playArray”没有从我的其他意图正确传递。 I looked into the variable tree and found it was actually equal to null. 我查看了变量树,发现它实际上等于null。 I solved this by sending the size of the area (which was a variable determined in the previous class) with the intent, and creating the array after arrival. 我通过发送具有意图的区域大小(这是上一类中确定的变量)并在到达后创建数组来解决此问题。

Thank you everyone for your help. 感谢大家的帮助。 I am impressed with the support and helpfulness found on Stack Overflow. 我对Stack Overflow的支持和帮助印象深刻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM