Android/Java:使用 EditText/getText 更改公共类字符串变量

发布于 2024-12-09 03:15:25 字数 2601 浏览 3 评论 0原文

首先我想说这是我唯一的java经历。我已经把c++搞得一团糟了,但已经有一段时间了,所以这可能只是愚蠢地缺乏对java类的理解。

好吧,我有一个名为 Player 的简单类文件:

package com.iRprojects.HelloAgain;


public class Player{
    public int Health;
    public int Strength;
    public String Name;

}

然后我有另一个名为 Options 的类(这是我决定用于此目的的先前活动。此外,这是从另一个名为主菜单的活动打开的。)

package com.iRprojects.HelloAgain;


import android.app.Activity;
import android.view.View;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.iRprojects.HelloAgain.Player;



public class Options extends Activity {

        EditText name;
        Button getName;

        TextView playerName;

        Player playerOne;




        @Override

        public void onCreate(Bundle options) {
            super.onCreate(options);



            setContentView(R.layout.options);


            name = (EditText) findViewById(R.id.tName);
            getName = (Button) findViewById(R.id.bGetName);

            playerName = (TextView) findViewById(R.id.vName);

            getName.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    playerOne.Name = name.getText().toString(); //This is the problem

                    playerName.setText(playerOne.Name);
                }
            });


        }


}

我正在尝试使用来自 EditText 的输入来设置玩家名称,但是当我单击“getName”按钮时它崩溃了。我认为可以肯定地说,我要么设置了错误的类,创建了错误的类成员,要么调用/声明了错误的变量。任何帮助将不胜感激。

如果有帮助的话,这是调试日志:

HelloAgain [Android Application]    
    DalvikVM[localhost:8600]    
        Thread [<1> main] (Suspended (exception NullPointerException))  
            Options$1.onClick(View) line: 46    
            Button(View).performClick() line: 2485  
            View$PerformClick.run() line: 9080  
            ViewRoot(Handler).handleCallback(Message) line: 587 
            ViewRoot(Handler).dispatchMessage(Message) line: 92 
            Looper.loop() line: 123 
            ActivityThread.main(String[]) line: 3683    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 507  
            ZygoteInit$MethodAndArgsCaller.run() line: 864  
            ZygoteInit.main(String[]) line: 622 
            NativeStart.main(String[]) line: not available [native method]  
        Thread [<8> Binder Thread #2] (Running) 
        Thread [<7> Binder Thread #1] (Running) 

First off I'd like to say this is the only java experience I've had. I've messed around with c++ a good bit but its been awhile, so this may just be a stupid lack of understanding java classes.

Alright so I have a simple class file called Player:

package com.iRprojects.HelloAgain;


public class Player{
    public int Health;
    public int Strength;
    public String Name;

}

Then I have another class called Options (It was a previous activity that I decided to use for this. Also this is opened from another activity called main menu.)

package com.iRprojects.HelloAgain;


import android.app.Activity;
import android.view.View;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.iRprojects.HelloAgain.Player;



public class Options extends Activity {

        EditText name;
        Button getName;

        TextView playerName;

        Player playerOne;




        @Override

        public void onCreate(Bundle options) {
            super.onCreate(options);



            setContentView(R.layout.options);


            name = (EditText) findViewById(R.id.tName);
            getName = (Button) findViewById(R.id.bGetName);

            playerName = (TextView) findViewById(R.id.vName);

            getName.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    playerOne.Name = name.getText().toString(); //This is the problem

                    playerName.setText(playerOne.Name);
                }
            });


        }


}

I'm trying to use the input from the EditText to set the player name, but when I click the "getName" button it crashes. I think its pretty safe to say I either set up the class wrong, created a class member wrong, or called/declared the variables wrong. Any help would be appreciated.

Heres the debug log if it helps:

HelloAgain [Android Application]    
    DalvikVM[localhost:8600]    
        Thread [<1> main] (Suspended (exception NullPointerException))  
            Options$1.onClick(View) line: 46    
            Button(View).performClick() line: 2485  
            View$PerformClick.run() line: 9080  
            ViewRoot(Handler).handleCallback(Message) line: 587 
            ViewRoot(Handler).dispatchMessage(Message) line: 92 
            Looper.loop() line: 123 
            ActivityThread.main(String[]) line: 3683    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 507  
            ZygoteInit$MethodAndArgsCaller.run() line: 864  
            ZygoteInit.main(String[]) line: 622 
            NativeStart.main(String[]) line: not available [native method]  
        Thread [<8> Binder Thread #2] (Running) 
        Thread [<7> Binder Thread #1] (Running) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云醉月微眠 2024-12-16 03:15:25

您必须创建 Player 的实例并将其分配给playerOne 变量。

Player playerOne = new Player();

You have to create an instance of Player and assign it to playerOne variable.

Player playerOne = new Player();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文