私はかなり新しい Android プログラマーで、いくつかのチュートリアルに取り組んだ後、完了した BMI Calculator チュートリアルを拡張し始めました。このプロセス中に、Eclipse を削除して再インストールする必要が生じました。また、ワークスペースを切り替える必要がありました。私のミスにより、アプリの .java ファイルしか保持できませんでした。.xml とマニフェストを再作成しましたが、計算ボタンをクリックしようとすると IllegalStateException が発生するようになりました。切り替えて再作成する前は、すべて正常に動作していました。私は 4.2.2 を実行している Nexus Galaxy のエミュレーターを使用しています。コードは次のとおりです。
パッケージcom.example.bmiccalculator;
android.app.Activity をインポートします。android.os.Bundle をインポートします。android.view.Menu をインポートします。android.view.View をインポートします。android.widget.EditText をインポートします。android.widget.ImageView をインポートします。android.widget.TextView をインポートします。
public class MainActivity extends Activity { 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 クラス:
パッケージcom.example.bmiccalculator;
java.io.InputStream をインポートします。java.net.URL をインポートします。
android.graphics.Bitmap をインポートします。android.graphics.BitmapFactory をインポートします。android.os.AsyncTask をインポートします。android.view.View をインポートします。android.widget.ImageView をインポートします。public class ImageDownloader extends AsyncTask { ImageView image; public ImageDownloader(ImageView 画像) { this.image = 画像; } protected Bitmap doInBackground(String... strings) { try { Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(strings[0]).getContent()); ビットマップを返します。} catch (例外 e) { System.out.println("エラー: " + e); null を返します。} }
protected void onProgressUpdate(Integer... progress) { } protected void onPostExecute(Bitmap result) { image.setImageBitmap(result); image.setVisibility(View.VISIBLE); } }
そしてLogCat:
E/AndroidRuntime(1083): java.lang.reflect.Method.invoke(Method.java:511) 05-23 14:45:52.447: E/AndroidRuntime(1083): 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): com.example.bmicalculator.MainActivity.calculateClickHandler(MainActivity.java:23) にキャストできません 05 -23 14:45:52.447: E/AndroidRuntime(1083): ... 14 more 05-23 14:47:48.527: D/dalvikvm(1158): GC_CONCURRENT 解放 55K、7% 解放 2770K/2948K、一時停止 6ms+ 14ms、合計 96ms 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 が正しいことを確認しました。アプリは、すべてを移動する前と同じように構築されているようです。