无法同时将两个字符串从一个活动传递到另一个活动

时间:2015-04-01 05:47:05

标签: java android android-intent android-activity

我有两个活动选择和上传。我将两个字符串id和scan_id从一个活动传递到另一个活动。当我单独传递id时,该值正确传递。但是当我尝试传递两个值时,一个值会覆盖另一个值。是否有单独的方法来传递两个字符串???

SelectActivity.java

public class SelectActivity extends Activity {
String v1;
public static String EXTRA_MESSAGE_ID;
public static String EXTRA_MESSAGE_SCAN;
int flag;
String strflag;

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                                    .permitAll()
                                    .build();


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

    Bundle b = getIntent().getExtras();
    EditText ed=(EditText)findViewById(R.id.patient_id);
    ed.setText(b.getCharSequence("Contents"));

    Button button = (Button) findViewById(R.id.button1);
    StrictMode.setThreadPolicy(policy);

    Button CTbutton = (Button) findViewById(R.id.CTScan);
    CTbutton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
            flag=1;
            strflag = String.valueOf(flag);
            Log.e("log_tag", "sel id"+flag);
            Log.e("log_tag", "sel scan"+v1);

            Intent intent = new Intent(SelectActivity.this,MainActivity.class);
            intent.putExtra(EXTRA_MESSAGE_ID,v1);
            intent.putExtra(EXTRA_MESSAGE_SCAN,strflag);
            startActivity(intent);
        }
    });
}
}

MainActivity.java

public class MainActivity extends Activity {    
  private static final String TAG = MainActivity.class.getSimpleName();

  private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;

  public static final int MEDIA_TYPE_IMAGE = 1;

  private Uri fileUri;
  private Button btnCapturePicture;
  public static String EXTRA_MESSAGE_ID;
  public static String EXTRA_MESSAGE_SCAN;
  String message_id;
  String message_scan;

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

    getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.color.action_bar))));
    Intent intent = getIntent();
    message_id = intent.getStringExtra(SelectActivity.EXTRA_MESSAGE_ID);
    message_scan = intent.getStringExtra(SelectActivity.EXTRA_MESSAGE_SCAN);

    Log.e("log_tag", "id"+message_id);
    Log.e("log_tag", "scan"+message_scan);

    btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
   /**
    * Capture image button click event
    */
    btnCapturePicture.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        captureImage();
      }
    });
  }
}

logcat看起来像这样:

04-01 10:57:43.671: E/log_tag(12649): connection success 
04-01 10:57:50.374: E/log_tag(12649): sel id1
04-01 10:57:50.374: E/log_tag(12649): sel scan2
04-01 10:57:50.403: E/log_tag(12649): id1
04-01 10:57:50.403: E/log_tag(12649): scan1

3 个答案:

答案 0 :(得分:2)

使用相同的值初始化两个活动中的字符串

public static String EXTRA_MESSAGE_ID="something1";

public static String EXTRA_MESSAGE_SCAN="something2";

答案 1 :(得分:1)

您的逻辑是正确的,但在下面的代码中存在问题

public static String EXTRA_MESSAGE_ID;
public static String EXTRA_MESSAGE_SCAN;

您没有将值分配给两个变量。所以默认情况下两个变量都有空值。因此,当您输入值时,第二个将覆盖它。因为两个varable相同的键都是空白值 因此,尝试使用给变量赋予唯一值。

public static String EXTRA_MESSAGE_ID = "MSG_ID";
public static String EXTRA_MESSAGE_SCAN = "MSG_SCAN";

我希望你明白。

答案 2 :(得分:0)

你应该这样试试:

更改 SelectActivity

 public static String EXTRA_MESSAGE_ID="ExtraMsgId";
 public static String EXTRA_MESSAGE_SCAN="ExtraMsgScan";

不应该这样做。如果你这样给出

public static String EXTRA_MESSAGE_ID;
public static String EXTRA_MESSAGE_SCAN;

这看起来像空字符串。