Android/Java:使用 EditText/getText 更改公共类字符串变量
首先我想说这是我唯一的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须创建 Player 的实例并将其分配给playerOne 变量。
You have to create an instance of Player and assign it to playerOne variable.