我试图创建一个BMI计算器,由于某种原因,每当我单击按钮时,应用程序就会崩溃。但是,在构建应用程序时,它不会报告任何问题。我认为buttonClick方法存在问题,因为Android Studio表示从未使用过它。我想构建该应用程序,以便在单击按钮后,它将在textview中显示您的BMI。
这是我的Java文件:
package com.example.amitkulkarni.firstandroidapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ActivityExample extends AppCompatActivity implements View.OnClickListener {
Button button;
TextView textView;
String display;
EditText userWeight, userHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
userWeight = (EditText) findViewById(R.id.userWeight);
userHeight = (EditText) findViewById(R.id.userHeight);
display = "Calculator";
}
public void buttonClick(View view){
int weight = Integer.parseInt(userWeight.getText().toString());
int height = Integer.parseInt(userHeight.getText().toString());
if(((703*weight)/(height * height))<18.5) {
textView.setText("Underweight" + ", " + "BMI: " + Integer.toString( (703*weight)/((height * height)) ));
}
else if(((703*weight)/(height * height)) <= 24.9) {
textView.setText("Normal Weight" + ", " + "BMI: " + Integer.toString( (703*weight)/((height * height)) ));
}
else if(((703*weight)/(height * height)) <= 29.9) {
textView.setText("Overweight"+ ", " + "BMI: " + Integer.toString( (703*weight)/((height * height)) ));
}
else {
textView.setText("Obese"+ ", " + "BMI: " + Integer.toString( (703*weight)/((height * height)) ));
}
// BMI Formula [703 * weight (lb)]/[height^2 (in)]
}
@Override
public void onClick(View view) {
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}
这是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_example"
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"
android:background="#fff"
tools:context="com.example.amitkulkarni.firstandroidapp.ActivityExample">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:ems="10"
android:id="@+id/userWeight"
tools:ignore="LabelFor"
android:hint="Enter Weight (pounds)"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/userHeight"
android:selectAllOnFocus="true"
tools:ignore="TextFields"
android:hint="Enter Height (inches)"
android:layout_below="@+id/userWeight"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/userHeight"
android:layout_marginStart="16dp"
android:layout_marginTop="18dp"
android:onClick="buttonClick(ActivityExample)"
android:text="@string/calculate" />
<TextView
android:layout_width="200dp"
android:layout_height="120dp"
android:id="@+id/textView"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:layout_marginEnd="11dp"
android:textSize="20sp"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_alignParentEnd="true"
android:textStyle="normal|bold" />
</RelativeLayout>
LogCat文件:
07-19 19:31:20.011 8395-8395/? I/firstandroidap: Not late-enabling -Xcheck:jni (already on)
07-19 19:31:20.143 8395-8395/? W/firstandroidap: Unexpected CPU variant for X86 using defaults: x86
07-19 19:31:20.240 8395-8395/? I/firstandroidap: The ClassLoaderContext is a special shared library.
07-19 19:31:20.355 8395-8395/? W/firstandroidap: JIT profile information will not be recorded: profile file does not exits.
07-19 19:31:20.360 8395-8395/? I/chatty: uid=10083(com.example.amitkulkarni.firstandroidapp) identical 10 lines
07-19 19:31:20.360 8395-8395/? W/firstandroidap: JIT profile information will not be recorded: profile file does not exits.
07-19 19:31:20.385 8395-8395/? I/InstantRun: starting instant run server: is main process
07-19 19:31:20.506 8395-8395/? W/firstandroidap: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
07-19 19:31:20.605 8395-8395/com.example.amitkulkarni.firstandroidapp D/OpenGLRenderer: Skia GL Pipeline
07-19 19:31:20.682 8395-8420/com.example.amitkulkarni.firstandroidapp I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
07-19 19:31:20.682 8395-8420/com.example.amitkulkarni.firstandroidapp I/OpenGLRenderer: Initialized EGL, version 1.4
07-19 19:31:20.682 8395-8420/com.example.amitkulkarni.firstandroidapp D/OpenGLRenderer: Swap behavior 1
07-19 19:31:20.683 8395-8420/com.example.amitkulkarni.firstandroidapp W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
07-19 19:31:20.683 8395-8420/com.example.amitkulkarni.firstandroidapp D/OpenGLRenderer: Swap behavior 0
07-19 19:31:20.702 8395-8420/com.example.amitkulkarni.firstandroidapp D/EGL_emulation: eglCreateContext: 0xeb5de100: maj 3 min 0 rcv 3
07-19 19:31:20.818 8395-8420/com.example.amitkulkarni.firstandroidapp D/EGL_emulation: eglMakeCurrent: 0xeb5de100: ver 3 0 (tinfo 0xef57ee20)
07-19 19:31:20.946 8395-8420/com.example.amitkulkarni.firstandroidapp D/EGL_emulation: eglMakeCurrent: 0xeb5de100: ver 3 0 (tinfo 0xef57ee20)
07-19 19:31:21.032 8395-8395/com.example.amitkulkarni.firstandroidapp I/AssistStructure: Flattened final assist data: 3364 bytes, containing 1 windows, 11 views
07-19 19:31:22.780 8395-8395/com.example.amitkulkarni.firstandroidapp V/StudioProfiler: StudioProfilers agent attached.
07-19 19:31:22.823 8395-8463/com.example.amitkulkarni.firstandroidapp V/StudioProfiler: Acquiring Application for Events
07-19 19:31:22.914 8395-8395/com.example.amitkulkarni.firstandroidapp V/StudioProfiler: Transformed class: java/net/URL
07-19 19:31:22.915 8395-8395/com.example.amitkulkarni.firstandroidapp W/firstandroidap: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
07-19 19:31:23.284 8395-8395/com.example.amitkulkarni.firstandroidapp V/StudioProfiler: Memory control stream started.
07-19 19:31:23.370 8395-8395/com.example.amitkulkarni.firstandroidapp I/AssistStructure: Flattened final assist data: 3420 bytes, containing 1 windows, 11 views
07-19 19:31:23.808 8395-8469/com.example.amitkulkarni.firstandroidapp V/StudioProfiler: Live memory tracking disabled.
07-19 19:31:23.813 8395-8469/com.example.amitkulkarni.firstandroidapp V/StudioProfiler: Live memory tracking enabled.
JNIEnv not attached
07-19 19:31:23.846 8395-8469/com.example.amitkulkarni.firstandroidapp V/StudioProfiler: Loaded classes: 11041
07-19 19:31:24.072 8395-8469/com.example.amitkulkarni.firstandroidapp V/StudioProfiler: Tracking initialization took: 259509307ns
答案 0 :(得分:1)
在您的xml中,更改按钮内的这一行
android:onClick = "buttonClick"
答案 1 :(得分:0)
致电
void buttonClick(查看视图)
在
void onClick(查看视图)
和setOnClickListener像这样:
package com.example.amitkulkarni.firstandroidapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ActivityExample extends AppCompatActivity implements View.OnClickListener {
Button button;
TextView textView;
String display;
EditText userWeight, userHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
textView = (TextView) findViewById(R.id.textView);
userWeight = (EditText) findViewById(R.id.userWeight);
userHeight = (EditText) findViewById(R.id.userHeight);
display = "Calculator";
}
public void buttonClick(View view){
int weight = Integer.parseInt(userWeight.getText().toString());
int height = Integer.parseInt(userHeight.getText().toString());
if(((703*weight)/(height * height))<18.5) {
textView.setText("Underweight" + ", " + "BMI: " + Integer.toString( (703*weight)/((height * height)) ));
}
else if(((703*weight)/(height * height)) <= 24.9) {
textView.setText("Normal Weight" + ", " + "BMI: " + Integer.toString( (703*weight)/((height * height)) ));
}
else if(((703*weight)/(height * height)) <= 29.9) {
textView.setText("Overweight"+ ", " + "BMI: " + Integer.toString( (703*weight)/((height * height)) ));
}
else {
textView.setText("Obese"+ ", " + "BMI: " + Integer.toString( (703*weight)/((height * height)) ));
}
// BMI Formula [703 * weight (lb)]/[height^2 (in)]
}
@Override
public void onClick(View view) {
buttonClick(view)
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}