我正在尝试使对象(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();
}
}
}
答案 0 :(得分:0)
首先,我希望你的变量是int,所以初始化它wv=0
。错误是“Caused by: java.lang.NullPointerException
”意味着你的变量无法从你传递调试点的地方得到任何东西{{1}变量或将此代码放在try-catch中,以便得到错误。