我是一个相当新的 Android 程序员,在完成了几个教程之后,我开始扩展我完成的 BMI 计算器教程。在此过程中,我需要删除并重新安装 Eclipse。我还不得不切换工作区。由于我的错误,我只能保留应用程序的 .java 文件。我重新创建了 .xml 和清单,只是现在当我尝试单击计算按钮时才收到 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 扩展 Activity { ImageDownloader 下载器;
@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 类:
包 com.example.bmicalculator;
导入 java.io.InputStream;导入 java.net.URL;
导入android.graphics.Bitmap;导入android.graphics.BitmapFactory;导入android.os.AsyncTask;导入android.view.View;导入android.widget.ImageView;公共类 ImageDownloader 扩展 AsyncTask { ImageView 图像;公共 ImageDownloader(ImageView 图像) { this.image = image; } 受保护的位图 doInBackground(String...strings) { 尝试 { 位图位图 = BitmapFactory.decodeStream((InputStream)new URL(strings[0]).getContent()); 返回位图;} catch (Exception e) { System.out.println("错误:" + e); 返回空值;} }
protected void onProgressUpdate(Integer... progress) { } protected void onPostExecute(Bitmap result) { image.setImageBitmap(result); image.setVisibility(View.VISIBLE); } }
和 LogCat:
E/AndroidRuntime(1083): at java.lang.reflect.Method.invoke(Method.java:511) 05-23 14:45:52.447: E/AndroidRuntime(1083): at android.view.View$1.onClick( View.java:3594) 05-23 14:45:52.447: E/AndroidRuntime(1083): ... 11 更多 05-23 14:45:52.447: E/AndroidRuntime(1083): 引起:java.lang。 ClassCastException: android.widget.TextView 不能转换为 android.widget.EditText 05-23 14:45:52.447: E/AndroidRuntime(1083): at com.example.bmicalculator.MainActivity.calculateClickHandler(MainActivity.java:23) 05 -23 14:45:52.447: E/AndroidRuntime(1083): ... 14 更多 05-23 14:47:48.527: D/dalvikvm(1158): GC_CONCURRENT 释放 55K,7% 释放 2770K/2948K,暂停 6ms+ 14 毫秒,总共 96 毫秒 05-23 14:47:48.877:D/gralloc_goldfish(1158):没有检测到 GPU 仿真的模拟器。05-23 14:47:53.837:D/InputEventConsistencyVerifier(1158):KeyEvent: ACTION_UP 但键未按下。05-23 14:47:53.837: D/InputEventConsistencyVerifier(1158): 在 android.widget.EditText{40d2d8e8 VFED..CL .F....I. 32,209-452,288
7f080003 app:id/heightText} 05-23 14:47:53.837: D/InputEventConsistencyVerifier(1158): 0: 在 922301000000 发送,
java:23) 05-23 14:47:55.187: E/AndroidRuntime(1158): ... 14 更多 05-23 14:47:57.797: I/Process(1158): 发送信号。PID:1158 SIG:9
我确保所有用于参考的 id 都是正确的。该应用程序似乎已经像我将所有内容移过来之前一样构建了。