gpt4 book ai didi

java - 当我尝试使对象不可见/可见时,我的android应用程序崩溃-Eclipse

转载 作者:行者123 更新时间:2023-12-03 17:47:10 25 4
gpt4 key购买 nike

我试图使对象(TextView,EditText,Button)不可见/可见,而当我这样做时,应用程序崩溃。这部分导致崩溃:
我添加了共享首选项

编辑:最后的问题是我没有初始化对象,我没有注意到,因为这只是其中的一部分

public class Nutrition extends Activity implements android.view.View.OnClickListener {

TextView weight_tv;
TextView hight_tv;
TextView age_tv;
EditText weight_ed;
EditText hight_ed;
EditText age_ed;
Button calculate_bt;

TextView recommendation_tv;
TextView calories_tv;
TextView protien_tv;
TextView carbohydrate_tv;
TextView fat_tv;
Button change_values_bt;

SharedPreferences sp;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nutrition);
calculate_bt = (Button)findViewById(R.id.calculate_bt);
calculate_bt.setOnClickListener(this);
change_values_bt = (Button)findViewById(R.id.change_values_bt);
change_values_bt.setOnClickListener(this);

weight_tv = (TextView)findViewById(R.id.weight_tv);
hight_tv = (TextView)findViewById(R.id.hight_tv);
age_tv = (TextView)findViewById(R.id.age_tv);
weight_ed = (EditText)findViewById(R.id.weight_ed);
hight_ed = (EditText)findViewById(R.id.hight_ed);
age_ed = (EditText)findViewById(R.id.age_ed);

recommendation_tv = (TextView)findViewById(R.id.recommendation_tv);
calories_tv = (TextView)findViewById(R.id.calories_tv);
protien_tv = (TextView)findViewById(R.id.protien_tv);
carbohydrate_tv = (TextView)findViewById(R.id.carbohydrate_tv);
fat_tv = (TextView)findViewById(R.id.fat_tv);

SharedPreferences prefs = getSharedPreferences("com.example.shreddedacademy", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();



int wv = prefs.getInt("weight", -1);


if(wv == -1)
show_calculate();
else
{
calculate();
show_change();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nutrition, menu);
return true;
}

public void show_calculate()
{
weight_ed.setText("");
hight_ed.setText("");
age_ed.setText("");

weight_tv.setVisibility(View.VISIBLE);
hight_tv.setVisibility(View.VISIBLE);
age_tv.setVisibility(View.VISIBLE);
weight_ed.setVisibility(View.VISIBLE);
hight_ed.setVisibility(View.VISIBLE);
age_ed.setVisibility(View.VISIBLE);
calculate_bt.setVisibility(View.VISIBLE);

recommendation_tv.setVisibility(View.INVISIBLE);
calories_tv.setVisibility(View.INVISIBLE);
protien_tv.setVisibility(View.INVISIBLE);
carbohydrate_tv.setVisibility(View.INVISIBLE);
fat_tv.setVisibility(View.INVISIBLE);
change_values_bt.setVisibility(View.INVISIBLE);
}

public void show_change()
{
weight_tv.setVisibility(View.INVISIBLE);
hight_tv.setVisibility(View.INVISIBLE);
age_tv.setVisibility(View.INVISIBLE);
weight_ed.setVisibility(View.INVISIBLE);
hight_ed.setVisibility(View.INVISIBLE);
age_ed.setVisibility(View.INVISIBLE);
calculate_bt.setVisibility(View.INVISIBLE);

recommendation_tv.setVisibility(View.VISIBLE);
calories_tv.setVisibility(View.VISIBLE);
protien_tv.setVisibility(View.VISIBLE);
carbohydrate_tv.setVisibility(View.VISIBLE);
fat_tv.setVisibility(View.VISIBLE);
change_values_bt.setVisibility(View.VISIBLE);
}

public void save()
{
SharedPreferences prefs = getSharedPreferences("com.example.shreddedacademy", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();

int weight = Integer.parseInt(weight_ed.getText().toString());
int hight = Integer.parseInt(hight_ed.getText().toString());
int age = Integer.parseInt(age_ed.getText().toString());

editor.putInt("weight", weight);
editor.putInt("hight", hight);
editor.putInt("age", age);

editor.commit();
}

public void calculate()
{
SharedPreferences prefs = getSharedPreferences("com.example.shreddedacademy", Context.MODE_PRIVATE);
//SharedPreferences.Editor editor = prefs.edit();

int weight = prefs.getInt("weight", -1);
int hight = prefs.getInt("hight", -1);
int age = prefs.getInt("age", -1);



int calories = (int) ((10 * weight) + (6.25 * hight) - (5 * age) + 5);
int rmr = (int) (calories * 1.2 + 400);
String text_calories = String.valueOf(rmr);
calories_tv.setText("Consume " + text_calories + " calories per day");

int protien = weight * 2;
String protien_text = String.valueOf(protien);
protien_tv.setText(protien_text + " grams of protien per day");

String fat_text = String.valueOf(weight);
fat_tv.setText(fat_text + " grams of fat per day");

int carbohydrate = (protien * 4 + weight * 9) / 4;
String carbohydrate_text = String.valueOf(carbohydrate);
carbohydrate_tv.setText(carbohydrate_text + " grams of carbohydrate per day");

show_change();
}

@Override
public void onClick(View v) {

if(v.getId() == calculate_bt.getId())
{

if(weight_ed.length() > 0 && hight_ed.length() > 0 && age_ed.length() > 0)
{
save();
calculate();
show_change();

}
}

if(v.getId() == change_values_bt.getId())
{
show_calculate();
}

}

}

最佳答案

首先,我希望您的变量为int,以便将其初始化为wv=0。错误为“Caused by: java.lang.NullPointerException”表示您的变量无法从传递的调试点上的所有wv变量处获取任何内容,或将此代码放入try-catch中,以便错误。

关于java - 当我尝试使对象不可见/可见时,我的android应用程序崩溃-Eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29031828/

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