簡體   English   中英

無法接收從Intent發送的Bundle中找到的任何值

[英]Can't receive any value found in Bundle sent from an Intent

我正在研究我的第一個Android應用程序,並且我在編碼時意識到我有一些值,我沒有從一個意圖中的另一個活動發送的包中收到。 我能夠隔離代碼,我只是不知道為什么我沒有收到它的值。

public class Registration extends Activity {
    EditText edit1;
    EditText edit2;
    EditText edit3;
    EditText edit4;
    JSONParser jsonParser = new JSONParser();
    public static String url_data="";
    private static final String TAG_SUCCESS = "operation";
    public final static String user="com.example.try1.MESSAGE";
    public final static String pass="com.example.try1.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        setContentView(R.layout.activity_registration);
         edit1= (EditText) findViewById(R.id.nameuser);
         edit2= (EditText) findViewById(R.id.email);
         edit3= (EditText) findViewById(R.id.username);
         edit4= (EditText) findViewById(R.id.password);


        Button btnRegister= (Button) findViewById(R.id.new_user);

        btnRegister.setOnClickListener(new View.OnClickListener(){

            public void onClick(View view){
                //new CreateNewProduct().execute();
                String message1= edit1.getText().toString();
                String message2= edit2.getText().toString();
                String message3= edit3.getText().toString();
                String message4= edit4.getText().toString();
                Intent i = new Intent(Registration.this, Login.class);
                Bundle extras=new Bundle();
                Log.d("username", message3);
                extras.putString(user, message3);
                extras.putString(pass, message4);
                i.putExtras(extras);
                startActivity(i);
                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.registration, menu);
        return true;
    }
}

這是注冊的代碼。 現在這是Login.class的代碼

public class Login extends Activity {
    JSONParser jsnParser = new JSONParser();
    public static String url_login="http://www.reflap.com/account/reflap_login";
    private static final String TAG_SUCCESS = "operation";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //setContentView(R.layout.activity_login);
          //Intent it =getIntent();
          Bundle extras=this.getIntent().getExtras();
            String message=extras.getString("user");
            String message2=extras.getString("pass");
            //Log.d("username", message);
            if(message==null){
                Log.d("username", "empty value");
            }
            //Create the text view
            TextView text = new TextView(this);
            text.setTextSize(40);
            text.setText(message);

            setContentView(text);
        //new Loggerin().execute();
    }

log.d寫入空值,因為message1為null ,與message2相同。 此視圖也沒有任何內容。 如果我替換它

String message=extras.getString("user");
String message2=extras.getString("pass");

有了這個

String message=extras.getString(Registration.user);
String message2=extras.getString(Registration.pass);

我只收到最后一個值集。 我只是想使用bundle發送兩個我想在登錄類中使用的字符串。

您正在使用類似的鍵將兩個對象發送到另一個活動。 這將導致丟失已發送的字符串。

嘗試使用不同的密鑰發送字符串。

public final static String user="user";
public final static String pass="pass";

除了在接收活動中,您沒有使用您用於從另一個活動發送數據的相同密鑰。 您應該使用相同的密鑰來接收您曾經從另一個活動發送的數據。

public final static String user="user";
public final static String pass="pass"; 

然后

extras.putString(user, message3);
extras.putString(pass, message4);  

要得到

Bundle extras=getIntent().getExtras();
String message=extras.getString("user");
String message2=extras.getString("pass");

您也可以執行以下操作,而不是使用靜態最終字符串作為鍵

 extras.putString("user", message3);
 extras.putString("pass", message4);  

鑰匙必須匹配。

public String getString (String key)

在API級別1中添加

返回與給定鍵關聯的值,如果給定鍵不存在所需類型的映射,或者null值與鍵明確關聯,則返回null。

參數

key一個String,或null

返回

String值,或null

您在下面分配相同字符串的 原因

   public final static String user="com.example.try1.MESSAGE";
    public final static String pass="com.example.try1.MESSAGE";

所以改變它 //使字符串相關,字符串不應該相同;

public final static String user="user";
public final static String pass="pass";

你將能夠獲得兩者的數據

String message=extras.getString(Registration.user);
String message2=extras.getString(Registration.pass);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM