简体   繁体   中英

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

I'm working on my first Android app and I realized while coding that I had values I wasn't receiving from a bundle sent from an another activity in an intent. I was able to isolate the code and I just an't figure out why I don't receive the values from it.

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;
    }
}

That's the code for the registration. Now this is the code for the 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();
    }

The log.d writes empty value because message1 is null , same thing with message2. Also the view has nothing on it. If I replace this

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

with this

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

I only receive the last value set. I'm just trying to use bundles to send two strings that I want to use in the login class.

You are using similar keys to send two objects to another activity. This will cause in the loss of the sent strings.

Try to use different keys for sending the Strings.

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

Besides in the receiving activity you are not using the same key you used to send data from another activity. You should use the same key for receiving data which you used to send from another activity.

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

Then

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

To get

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

You can also do as below instead of using static final string as keys

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

Keys must match.

public String getString (String key)

Added in API level 1

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Parameters

key a String, or null

Returns

a String value, or null

Reason you are assigning same strings below

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

so change it // make String something relevant and string should not be same ;

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

than you will be able to get data for both using

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM