【问题标题】:My android application crashes when i am trying to make objects invisble/visible - eclipse当我试图使对象不可见/可见时,我的 android 应用程序崩溃 - eclipse
【发布时间】:2015-03-13 12:07:00
【问题描述】:

我正在尝试使对象(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();
    }

}

}

【问题讨论】:

  • 所有变量都已初始化,请显示初始化并发布您的 LOGCAT!
  • 我怎样才能发布一个LOGCAT,因为如果我只是粘贴,它看起来很不舒服,非常感谢您的帮助
  • 您可以发布它,标记它并在发布窗口点击{}标志
  • 可以吗?抱歉无知
  • 如何搜索特定行?

标签: java android eclipse crash


【解决方案1】:

首先我希望你的变量是 int 所以初始化它 woth wv=0。错误是“Caused by: java.lang.NullPointerException”意味着你的变量不能从你传递的地方得到任何东西 放置调试点在所有 wv 变量或把你的这个try-catch 中的代码,所以你会得到错误。

【讨论】:

  • 我查了,问题不在于共享偏好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
相关资源
最近更新 更多