应用程序在.putExtra意图后崩溃

时间:2016-12-29 05:26:58

标签: java android android-intent

我试图将变量从一个活动发送到另一个活动。当我尝试在手机上测试应用程序时,它会崩溃并说应用程序已停止工作。

发送方的代码

if (id==R.id.action_cart){

        Intent good1 = new Intent(MainActivity.this, Scroll.class);

        good1.putExtra("intVariableName", 5);
        startActivity(good1);

        TextView hides = (TextView) findViewById(R.id.textView3);
        hides.setVisibility(View.INVISIBLE);
    }

和接收方的代码

public class Scroll extends AppCompatActivity {
TextView btn = (TextView) findViewById(R.id.text);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scroll);

    Intent mIntent = getIntent();
    int intValue = mIntent.getIntExtra("intVariableName", 0);

    btn.setText(intValue);



}}

如果您知道问题所在,请告诉我们。我花了好几个小时寻找解决方案。谢谢

6 个答案:

答案 0 :(得分:2)

您需要在TextView btn = (TextView) findViewById(R.id.text);内移动此oncreate,因为oncreate之前没有任何布局附加到Scroll个活动,并且使用此{textview找到findViewById(R.id.text) 1}}将导致失败因此崩溃

public class Scroll extends AppCompatActivity {
    TextView btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll);
        btn = (TextView) findViewById(R.id.text)
        //^^^^
        Intent mIntent = getIntent();
        int intValue = mIntent.getIntExtra("intVariableName", 0);
        btn.setText(""+intValue);                
      }
}

注意:为了清晰起见,请使用适当的命名约定,例如textViewVar代替TextView代替btn,并使用{{1}将int值转换为text因为""+value仅允许TextView使用

答案 1 :(得分:1)

我认为问题出在这个调用中:btn.setText(intValue);:不存在值为5的字符串资源ID。尝试将其替换为btn.setText(Integer.toString(intValue));

答案 2 :(得分:-1)

在您的活动中

public class Scroll extends AppCompatActivity {
TextView btn ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scroll);
    btn = (TextView) findViewById(R.id.text); // initialize your button here
    Intent mIntent = getIntent();
    int intValue = mIntent.getIntExtra("intVariableName", 0);

    btn.setText(String.valueOf(intValue));



}}

答案 3 :(得分:-1)

试试这个

    if (id==R.id.action_cart){
         //hide views before moving
           TextView hides = (TextView) findViewById(R.id.textView3);
            hides.setVisibility(View.INVISIBLE);
            Intent good1 = new Intent(MainActivity.this, Scroll.class);
            good1.putExtra("intVariableName", 5);
            startActivity(good1);

        }

和接收活动    把它放在oncreate

btn = (TextView) findViewById(R.id.text);

喜欢这个

 public class Scroll extends AppCompatActivity {
    TextView btn ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll);
        btn = (TextView) findViewById(R.id.text);
        Intent mIntent = getIntent();
        int intValue = mIntent.getIntExtra("intVariableName", 0);
        btn.setText(intValue);



    }}

答案 4 :(得分:-1)

将一行更改为: btn.setText(String.valueOf(intValue));

public class Scroll extends AppCompatActivity {
TextView btn = (TextView) findViewById(R.id.text);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scroll);

    Intent mIntent = getIntent();
    int intValue = mIntent.getIntExtra("intVariableName", 0);

    btn.setText(String.valueOf(intValue));
}}

答案 5 :(得分:-1)

用此替换您的代码

public class Scroll extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_scroll);

        TextView btn = (TextView) findViewById(R.id.text);

        Intent mIntent = getIntent();
        int intValue = mIntent.getIntExtra("intVariableName", 0);

        btn.setText(intValue);

    }
}