当前位置: 首页 > 知识库问答 >
问题:

IllegalStateException:无法用Android执行活动的方法

海典
2023-03-14

我是一个相当新的Android程序员,在完成了几个教程后,我开始扩展我完成的BMI计算器教程。在此过程中,我需要删除并重新安装Eclipse。我还不得不切换工作区。由于我的错误,我只能保留应用程序的。java文件。我重新创建了。xml和清单,直到现在,当我尝试单击calculate按钮时,才得到了IllegalStateException。在切换和重新创建之前,一切都很好。我正在使用Nexus Galaxy的仿真程序,运行4.2.2代码如下:

包com.example.bmicalculator;

导入Android.app.activity;导入Android.os.bundle;导入Android.view.menu;导入Android.view.view;导入Android.widget.EditText;导入Android.widget.ImageView;导入Android.widget.TextView;

公共类MainActivity扩展活动{ImageDownloader Downloader;

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

}

public void calculateClickHandler(View view) {
  if(view.getId() == R.id.calculateButton) {
      EditText weightText = (EditText)findViewById(R.id.weightLabel);
      EditText heightText = (EditText)findViewById(R.id.heightText);
      TextView resultText = (TextView)findViewById(R.id.resultLabel);
      TextView suggestText= (TextView)findViewById(R.id.suggestedFix);
      ImageView image = (ImageView)findViewById(R.id.relatedPicture);
      float weight = Float.parseFloat(weightText.getText().toString());
      float height = Float.parseFloat(heightText.getText().toString());

      float bmiValue = calculateBMI(weight, height);

      String bmiInterpretation = interpretBMI(bmiValue);

      String suggest = "We suggest " + interpretBMIInterpretation(bmiInterpretation);

      resultText.setText(bmiValue + "-" + bmiInterpretation);
      suggestText.setText(suggest);
      setUpImage(image, bmiInterpretation);
  }
}

private float calculateBMI(float weight, float height) {
  return (float) (weight * 4.88 / (height * height));
}

private String interpretBMI(float bmiValue) {
  if(bmiValue < 16) {
      return "Severely underweight";
  } else if (bmiValue < 18.5) {
      return "Underweight";
  } else if(bmiValue < 25) {
      return "Normal";
  }
  else if(bmiValue < 30) {
      return "Overweight";
  }
  else {
      return "Obese";
  }
}
@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;
}

private String interpretBMIInterpretation(String interp) {
  if(interp.equals("Severely underweight")) {
      return "eating more.  You can still work out and stay thin, but you need more weight.";
  }
  else if(interp.equals("Underweight")) {
      return "eating a little more, alongside working out to gain muscle weight.";
  }
  else if(interp.equals("Normal")) {
      return "continuing to do whatever it is you're doing.";
  }
  else if(interp.equals("Overweight")) {
      return "working out a little.  Lose a few fat poinds and put on some muscle pounds, and you'll be a healthy weight.";
  }
  else {
      return "cutting back on your food intake and working out.  A little goes a long way.";
  }
}

private void setUpImage(ImageView image, String interp) {
  if(interp.equals("Severely underweight")) {
      image.setVisibility(View.INVISIBLE);
      downloader = new ImageDownloader(image);
      String url = "http://www.wendys.com/cs/Satellite?blobcol=urldata&blobheader=image%2Fpng&blobkey=id&blobtable=MungoBlobs&blobwhere=1365660287009&ssbinary=true";
      downloader.execute(url);
  }
  else if(interp.equals("Underweight")) {
      image.setVisibility(View.INVISIBLE);
      downloader = new ImageDownloader(image);
      String url = "http://www.livestrongfitness.com/blog/wp-content/uploads/barbell.jpg";
      downloader.execute(url);
  }
  else if(interp.equals("Normal")) {
      image.setVisibility(View.INVISIBLE);
      downloader = new ImageDownloader(image);
      String url = "http://primary3.tv/blog/wp-content/uploads/2011/03/thumbsup.jpg";
      downloader.execute(url);
  }
  else if(interp.equals("Overweight")) {
      image.setVisibility(View.INVISIBLE);
      downloader = new ImageDownloader(image);
      String url = "http://www.livestrongfitness.com/blog/wp-content/uploads/barbell.jpg";
      downloader.execute(url);
  }
  else {
      image.setVisibility(View.INVISIBLE);
      downloader = new ImageDownloader(image);
      String url = "http://wisefitnesstips.com/wp-content/uploads/2013/02/salad-nutrients-facts.jpg";
      downloader.execute(url);
  }
}
 }

和ImageDownload类:

protected void onProgressUpdate(Integer... progress) {

}

protected void onPostExecute(Bitmap result) {
  image.setImageBitmap(result);
  image.setVisibility(View.VISIBLE);
} }

KeyEvent{action=action_up,keycode=keycode_tab,ScanCode=15,转移瘤=0,flags=0x8,RepeatCount=0,事件时间=922301,停机时间=922168,DeviceID=0,来源=0x101}05-23 14:47:55.097:D/AndroidRuntime(1158):正在关闭VM 05-23 14:47:55.097:W/Dalvikvm(1158):Threadid=1:线程因未捕获异常而退出(group=0x40A71930)05-23 14:47:55.187:e/AndroidRuntime(1158):致命异常:main 05-23 14:47:55.187:e/AndroidRuntime(1158):java.lang.IllegalStateException:无法执行活动的方法05-23 14:47:55.187:e/AndroidRuntime(1158):adandroid.view.view$1.onclick(View.java:3599)05-23:47:55.187:e/AndroidRuntime(1158):at Android.view.view$performClick.run(view.java:17355)05-23 14:47:55.187:e/AndroidRuntime(1158):at Android.os.handler.handleCallback(handler.java:725)05-23 14:47:55.187:e/AndroidRuntime(1158):at Android.os.handler.dispatchMessage(handler.java:92)05-23 14:47:55.187:e/AndroidRuntime(1158):at)05-23 14:47:55.187:e/AndroidRuntime(1158):at android.app.activitythread.main(activitythread.java:5041)05-23 14:47:55.187:e/AndroidRuntime(1158):at java.lang.reflect.method.invokenative(本机方法)05-23 14:47:55.187:e/androidruntime(1158):at java.lang.reflect.Method.invoke(Method.java:511)05-23 14:47:55.187:e/androidruntime(1158):at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793)05-23 14:47:55.187:e/androidruntime(1158):at AndroidRuntime(1158):at dalvik.system.nativeStart.main(本机方法)05-23 14:47:55.187:e/AndroidRuntime(1158):原因:java.lang.reflect.invocationtargetException 05-23 14:47:55.187:e/AndroidRuntime(1158):at java.lang.reflect.Method.invokenative(本机方法)05-23 14:47:55.187:e/AndroidRuntime(1158):at java.lang.reflect.Method.invokenative(roid.view.view$1.onclick(View.java:3594)05-23 14:47:55.187:e/AndroidRuntime(1158):...11更多05-23 14:47:55.187:e/AndroidRuntime(1158):原因:java.lang.ClassCastException:Android.Widget.TextView不能转换为Android.widget.edittext 05-23 14:47:55.187:e/AndroidRuntime(1158):at com.example.bmicalculator.mainActivity.calculateClickHandler(mainActivity.java:23)05-23 14:47:55.187:e/AndroidRuntime(1158):...14更多05-23 14:47:57.797:I/process(1158):发送信号。PID:1158签名:9

我已经确保所有用作参考的ID都是正确的。这个应用程序看起来就像我把所有东西都搬过来之前一样。

共有1个答案

廖华翰
2023-03-14
Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText 05-23 14:45:52.447: E/AndroidRuntime(1083): at 

 EditText weightText = (EditText)findViewById(R.id.weightLabel);
 EditText heightText = (EditText)findViewById(R.id.heightText);

WeightTextHeightText中的一个是TextView

 类似资料: