从 class 获取 Intent 值并将 Intent 设置为另一个 class

Getting Intent values from a class and set Intent to another class

大家好,有人可以帮我解决这个过程吗?我只是想在一个按钮上测试这个。假设:

点击btnEasy1后,会进入ButtonFunctionsclass,对GameActivityclass进行必要的设置。

PS。我暂时只在一个按钮(btnEasy1)上尝试这个 PPS。该错误表明我正在尝试对空对象引用调用虚拟方法。

ImageButton btnEasy1, btnEasy2, btnEasy3, btnEasy4, btnEasy5, btnEasy6, btnEasy7, btnEasy8, btnEasy9, btnEasy10, btnBack;
    int row = 0 , column = 0 , stage = 0;
    ButtonFunctions btnFunc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level__easy);

        this.btnEasy1 = findViewById(R.id.btnEasy1);
        this.btnEasy2 = findViewById(R.id.btnEasy2);
        this.btnEasy3 = findViewById(R.id.btnEasy3);
        this.btnEasy4 = findViewById(R.id.btnEasy4);
        this.btnEasy5 = findViewById(R.id.btnEasy5);
        this.btnEasy6 = findViewById(R.id.btnEasy6);
        this.btnEasy7 = findViewById(R.id.btnEasy7);
        this.btnEasy8 = findViewById(R.id.btnEasy8);
        this.btnEasy9 = findViewById(R.id.btnEasy9);
        this.btnEasy10 = findViewById(R.id.btnEasy10);
        this.btnBack = findViewById(R.id.btnBack);

        //Easy Level intents
        Intent easyLevelIntent = new Intent(getApplicationContext(), GameActivity.class);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), Level_Selection.class);
                startActivity(intent);
                finish();
            }
        });

        btnEasy1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnFunc =new ButtonFunctions();
                btnFunc.functions("easy1");
                finish();
            }
        });
public class ButtonFunctions extends AppCompatActivity {

    int row, column, stage;

    public void functions(String diffStage){
        Intent easyLevelIntent = new Intent(getApplicationContext(), GameActivity.class);

        if(diffStage.equals("easy1")){
            stage = 1 ;row = 2; column = 2 ;
            setRowCol(easyLevelIntent);
            startActivity(easyLevelIntent);
        }
    }
    private void setRowCol(Intent easyLevelIntent) {
        easyLevelIntent.putExtra("easyStageCount", stage);
        easyLevelIntent.putExtra("rowCount", row);
        easyLevelIntent.putExtra("colCount", column);
        easyLevelIntent.putExtra("difficulty", 1);
    }
    Process: com.flip, PID: 10543
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
        at com.flip.Functions.ButtonFunctions.functions(ButtonFunctions.java:14)
        at com.flip.StageSelection.Level_Easy.onClick(Level_Easy.java:54)
        at android.view.View.performClick(View.java:5680)
        at android.view.View$PerformClick.run(View.java:22650)
        at android.os.Handler.handleCallback(Handler.java:836)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6364)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1076)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:937)

您已将 ButtonFunctions 声明为扩展 AppCompatActivity。但是这个 class 不是 Activity,它只是一个实用程序 class。您收到此 NullPointerException 是因为您使用 new 关键字创建了 Activity。这是不允许的。只有 Android 框架可以正确创建 Android 组件(Activity, Service, Provider)的新实例。

您应该从 ButtonFunctions class 声明中删除 extends AppCompatActivity 并向任何需要参数的方法添加 Context 参数。比如这个:

public void functions(Context context, String diffStage){
    Intent easyLevelIntent = new Intent(context, GameActivity.class);

    if(diffStage.equals("easy1")){
        stage = 1 ;row = 2; column = 2 ;
        setRowCol(easyLevelIntent);
        context.startActivity(easyLevelIntent);
    }
}

另一种方法是将所有方法从 ButtonFunctions 移动到您的 Activity,因为它们似乎非常依赖它。但这是您需要做出的架构选择。