0

我的应用程序有问题。我有一个名为 PetActivity 的活动。在这个活动中有一些值......还有一个按钮(更新)。此按钮将用户切换到另一个名为 UpdateActivity 的活动,他可以在其中编辑来自 PetActivity 的值。有一个保存按钮应该完成此活动并重新打开 PetActivity。我在这个动作中有错误......(空指针异常)

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.PetActivity}: java.lang.NullPointerException

PetActivity onclick 代码:

if (v.getId() == R.id.button1updateAP) {
        Intent i = new Intent(PetActivity.this, UpdateActivity.class);
        Bundle ext = getIntent().getExtras();
        String idOfPet = ext.getString(IDR);        
        i.putExtra(UpdateActivity.ID, idOfPet);         
        startActivity(i);
        finish();           
    }

UpdateActivity onclick 代码:

if (v.getId() == R.id.button1saveAU) {
        MyDatabaseHelper db = new MyDatabaseHelper(this);

        Pet petU = new Pet();
        petU.setID(id);
        petU.setName(TVname.getText().toString());          
        petU.setAge((ETage.getText().toString()));
        petU.setWeight(ETweight.getText().toString());          
        db.updatePet(petU);
        db.close();

        DatabaseWeight dbw = new DatabaseWeight(this);          
        dbw.addPetWeight(new Pet(petU.getID(), petU.getWeight()));          

        Intent i2 = new Intent(this, PetActivity.class);
        startActivity(i2);
        finish();
    }

当我尝试这个时,没关系:

Intent i2 = new Intent(this, ListActiv.class);
startActivity(i2);
finish();

这段代码让我切换到列表,我可以去刷新 PetActivity,但我必须直接进入 PetActivity。

我会非常感谢任何建议。

谢谢!

4

1 回答 1

2

从 PetActivity 启动 UpdateActivity 后,您无需调用 finish(),而在 UpdateActivity 内部,当您的工作完成后,只需在 finish() 之前调用 setResult()...

您的代码应该用于 MainActivity 的 onClick

if (v.getId() == R.id.button1updateAP) {
        Intent i = new Intent(PetActivity.this, UpdateActivity.class);
        Bundle ext = getIntent().getExtras();
        String idOfPet = ext.getString(IDR);        
        i.putExtra(UpdateActivity.ID, idOfPet);         
        startActivityForResult(i,some request code ););
        finish();           
    }

对于 ChildActivity 的 onClick

if (v.getId() == R.id.button1saveAU) {
        MyDatabaseHelper db = new MyDatabaseHelper(this);

        Pet petU = new Pet();
        petU.setID(id);
        petU.setName(TVname.getText().toString());          
        petU.setAge((ETage.getText().toString()));
        petU.setWeight(ETweight.getText().toString());          
        db.updatePet(petU);
        db.close();

        DatabaseWeight dbw = new DatabaseWeight(this);          
        dbw.addPetWeight(new Pet(petU.getID(), petU.getWeight()));          
        if(/*task completed successfully*/){
           setResult(RESULT_OK);
        }else{
           setResult(RESULT_CANCELED); 
        }
        finish();
    }

现在在您的父活动中,您可以在里面检查这些值onActivityResult()

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i(LOG_TAG, "Inside onActiviy Result : "+resultCode);
        if ( resultCode == RESULT_OK ) {
            Toast.makeText(this, "Update executed successfully..", Toast.LENGTH_SHORT).show();
        }else if ( resultCode == RESULT_CANCELED ) {
            Toast.makeText(this, "There was an error", Toast.LENGTH_SHORT).show();
        }
    }

另请参阅文档中的各种意图标志。

于 2012-12-18T10:24:00.670 回答