gpt4 book ai didi

java - 如何将文本设置为整数并获取 int 而不会出错

转载 作者:太空宇宙 更新时间:2023-11-03 11:29:41 25 4
gpt4 key购买 nike

我试图从整数中获取 Intent 。 String get intent 工作正常并且显示良好但是当我输入整数时出现强制关闭错误。

package kfc.project;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class productdetail extends Activity{

@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.productdetail);
//stuff to get intent
Intent receivedIntent = getIntent();

String productName = receivedIntent.getStringExtra("name");
int productCalories = receivedIntent.getIntExtra("calories",0);

Bundle extras = getIntent().getExtras();

String name = extras.getString("name");

if (name != null) {
TextView text1 = (TextView) findViewById(R.id.servingsize);
text1.setText(productName);

}
//int calories = extras.getInt("calories");

TextView text1 = (TextView) findViewById(R.id.calories);
text1.setText(productCalories);


}

}

最佳答案

TextView text1 = (TextView) findViewById(R.id.calories);
text1.setText(""+productCalories);

或者

text1.setText(String.valueOf(productCalories));

关于java - 如何将文本设置为整数并获取 int 而不会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8327235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com