将可绘制图像从第1个活动传递到第2个不起作用(仅从第2个到1个)Android

时间:2015-10-25 12:11:59

标签: java android android-intent android-studio

enter code here这是我的第一个活动::>我需要: 1)选择一个选项 2)点击发送图像(从drawable)应该转到第二个活动,但它不仅从第2个到第1个工作正常我的所有xml和清单文件都是正确的(我仔细检查)

package com.example.damianlopez.myassignment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button toBob;
    private ImageView burgerImage;
    private RadioGroup radioGroupChoice;
    private RadioButton radBurger;
    private RadioButton radScare;
    private RadioButton radSick;
    private final int MY_SELECTION = 111;


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

            //get radio Buttons
           //radioGroupChoice = (RadioGroup) findViewById(R.id.radGroupChoice);
           radBurger = (RadioButton)findViewById(R.id.radBurger);
           radScare = (RadioButton)findViewById(R.id.radScare);
           radSick = (RadioButton)findViewById(R.id.radSick);


           //display app icon
           ActionBar ab = getSupportActionBar();
           ab.setDisplayShowHomeEnabled(true);
           ab.setIcon(R.mipmap.ic_launcher);

           //getting Intent
           Intent getIntent = getIntent();
           Bundle myBundle2 = getIntent.getExtras();
           setResult(Activity.RESULT_OK, getIntent);

           //
       }catch(Exception e ){

    }

    }//End onCreate()
    public void onClickButton(View v){
        try {
            //get radio gruop id's
            radioGroupChoice = (RadioGroup) findViewById(R.id.radGroupChoice);

            int mySelection = radioGroupChoice.getCheckedRadioButtonId();
            if(radioGroupChoice.getCheckedRadioButtonId() == -1){
                Toast.makeText(getApplicationContext(), "Please make a selection", Toast.LENGTH_LONG).show();
            }else {
                //creating new Intent
                Intent msgToBob = new Intent(MainActivity.this, Main22Activity.class);
                Bundle myBundle = new Bundle();
                myBundle.putInt("imageChoice", mySelection);
                msgToBob.putExtras(myBundle);
                startActivityForResult(msgToBob, MY_SELECTION);
            }

        }catch(Exception e ){

        }

    }//End of onClick()
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent myData) {
        super.onActivityResult(requestCode, resultCode, myData);
        Log.i("OnActivityResult() call", "***********");
        radioGroupChoice = (RadioGroup) findViewById(R.id.radGroupChoice);
        int selected = radioGroupChoice.getCheckedRadioButtonId();
        //get Images
        burgerImage = (ImageView) findViewById(R.id.pumpkin_burger);
        if(radioGroupChoice.getCheckedRadioButtonId() == -1){
            Toast.makeText(getApplicationContext(), "Please make a selection", Toast.LENGTH_LONG).show();
        }
        else {
            switch (requestCode) {
                case MY_SELECTION: {
                    if (resultCode == Activity.RESULT_OK) {
                        if (radBurger.isChecked()) {
                            burgerImage.setImageResource(R.drawable.pumpkin_burger);
                        } else if (radScare.isChecked()) {
                            burgerImage.setImageResource(R.drawable.pumpkin_scared);
                        } else if (radSick.isChecked()) {
                            burgerImage.setImageResource(R.drawable.pumpkin_sick);
                        };
                    }
                break;
                }//end case
            }//end switch
        }//end else if

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();``

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

这是我的第二项活动===>

package com.example.damianlopez.myassignment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class Main22Activity extends AppCompatActivity {
    private RadioGroup radioGroupChoice2;
    private RadioButton radBurger;
    private RadioButton radScare;
    private RadioButton radSick;
    private Button toAlice;
    private ImageView currentImage;
    private ImageView scareImage;
    private ImageView sickImage;
    private final int MY_SELECTION2 = 222;

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

        //display app icon
        ActionBar ab = getSupportActionBar();
        ab.setDisplayShowHomeEnabled(true);
        ab.setIcon(R.mipmap.ic_launcher);
        //Buttons
        radioGroupChoice2 = (RadioGroup)findViewById(R.id.radGroupChoice);
        //toAlice = (Button) findViewById(R.id.sndToAlice);

        //get the intent called
        Intent myLocalIntent = getIntent();

        Bundle myBundle = myLocalIntent.getExtras();
        myLocalIntent.putExtras(myBundle);
        setResult(Activity.RESULT_OK, myLocalIntent);


    }
    public void onClickButton2(View v){

        try {
            radioGroupChoice2 = (RadioGroup) findViewById(R.id.radGroupChoice);
            if (radioGroupChoice2.getCheckedRadioButtonId() == -1) {
                Toast.makeText(getApplicationContext(), "Please make a selection", Toast.LENGTH_LONG).show();
            } else {
                Intent myIntent2 = new Intent();
                Bundle myBundle2 = new Bundle();
                int myImage = radioGroupChoice2.getCheckedRadioButtonId();
                myBundle2.putInt("selectedImage", myImage);
                myIntent2.putExtras(myBundle2);
                setResult(Activity.RESULT_OK, myIntent2);

            }
        }catch(Exception e) {

        }
        finish();
   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main22, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

0 个答案:

没有答案