【问题标题】:I can't understand how to pass parameters from activity to custom view我不明白如何将参数从活动传递到自定义视图
【发布时间】:2019-10-27 03:14:06
【问题描述】:

我是在 android 上编程的新手,我不明白如何在活动和自定义视图之间传递参数。 我的活动只实现了自定义视图,我在自定义视图中使用了一个getter方法来传递一个字符串。该字符串用于在文本视图中设置播放器的名称。 在 setPlayerName 方法中打印字符串似乎传递正确,但实际上当我执行 changeTurn () 方法时,变量 mPlayerName 为空。我不明白为什么。

这是我的活动

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)

这是布局

xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GameActivity">


    <com.game.project.GameMap
        android:id="@+id/gameMapView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 你能提供你尝试调用 changeTurn() 的代码吗?
  • 你在哪里打电话给changeTurn(),你实际上是如何膨胀GameMap自定义视图的?
  • @joshuadeguzman changeTurn() 在 movePlayer() 中被调用,我如何在 init() 方法中为 gameMap 充气

标签: android parameter-passing android-custom-view


【解决方案1】:

在您的init 方法中,您在设置mPlayerName 之前调用了mPlayerName 变量,因此它给您一个空的String

要解决这个问题,您可以创建一个接受另一个参数 playerName 的构造函数。

public GameMap(Context context, String playerName) {
    super(context);
    mPlayerName = playerName;
    init(context);
}

使用上面的代码,您的mPlayerName 将在调用init 方法之前设置,因此tv 将设置正确的String 值。

【讨论】:

  • 通过将字符串 playerName 参数添加到每个构造函数,所有其他构造函数会发生什么情况?但是通过这种方式,活动在 setContentView(R.layout.activity_game); 上崩溃
  • 你能发布整个构造函数吗?另外,在这里复制崩溃日志,这样我就可以知道是什么导致了错误。
  • 我修改了构造函数代码,收到的错误信息在底部
  • 您的R.layout.activity_game 布局似乎导致了错误。请发布布局。
  • 布局贴在底部
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-03
  • 1970-01-01
相关资源
最近更新 更多