无法从儿童活动中获得结果

时间:2012-10-20 19:28:52

标签: android

我有一个父活动,它将数据发送到子活动,但是孩子没有返回结果。我在下面的两个活动中都发布了部分代码 - 还有一些代码,我没有发布不必要的代码。

    public class TdeeActivity extends Activity {

    public static final int CALLED_ACTIVITY = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tdee);
        Button ok=(Button)findViewById(R.id.btnOk);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent bmr= new Intent(TdeeActivity.this,BMRActivity.class);
                startActivityForResult(bmr,CALLED_ACTIVITY);
            }
        });
        }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode) {
        case CALLED_ACTIVITY:
            if (resultCode == RESULT_OK) {

                Toast.makeText(this, "THE RESULT-"+data.getExtras().getString("result"),
                        Toast.LENGTH_SHORT).show();
            }
        }

    }
}  // end class

public class BMRActivity extends Activity{


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_bmr);
       Button btnOk=(Button)findViewById(R.id.btnOk);
       btnOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                showResult();
            }
        });

        public void showResult() {


            Intent data= new Intent();
            data.putExtra("result",result);
            setResult(RESULT_OK, data);
            finish();



    }   



}




}// end class

1 个答案:

答案 0 :(得分:0)

我在一个虚拟项目中测试了你的代码,它在我的结尾工作正常..以下是两个活动的代码:

public class ParentActivity extends Activity {

    private static final int CALLED_ACTIVITY = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent bmr = new Intent(ParentActivity.this, ChildActivity.class);
        startActivityForResult(bmr, CALLED_ACTIVITY);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode) {
        case CALLED_ACTIVITY:
            if (resultCode == RESULT_OK) {

                Toast.makeText(this, "THE RESULT-"+data.getExtras().getString("result"),
                        Toast.LENGTH_SHORT).show();
            }
        }

    }
}


public class ChildActivity extends Activity {

    final Activity activity = this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Returning result from child activity
        Intent data = new Intent();
        data.putExtra("result", "from child"
                + this.getCallingActivity().getClassName());
        setResult(RESULT_OK, data);
        finish();
    }
}

所以请发布更多代码,以便找到问题..