setText到另一个活动?

时间:2015-12-30 04:26:40

标签: java android settext

我在一个名为" HomePage"的活动上有一个按钮。单击按钮时,我希望它将setText设置为名为" weaponTitle"的TextView。在一个单独的活动上,称为ItemSelection。但是,当我运行setText时,它会给出错误:

  

尝试在void android.widget.TextView.setText(java.lang.CharSequence)对象引用

上调用虚方法null

这意味着它无法找到TextView" weaponTitle"。有没有好办法解决这个问题?我已经确定我也正确设置了所有内容!

这是我忘记分享的一小段代码!

new displayPrices(weaponTitle, "Genuine Freedom Staff");

4 个答案:

答案 0 :(得分:1)

试试这个

<强>的Firstclass

 Intent intent = new Intent(getApplicationContext,SecondActivity.class);
        intent.putExtra("KEY",value);
        intent.putExtra("KEY",VALUE);
        startActivity(intent)

第二项活动

 Intent intent =getIntent();
 confirm_txt =(TextView)findViewById(R.id.txt);
 String txt_put =intent.getStringExtra("KEY");
 confirm_tId.setText(txt_put);

答案 1 :(得分:0)

通过这种方式,

从您的第一个活动中传递

Intent i = new Intent(Login.this, ChangePasswordActivity.class);
        i.putExtra("savedUser", savedUser);
        Log.e("username", "--->>" + savedUser);
        startActivity(i);
在第二项活动中

,请按以下方式获取

Intent extras = getIntent();
        savedUser = extras.getStringExtra("savedUser");
        Log.e("savedUser", "--->>" + savedUser);

现在,您可以在其他活动中设置文本。

etUsername.setText(userName);

答案 2 :(得分:0)

  Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
        intent.putExtra("weaponTitle","Genuine Freedom Staff");
        startActivity(intent);

在第二项活动中:

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String weaponName = bundle.getString("weaponTitle");



TextView textView = (TextView) findViewById(R.id.textView);
   textView.setText(weaponName);

答案 3 :(得分:0)

您的HomePage活动的oncrete内部

new recyclerAdapter.ClickListener() {
    @Override
    public void onClick(int position) {

        ... awesome item onClick code ...

        notifyItemChanged(position);
        //notifyDataSetChanged(); <//--- Causes the no ripple bug
    }
};

Inside ItemSelection Activity的oncreate

Button yourbutton = (Button) findViewById(R.id.your_button_id);
         yourbutton.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
                 Intent intent = new Intent(HomePage.this, ItemSelection.class);
                intent.putExtra("weapontitle",value);
                startActivity(intent);
             }
         });