首先,我想说这是我唯一的 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)