gpt4 book ai didi

java - 我该如何解决这个 "Method invocation ' bundle.getString ("Ans_age").equals(R.string.age2 )' may produce ' java.lang.NullPointerException'"

转载 作者:搜寻专家 更新时间:2023-11-01 07:51:08 26 4
gpt4 key购买 nike

为什么我总是在 if else 语句中收到此消息:方法调用 'bundle.getString("Ans_phy").equals(R.string.active)' 可能会产生 'java.lang.NullPointerException'

我的提交按钮也不起作用,因为它假设在我单击提交按钮后在下一个 Activity 中显示“新体重”和“卡路里”。

评论.java

public void setBtnSubmit()
{
//Create a bundle object to store the bundle added to the intent
final Bundle bundle = getIntent().getExtras();

//Get the values out by key
final String umur = bundle.getString("Ans_age");
final String textBMI = bundle.getString("Ans_bmi");
final String aktiviti = bundle.getString("Ans_phy");

final String textCategory = bundle.getString("Ans_category");

final String txtcf = bundle.getString("cfDisease");

//Get the textview controls
final TextView txtage = (TextView) findViewById(R.id.textView5);
final TextView txtbmi = (TextView) findViewById(R.id.textView7);
final TextView txtphy = (TextView) findViewById(R.id.textView6);

final TextView txtcategory = (TextView) findViewById(R.id.result);

//final TextView cf_result = (TextView) findViewById(R.id.result);

//Button buttonSend = (Button) findViewById(R.id.btnsend);

//Set the text values of the text controls
txtage.setText(umur);
txtbmi.setText(textBMI);
txtphy.setText(aktiviti);

txtcategory.setText(textCategory);

//cf_result.setText(txtcf);

Button btnsubmit = (Button) findViewById(R.id.btnsubmit);
//Intent intentsubmit = new Intent();

//intentsubmit.setClass(Review.this, Result.class);
btnsubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//startActivity(intentsubmit);

Integer calories;
double weight1, weight2, weight3;
double newWeight;

// AGE 41 - 45
if (bundle.getString("Ans_age").equals(R.string.age1)) // 41 - 45
{
weight1 = 0.7;
if (bundle.getString("Ans_phy").equals(R.string.moderate)) //Sedentary Active
{
weight2 = 0.8;
if ((txtcategory.equals("Underweight"))) //Underweight
{
weight3 = 0.6;
newWeight = 0.8 * weight2;
calories = 1600;

String cf = Double.toString(newWeight);
String cal = Integer.toString(calories);


Intent intent = new Intent(Review.this, Result.class);
Bundle bundle = new Bundle();

bundle.putString("Ans_calories", cal);
bundle.putString("cfCalories", cf);

intent.putExtras(bundle);
startActivity(intent);
}
}
}
else
if (bundle.getString("Ans_age").equals(R.string.age2)) // 46 - 50
{
weight1 = 0.7;
if (bundle.getString("Ans_phy").equals(R.string.active)) // Moderately Active
{
weight2 = 0.7;
if ((txtcategory.equals("Normal"))) // Underweight
{
weight3 = 0.8;
newWeight = 0.9 * weight1;
calories = 1800;

String cf = Double.toString(newWeight);
String cal = Integer.toString(calories);


Intent intent = new Intent(Review.this, Result.class);
Bundle bundle = new Bundle();

bundle.putString("Ans_calories", cal);
bundle.putString("cfCalories", cf);

intent.putExtras(bundle);
startActivity(intent);
}
}
}
else
if (bundle.getString("Ans_age").equals(R.string.age2)) // 41 - 45
{
weight1 = 0.8;
if (bundle.getString("Ans_phy").equals(R.string.moderate)) // Active
{
weight2 = 0.8;
if ((txtcategory.equals("Obese"))) // Underweight
{
weight3 = 0.8;
newWeight = 0.9 * weight3;
calories = 2000;

String cf = Double.toString(newWeight);
String cal = Integer.toString(calories);


Intent intent = new Intent(Review.this, Result.class);
Bundle bundle = new Bundle();

bundle.putString("Ans_calories", cal);
bundle.putString("cfCalories", cf);

intent.putExtras(bundle);
startActivity(intent);
}
}
}

}
});
}

结果.java

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

Intent intent = getIntent();
final String cal = intent.getStringExtra("Ans_calories");
final String cf = intent.getStringExtra("cfCalories");

final TextView kalori = (TextView) findViewById(R.id.show_calories);
final TextView keputusan = (TextView) findViewById(R.id.show_result);

kalori.setText(cal);
keputusan.setText(cf);
}

最佳答案

您可以执行以下操作以从 Intent 获取数据:

Bundle extras = getIntent().getExtras();
if (extras != null) {
String yourStirng = extras.getString("yourKeyString");
}

当尝试将数据用于 Intent 时,您也可以这样做:

 Intent intent = new Intent(getActivity() / this ,YourActivity.class);
intent.putExtra("YOURKEyString", "Your value string");
startActivity(intent);

试试这个,希望对你有帮助

关于java - 我该如何解决这个 "Method invocation ' bundle.getString ("Ans_age").equals(R.string.age2 )' may produce ' java.lang.NullPointerException'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34894555/

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