简体   繁体   English

.putExtra意向后应用崩溃

[英]App crashes after .putExtra intent

I'm trying to send a variable from one activity to another. 我正在尝试将变量从一个活动发送到另一个活动。 When I try to test the app on my phone, it crashes and says the app has stopped working. 当我尝试在手机上测试该应用程序时,它崩溃并表示该应用程序已停止运行。

Code from sending side 来自发送方的代码

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

and code on receiving side 和接收方的代码

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



}}

If you know what the issue is please let me know. 如果您知道问题所在,请告诉我。 I've spent hours looking for a solution. 我已经花了几个小时寻找解决方案。 Thank you 谢谢

you need to move this TextView btn = (TextView) findViewById(R.id.text); 您需要移动此TextView btn = (TextView) findViewById(R.id.text); inside oncreate because before oncreate there is no layout attached to Scroll activity and finding textview by using this findViewById(R.id.text) will result in a failure hence crash oncreate因为在oncreate之前没有附加到Scroll活动的布局,并且使用此findViewById(R.id.text)查找textview将导致失败,从而导致崩溃

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

Note : Use appropriate naming convention for clarity , like textViewVar for TextView instead of btn and convert your int value to text using ""+value because TextView allow only String to use 注:为清楚起见,使用适当的命名约定,如textViewVarTextView ,而不是btn和转换您的inttext使用""+value ,因为TextView只允许String使用

I think the problem is in this call: btn.setText(intValue); 我认为问题出在此调用中: btn.setText(intValue); : string resource ID with value 5 does not exist. :值5的字符串资源ID不存在。 Try replacing it with btn.setText(Integer.toString(intValue)); 尝试将其替换为btn.setText(Integer.toString(intValue));

In your activity 在您的活动中

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



}}

Try this 尝试这个

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

        }

and in Recieving activity put this in oncreate 并在接收活动中将其放在oncreate上

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

Like this 像这样

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



    }}

change a line to: btn.setText(String.valueOf(intValue)); 将行更改为: 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));
}}

Replace your code by this 以此替换您的代码

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

    }
}

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

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