Android:onActivityResult null值

时间:2014-03-21 13:55:40

标签: android android-intent onactivityresult

我正在尝试从mainActivitiy类中获取从colorActivity返回的值,但值retrieve为null,我不明白为什么。我能做些什么才能从colorAcvitity获得价值?

我的代码是: colorActivity

    public static final String CODE_KEY = "CODE";

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_ecolor);


        circlenametextview = (TextView)findViewById(R.id.circlenametextview);


        String circlename = super.getIntent().getStringExtra("circlename");
          circlenametextview.setText(circlename);//get the circle name


savebutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {


               colorcode = colorEditText.getText().toString();
                Intent resultIntent = new Intent();
                 resultIntent.putExtra(CODE_KEY, colorcode );

                   setResult(Activity.RESULT_OK, resultIntent);
                   finish();
            }// onclick

        });
        }

mainActivity类

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

        if (requestCode == 1) { /

            if (resultCode == RESULT_OK) {

                Bundle buddle = data.getExtras();
                ccode = buddle.getString(colorActivity.CODE_KEY);

        }

        }
    }

    public static String getcolorcode() { // so that i can use it in my view class
        return ccode; 
    }

我的观点类:

      @Override
        public boolean onTouchEvent(MotionEvent event) {

            x = event.getX();
            y = event.getY();


            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:


                for (int i = 0; i < circles.size(); i++) {


                    if (circles.get(i).contains(x, y)) {
                        circleID = i;

        Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    AlertDialog.Builder builder = new Builder(
                                            getContext());
                                    final EditText text = new EditText(getContext());

                                    builder.setTitle("Adding colors to circles").setMessage(
                                            "Proceed to Enter color");
                                    builder.setPositiveButton("Yes",
                                            new DialogInterface.OnClickListener() {

                                                public void onClick(DialogInterface di,
                                                        int i) {

                       Intent intent = new Intent(getContext(),colorActivity.class);
                                                                                                                                                        intent.putExtra("circlename", circleNameList.get(circleID));


                    ((Activity) getContext()).startActivityForResult(intent,1);


                                                }

                                            });
                                    builder.setNegativeButton("No",
                                            new DialogInterface.OnClickListener() {

                                                public void onClick(DialogInterface di,
                                                        int i) {
                                                }

                                            });

                                    builder.create().show();
                                }
                            }, 3000);
break;

}

2 个答案:

答案 0 :(得分:0)

你在myview类中有startActivityforResult,你期望在主Activity类中返回结果。在myview类中添加onActivityResult。

答案 1 :(得分:0)

尝试运行

来自UI线程的

startActivityFromResult(),因此修改您的视图代码,添加以下内容:

Activity activity = ((Activity) getContext());

activity.runOnUiThread(new Runnable() {

    @Override
    public void run() {

        Intent intent = new Intent(activity, colorActivity.class);
        ((Activity) getContext()).startActivityForResult(intent, 1);                
    }
});

更多信息here