关于Intent requestCode,Android

时间:2013-08-21 11:37:51

标签: android

为什么显示我的toast消息?我的CEVAP_SORGULA变量等于322,但我的意图请求代码是332

private final static int CEVAP_SORGULA = 322;
public void degistirActivity(final View view){
    startActivityForResult(new Intent(this,veriTopla.class),332);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if (resultCode == Activity.RESULT_OK && requestCode == CEVAP_SORGULA ){
        Toast.makeText(this, "Cevabin: " + data.getExtras().getInt("Cevap"),Toast.LENGTH_LONG).show();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

2 个答案:

答案 0 :(得分:3)

我清楚地看到CEVAP_SORGULA与通过startActivityForResult传递的值之间的类型错误。要修复它,请提供变量而不是硬编码的错误值。

startActivityForResult(new Intent(this,veriTopla.class),CEVAP_SORGULA);

答案 1 :(得分:2)

你可以尝试

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 332);

默认方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}

从安德鲁开发者网站上看这个例子,让你帮忙

 public class MyActivity extends Activity {
     ...

     static final int PICK_CONTACT_REQUEST = 0;

     protected boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
             // When the user center presses, let them pick a contact.
             startActivityForResult(
                 new Intent(Intent.ACTION_PICK,
                 new Uri("content://contacts")),
                 PICK_CONTACT_REQUEST);
            return true;
         }
         return false;
     }

     protected void onActivityResult(int requestCode, int resultCode,
             Intent data) {
         if (requestCode == PICK_CONTACT_REQUEST) {
             if (resultCode == RESULT_OK) {
                 // A contact was picked.  Here we will just display it
                 // to the user.
                 startActivity(new Intent(Intent.ACTION_VIEW, data));
             }
         }
     }
 }