我一直试图写一个简单的BMI计算器作为我的第一个应用程序。我使用过时的教程这样做并不是第一个运行我的应用程序时遇到问题(当我尝试运行它时它就会退出),但是其他人出汗的东西似乎并不适合我。我不知道出了什么问题,所以请帮我解决这个问题并指出错误所以我不会重蹈覆辙。
这是我的代码:
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int cm,kg,bmi,cm2;
Button wsub10,wsub1,wadd1,wadd10,hsub10,hsub1,hadd1,hadd10,calcbmi;
TextView displaybmi,h,w;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cm = 170;
cm2 = 0;
kg = 60;
bmi = 0;
displaybmi = (TextView) findViewById(R.id.TextView04);
h = (TextView) findViewById(R.id.TextView01);
w = (TextView) findViewById(R.id.TextView03);
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/lucida.ttf");
//set typeface for all text
TextView tv = (TextView) findViewById(R.id.textView00);
tv.setTypeface(tf);
TextView tv1 = (TextView) findViewById(R.id.TextView01);
tv1.setTypeface(tf);
TextView tv2 = (TextView) findViewById(R.id.TextView02);
tv2.setTypeface(tf);
TextView tv3 = (TextView) findViewById(R.id.TextView03);
tv3.setTypeface(tf);
TextView tv4 = (TextView) findViewById(R.id.TextView04);
tv4.setTypeface(tf);
TextView tv5 = (TextView) findViewById(R.id.TextView05);
tv5.setTypeface(tf);
TextView tv6 = (TextView) findViewById(R.id.TextView06);
tv6.setTypeface(tf);
TextView tv7 = (TextView) findViewById(R.id.TextView07);
tv7.setTypeface(tf);
TextView tv8 = (TextView) findViewById(R.id.TextView08);
tv8.setTypeface(tf);
TextView tv9 = (TextView) findViewById(R.id.TextView09);
tv9.setTypeface(tf);
TextView tv10 = (TextView) findViewById(R.id.TextView10);
tv10.setTypeface(tf);
TextView tv11 = (TextView) findViewById(R.id.TextView11);
tv11.setTypeface(tf);
TextView tv12 = (TextView) findViewById(R.id.TextView12);
tv12.setTypeface(tf);
//introduce buttons and what they do
//weight buttons
wsub10 = (Button) findViewById(R.id.ImageView07);
wsub10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
kg -= 10;
w.setText("" + kg);
}
});
wsub1 = (Button) findViewById(R.id.ImageView08);
wsub1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
kg -= 1;
w.setText("" + kg);
}
});
wadd1 = (Button) findViewById(R.id.ImageView09);
wadd1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
kg += 1;
w.setText("" + kg);
}
});
wadd10 = (Button) findViewById(R.id.ImageView10);
wadd10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
kg += 10;
w.setText("" + kg);
}
});
//height buttons
hsub10 = (Button) findViewById(R.id.imageView2);
hsub10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cm -= 10;
h.setText("" + cm);
}
});
hsub1 = (Button) findViewById(R.id.ImageView02);
hsub1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cm -= 1;
w.setText("" + cm);
}
});
hadd1 = (Button) findViewById(R.id.ImageView03);
hadd1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cm += 1;
w.setText("" + cm);
}
});
hadd10 = (Button) findViewById(R.id.ImageView04);
hadd10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cm += 10;
w.setText("" + cm);
}
});
//introduce button for bmi kg/cm^2
calcbmi = (Button) findViewById(R.id.imageView3);
calcbmi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cm2 = cm * cm;
bmi = kg/cm2;
displaybmi.setText("Your BMI is " + bmi);
}
});
}
}
答案 0 :(得分:1)
一个错误是您正在尝试将ImageView转换为按钮
wsub10 = (Button) findViewById(R.id.ImageView07);
应该是
wsub10 = (ImageView) findViewById(R.id.ImageView07);
答案 1 :(得分:0)
您没有提供堆栈跟踪, LET ME GUESS
我尝试解决您的问题:
您将ImageView
转换为Button
,但在View
Heirarchy中,TextView
是Button
的父级ImageView
而View
1}}的父母是{{1}}。
查看 - > TextView - >按钮
<强>同时强>
查看 - &gt; ImageView的