我的项目有些问题。当我通过虚拟设备运行我的项目时,AS发布了一个错误(如下图所示): Image here
这个问题的解决方案是什么(我想当我尝试创建第二个布局代码时会发生这种情况))
public class MainActivity extends Activity implements OnClickListener {
EditText bmisat;
TextView txtstat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnView =(Button) findViewById(R.id.ButtonView);
btnView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent scr=new Intent(MainActivity.this,Main2Activity.class);
startActivity(scr);
}
});
Button CalculateBMI_Btn = (Button) findViewById(R.id.CalculateBMI);
CalculateBMI_Btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
try{
EditText edtWeight = (EditText) findViewById(R.id.WeightInputEdt);
EditText edtHeight = (EditText) findViewById(R.id.HeightInputEdt);
txtstat=(TextView) findViewById(R.id.release_bmi);
String sWeigth = edtWeight.getText().toString();
String sHeigth = edtHeight.getText().toString();
float fWeight = Float.valueOf(sWeigth);
float fHeight = Float.valueOf(sHeigth);
final float fBMI = fWeight / (fHeight*fHeight);
EditText edtBMIEdt = (EditText) findViewById(R.id.BMIEdt);
bmisat=(EditText) findViewById(R.id.bmi_bar);
edtBMIEdt.setText(String.valueOf(fBMI));
Getstat(fBMI,0f,15f,"Thiếu cân rất nặng");
Getstat(fBMI,15f,16f,"Thiếu cân nặng");
Getstat(fBMI,16f,18.5f,"Thiếu cân");
Getstat(fBMI,18.5f,25f,"Bình thường");
Getstat(fBMI,25f,30f,"Tiền beó phì");
Getstat(fBMI,30f,35f,"Beó phì độ I");
Getstat(fBMI,35f,40f,"Béo phì độ II");
Getstat(fBMI,40f,1000f,"Beó phì độ III");
DB=this.openOrCreateDatabase(DB_NAME,MODE_PRIVATE,null);
DB.execSQL("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+"(BMI_num REAL);");
ContentValues ct=new ContentValues();
ct.put("BMI_num",fBMI);
DB.insert(TABLE_NAME,null,ct);
// DB.delete(TABLE_NAME,null,null);
Cursor c=DB.rawQuery("SELECT BMI_num FROM "+TABLE_NAME,null) ;
if (c.moveToFirst()){
do {
float code=c.getFloat(c.getColumnIndex("BMI_num"));
Toast.makeText(MainActivity.this,code+"",Toast.LENGTH_LONG).show();
} while (c.moveToNext());
}
}catch(Exception e ){
Toast.makeText(v.getContext(), "Error during calculating BMI"+e.toString(), 1000);
}
}
private void Getstat(float intial,float a,float b,String str)
{
if ((intial>=a) && (intial<b))
{
bmisat.setText(str);
}
}
} 我在项目中添加了以下2个代码
私人决赛String TABLE_NAME =&#34; BMI_TABLE&#34 ;; private final String DB_NAME =&#34; Database&#34 ;; SQLiteDatabase DB = null;