繁体   English   中英

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

[英]Getting Intent values from a class and set Intent to another class

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

单击btnEasy1后,它将 go 到ButtonFunctions class 并为GameActivity class 设置必要的设置。

PS。 我现在只在 PPS 上尝试这个按钮( btnEasy1 )。 该错误表明我正在尝试在 null object 参考上调用虚拟方法。

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$2.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中,因为它们似乎非常依赖它。 但这是您需要做出的架构选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM