单击“选项菜单”中的项目时,应用程序崩溃

时间:2014-03-29 18:28:29

标签: java android android-optionsmenu oncreateoptionsmenu

enter image description here我正在开发一个名为'MyBMI'的Android应用程序。我在选项菜单中创建了一个名为“关于MyBMI”的项目,单击该项目会将用户带到另一个名为“AboutActivity”的活动。但是,当我在选项菜单中单击“关于MyBMI”时,我的应用程序崩溃了。请帮忙。

package com.mavenmaverick.mybmi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class MainActivity extends Activity {

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


    final EditText entermass = (EditText) findViewById(R.id.entermass);
    final EditText enterheight = (EditText) findViewById(R.id.enterheight);
    entermass.requestFocus();

    Button BMI = (Button) findViewById(R.id.BMI);
    BMI.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(TextUtils.isEmpty(entermass.getText().toString()) || TextUtils.isEmpty(enterheight.getText().toString()))
            {
                if(TextUtils.isEmpty(entermass.getText().toString()) || TextUtils.isEmpty(enterheight.getText().toString()))
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setIcon(R.drawable.ic_launcher);
                    builder.setTitle("Developer's Check");
                    builder.setMessage("Either Of The Text Fields Are Empty");

                    builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            dialog.cancel();

                        }

                    });

                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
                    }
            else {
            double d = 0;
            double mass = Double.valueOf(entermass.getText().toString());           
            double height = Double.valueOf(enterheight.getText().toString());
            double heightF;
            heightF=height*0.3048;
            double bmi = mass/(heightF*heightF);
            d=bmi;
            int display = (int) bmi;

            Toast toast=Toast.makeText(getApplicationContext(), "Your BMI is " +display, Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
            toast.show();   
                if(d<15.0)
                {
                    Toast.makeText(MainActivity.this,"You Are Very Severely Underweight", Toast.LENGTH_LONG).show();
                }   
                else if(d>=15.0 && d<=16.0){

                Toast.makeText(MainActivity.this,"You Are Severely Underweight", Toast.LENGTH_LONG).show();
                }
                else if(d>=15.0 && d<=16.0){

                Toast.makeText(MainActivity.this,"You Are Severely Underweight", Toast.LENGTH_LONG).show();
                }
            else if(d>=16.0 && d<=18.5){

                Toast.makeText(MainActivity.this,"You Are Severely Underweight", Toast.LENGTH_LONG).show();
                }
            else if(d>=18.5 && d<=25.0){

                Toast.makeText(MainActivity.this,"You Are Normal (healthy weight)", Toast.LENGTH_LONG).show();
                }
            else if(d>=25.0 && d<=30.0){

                Toast.makeText(MainActivity.this,"You Are Overweight", Toast.LENGTH_LONG).show();
                }
            else if(d>=30.0 && d<=35.0){

                Toast.makeText(MainActivity.this,"You Are Obese Class I (Moderately obese)", Toast.LENGTH_LONG).show();
                }
            else if(d>=35.0 && d<=40.0){

                Toast.makeText(MainActivity.this,"You Are Obese Class II (Severely obese)", Toast.LENGTH_LONG).show();
                }
            else if(d>40.0){

                Toast.makeText(MainActivity.this,"You Are Obese Class III (Very severely obese)", Toast.LENGTH_LONG).show();
                }
        }
        }   

    });

};
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    startActivity(new Intent(this, AboutActivity.class));
    return true;
}

};

2 个答案:

答案 0 :(得分:2)

只是一个猜测,但您是否已将此活动添加到清单?

好的,我觉得你的位图空间不足了。你需要开始回收你的位图!将会有一个名为.Recycle()的位图方法,您可以在更改活动之前执行该方法!这应该有希望解决你的问题!

或者你正在加载一个太大的位图..希望这会有所帮助!

答案 1 :(得分:0)

堆栈跟踪说:您的活动有一个RelativeLayout,背景可绘制太大而导致内存不足。使图像更小,使其消耗更少的内存。