繁体   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