[英]Android/Java: Using EditText/getText to change public class string variable
First off I'd like to say this is the only java experience I've had. 首先,我想说这是我仅有的Java经验。 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.
我已经弄乱了c ++,但是已经有一段时间了,所以这可能只是对理解Java类的愚蠢的缺乏。
Alright so I have a simple class file called Player: 好了,所以我有一个简单的类文件,称为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.) 然后,我有另一个名为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);
}
});
}
}
I'm trying to use the input from the EditText to set the player name, but when I click the "getName" button it crashes. 我正在尝试使用EditText的输入来设置播放器名称,但是当我单击“ getName”按钮时,它会崩溃。 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)
您必须创建Player的实例,并将其分配给playerOne变量。
Player playerOne = new Player();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.