我是android编程的新手,我不明白如何在活动和自定义视图之间传递参数。 我的活动仅实现自定义视图,我在自定义视图中使用getter方法来传递字符串。该字符串用于在文本视图中设置播放器的名称。 在setPlayerName方法中打印字符串似乎已正确传递,但实际上,当我执行changeTurn()方法时,变量mPlayerName为null。我不明白为什么。
这是我的活动
by
这是我的自定义视图
public class GameActivity extends AppCompatActivity {
private GameMap gameMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
gameMap = new GameMap(this, "MYNAME");
gameMap.setPlayerName("MYNAME");
setContentView(R.layout.activity_game);
}
}
我已经修改了构造函数代码,并且收到的错误消息是
public class GameMap extends RelativeLayout {
private String mPlayerName;
private TextView tv;
private int turn;
public GameMap(Context context, String playerName) {
super(context);
init(context,playerName);
}
public GameMap(Context context, AttributeSet attrs, String playerName) {
super(context, attrs);
init(context, playerName);
}
public GameMap(Context context, AttributeSet attrs, int defStyleAttr, String playerName) {
super(context, attrs, defStyleAttr);
init(context, playerName);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public GameMap(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, String playerName) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, playerName);
}
private void init(Context context){
View rootView = inflate(context,R.layout.game_custom_layout,this);
tv = rootView.findViewById(R.id.turno);
turn = 0;
if(turn == 0) {
//here mPLayerName is null
tv.setText(mPlayerName);
}
else {
tv.setText("CPU");
}
createMap();
}
private void createMap(){
//I don't change mPLayerName here.
}
public void setPlayerName(String playerName){
this.mPlayerName = playerName;
System.out.println("string " + playerName + "mPLayerName " +mPlayerName);
//here playerName and mPlayerName are not null but there is the correct string
}
@Override
protected void dispatchDraw(Canvas canvas) {
//draw the map, don't edit mPlayerName
}
private void changeTurn(){
if(turn == 0) {
turn = 1;
tv.setText("CPU");
}
else {
turn = 0;
System.out.println("player name " + mPlayerName);
tv.setText(mPlayerName);
//here mPlayerName is null
}
}
private void movePlayer(int x, int y, float newX, float newY){
if(/*a specific condition */){
invalidate();
return;
}
changeTurn();
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
movePlayer(cellX, cellY, newX, newY);
return true;
}
if(event.getAction() == MotionEvent.ACTION_MOVE)
return false;
return super.onTouchEvent(event);
}
}
这是布局
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.game.project, PID: 24310
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.game.project/com.game.project.GameActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class com.crossthebox.progetto.GameMap
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class com.game.project.GameMap
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class com.game.project.GameMap
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor0(Class.java:2327)
at java.lang.Class.getConstructor(Class.java:1725)
at android.view.LayoutInflater.createView(LayoutInflater.java:615)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.game.project.GameActivity.onCreate(GameActivity.java:22) // this is setContentView(R.layout.activity_game);
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案 0 :(得分:0)
在您的init
方法中,您在设置mPlayerName
之前调用了mPlayerName
变量,因此它为您提供了一个空String
。
要解决此问题,您可以创建一个接受另一个参数playerName
的构造函数。
public GameMap(Context context, String playerName) {
super(context);
mPlayerName = playerName;
init(context);
}
使用上述代码,您的mPlayerName
将在调用init
方法之前设置,因此tv
将设置正确的String
值。