Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android应用程序在点击按钮时崩溃_Android_Xml_Onclicklistener_Android Appcompat_Android Button - Fatal编程技术网

Android应用程序在点击按钮时崩溃

Android应用程序在点击按钮时崩溃,android,xml,onclicklistener,android-appcompat,android-button,Android,Xml,Onclicklistener,Android Appcompat,Android Button,我正在使用按钮从一个活动切换到另一个活动。在同一活动中,浮动操作按钮工作正常,但正常按钮导致异常。我提供与按钮相关的java类、xml文件和logcat IndexPage.java public class IndexPage extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我正在使用按钮从一个活动切换到另一个活动。在同一活动中,浮动操作按钮工作正常,但正常按钮导致异常。我提供与按钮相关的java类、xml文件和logcat

IndexPage.java

public class IndexPage extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_index_page);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           Intent in = new Intent(IndexPage.this, FoodEntry.class);
            startActivity(in);

        }
    });

    Button b = (Button)findViewById(R.id.buttonBackUp);


        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(IndexPage.this, Form.class);
                startActivity(intent);
            }
        });

  }

}
activity_index_page.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.praval.healthfreak.IndexPage">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />


</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_index_page" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/newfloatnew" />
</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.praval.healthfreak.IndexPage"
tools:showIn="@layout/activity_index_page">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/buttonBackUp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>
(编辑)-->(新版本符合您的所有建议,但我仍然卡住了)

Form.java

public class Form extends AppCompatActivity {

String g="m";
int a=0,w=0,f=0,i=0;
double val , bmi;
int cal=0;

TextView tBmi = (TextView)findViewById(R.id.textResult);
TextView tFitOrNot = (TextView)findViewById(R.id.textFitOrNot);
TextView tCalorie = (TextView)findViewById(R.id.textCalorie);

EditText gender = (EditText)findViewById(R.id.editGender);
EditText age = (EditText)findViewById(R.id.editAge);
EditText weight = (EditText)findViewById(R.id.editWeight);
EditText feet = (EditText)findViewById(R.id.editFeet);
EditText inch = (EditText)findViewById(R.id.editInch);

static String s1="underweight";
static String s2="fit";
static String s3="overweight";
static String s4="obese";

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

    Button bCalculate = (Button)findViewById(R.id.buttonBMI);
    bCalculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            calculation();
        }
    });


}

public void calculation() {

   /* gender.setText("m");
    age.setText("m");
    weight.setText("m");
    feet.setText("m");
    inch.setText("m"); */

    g = gender.getText().toString();
    a = Integer.parseInt(age.getText().toString());
    w = Integer.parseInt(weight.getText().toString());
    f = Integer.parseInt(feet.getText().toString());
    i = Integer.parseInt(inch.getText().toString());
    //feet and inches converted to metre
    val=((f*12)+i)*0.245;
    bmi=w*100/(val*val);

    tBmi.setText(String.valueOf(bmi));

    if (bmi<18.5)
    {
        tFitOrNot.setText(s1);
        if(g.equals("m"))
        {
            if(a>=18 && a<=25)
            {
                cal=3200;
               // tCalorie.setText(s5);
            }
            if(a>25 && a<=40)
            {
                cal=3400;
                //tCalorie.setText(s6);
            }
            if(a>40 && a<=60)
            {
                cal=3400;
              //  tCalorie.setText(s6);
            }


        }
        else
        {
            if(a>=18 && a<=25)
            {
                cal=3100;
                //tCalorie.setText(s7);
            }
            if(a>25 && a<=40)
            {
                cal=2900;
                //tCalorie.setText(s8);
            }
            if(a>40 && a<=60)
            {
                cal=2900;
             //   tCalorie.setText(s8);
            }

        }
    }
    else if(bmi>=18.5 && bmi<=24.9)
    {
        tFitOrNot.setText(s2);
        if(g.equals("m"))
        {
            if(a>=18 && a<=25)
            {
                cal=3000;
               // tCalorie.setText(s9);
            }
            if(a>25 && a<=40)
            {
                cal=2800;
             //   tCalorie.setText(s12);
            }
            if(a>40 && a<=60)
            {
                cal=2700;
               // tCalorie.setText(s10);
            }


        }
        else
        {
            if(a>=18 && a<=25)
            {
                cal=2700;
                //tCalorie.setText(s10);
            }
            if(a>25 && a<=40)
            {
                cal=2400;
                //tCalorie.setText(s14);
            }
            if(a>40 && a<=60)
            {
                cal=2000;
                //tCalorie.setText(s13);
            }

        }
    }
    else if(bmi>=25 && bmi<=29.9)
    {
        tFitOrNot.setText(s3);
        if(g.equals("m"))
        {
            if(a>=18 && a<=25)
            {
                cal=2200;
                //tCalorie.setText(s11);
            }
            if(a>25 && a<=40)
            {
                cal=2200;
               // tCalorie.setText(s11);
            }
            if(a>40 && a<=60)
            {
                cal=1200;
                //tCalorie.setText(s16);
            }


        }
        else
        {
            if(a>=18 && a<=25)
            {
                cal=1200;
                //tCalorie.setText(s16);
            }
            if(a>25 && a<=40)
            {
                cal=1500;
               // tCalorie.setText(s15);
            }
            if(a>40 && a<=60)
            {
                cal= 1300;
               // tCalorie.setText(s19);
            }

        }
    }
    else
    {
        tFitOrNot.setText(s4);
        if(g.equals("m"))
        {
            if(a>=18 && a<=25)
            {
                cal=2000;
                //tCalorie.setText(s13);
            }
            if(a>25 && a<=40)
            {
                cal=2500;
                //tCalorie.setText(s18);
            }
            if(a>40 && a<=60)
            {
                cal=2050;
                //tCalorie.setText(s17);
            }


        }
        else
        {
            if(a>=18 && a<=25)
            {
                cal=1000;
                //tCalorie.setText(s20);
            }
            if(a>25 && a<=40)
            {
                cal=1300;
                //tCalorie.setText(s19);
            }
            if(a>40 && a<=60)
            {
                cal=1200;
                //tCalorie.setText(s16);
            }

        }
    }
    tCalorie.setText(String.valueOf(cal));
}
}
公共类表单扩展了AppCompatActivity{
字符串g=“m”;
int a=0,w=0,f=0,i=0;
双val,bmi;
int-cal=0;
TextView tBmi=(TextView)findViewById(R.id.textResult);
TextView tFitOrNot=(TextView)findViewById(R.id.textFitOrNot);
TextView tCalorie=(TextView)findViewById(R.id.textCalorie);
EditText性别=(EditText)findViewById(R.id.editGender);
EditText年龄=(EditText)findViewById(R.id.editAge);
EditText权重=(EditText)findViewById(R.id.editWeight);
EditText英尺=(EditText)findViewById(R.id.EditFoots);
EditText英寸=(EditText)findViewById(R.id.editInch);
静态字符串s1=“重量不足”;
静态字符串s2=“适合”;
静态字符串s3=“超重”;
静态字符串s4=“肥胖”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_表单);
按钮b计算=(按钮)findViewById(R.id.buttonBMI);
b计算.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
计算();
}
});
}
公共空间计算(){
/*性别:setText(“m”);
年龄。setText(“m”);
重量(m);
英尺。塞特克斯(“m”);
英寸.setText(“m”)*/
g=gender.getText().toString();
a=Integer.parseInt(age.getText().toString());
w=Integer.parseInt(weight.getText().toString());
f=Integer.parseInt(feet.getText().toString());
i=Integer.parseInt(inch.getText().toString());
//英尺和英寸换算成米
val=((f*12)+i)*0.245;
体重指数=w*100/(val*val);
tBmi.setText(String.valueOf(bmi));
如果(bmi=18&&a25&&a40&&a=18&&a25&&a40&&a=18.5&&bmi=18&&a25&&a40&&a=18&&a25&&a40&&a=25&&bmi=18&&a25&&a40&&a=18&&a25&&a40&&a=18&&a25&&a40&&a=18&&a25&&a40&&a=18&&a25&&a40&&a=18&&a25&&a40&&a40&&a=18&&a在editText中输入值

您正在传递
以获取
整数
对象。这将导致
异常

在这里:

将您的计算从创建时的
onCreate
中删除,因为如果您没有将任何内容设置为
EditText
,它将始终抛出
exception
。例如,您可以通过单击按钮将其放入

并尝试将解析放在
try
catch
块中,以防止
运行时异常

例:


将这些行移到按钮单击或其他内容中。因为当您的活动运行时,您的编辑文本为空,并在写入
age.getText()时返回“”,而toString()
。该“”将传递给
Integer.parseInt()
生成错误的函数。在调用
Integer.parseInt()
函数之前,请确保您的EditText不是空的,因为您的所有EditText都是空的,并且您的代码基本上会在这一点上抛出错误,所以当您启动表单活动时,您的应用程序会崩溃

Integer.parseInt("");
要解决这个问题,您可能应该在表单布局XML中添加一个按钮,并将计算值的大部分代码移动到onClick方法中


我还建议将逻辑分解为一些方法,而不是将所有内容都放在onCreate中,因为那些if语句看起来像是许多重复的代码

错误出现在Form.java类上。发布该类后,请使用浮动按钮调用
FoodEntry
活动,并使用普通按钮调用
Form
活动按钮。错误显然出现在
表单
活动添加
表单
类中,错误出现在
活动
中。添加了Form.java类您正在使用双精度int..val=((f*12)+i)*0.245;此行导致错误这是一个表单,因此我无法使用按钮单击。我将尝试处理您的建议并尽快答复您。如何在调用parseInt()之前为所有EditText赋值?我已经编辑了Form.java,但我仍然被困在@Adeel ShahzadYou只需在EditText中输入值。请确保输入的值是数字,否则您的应用程序将再次崩溃。我建议在每个要转换为整数的EditText上放置“android:inputType=number”,以避免再次崩溃您的应用程序。完成(给出问题的屏幕截图)但同样的问题是,在单击按钮时移动了所有计算,请告诉我如何在调用Integer.ParseInt()之前为所有EditText赋值;您键入它们,然后单击按钮将
findViewById
的所有用法移动到onCreate方法中。@RohitArya我已经编辑了Form.java,但仍然是stuck@PravalSharma,如我的回答所示,将其置于try
catch
块下。@Rohitary将其置于try-catch块后,再次出现相同的问题。
a = Integer.parseInt(age.getText().toString());
w = Integer.parseInt(weight.getText().toString());
f = Integer.parseInt(feet.getText().toString());
i = Integer.parseInt(inch.getText().toString());
try{
  a = Integer.parseInt(age.getText().toString());
  // similarly others.
}catch(NumberFormatException e){

}
a = Integer.parseInt(age.getText().toString()); 
w = Integer.parseInt(weight.getText().toString()); 
f = Integer.parseInt(feet.getText().toString()); 
i = Integer.parseInt(inch.getText().toString()); 
Integer.parseInt("");