简体   繁体   English

无法接收从Intent发送的Bundle中找到的任何值

[英]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. 我正在研究我的第一个Android应用程序,并且我在编码时意识到我有一些值,我没有从一个意图中的另一个活动发送的包中收到。 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 现在这是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. log.d写入空值,因为message1为null ,与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. 我只是想使用bundle发送两个我想在登录类中使用的字符串。

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 在API级别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. 返回与给定键关联的值,如果给定键不存在所需类型的映射,或者null值与键明确关联,则返回null。

Parameters 参数

key a String, or null key一个String,或null

Returns 返回

a String value, or null String值,或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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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