我是一个相当新的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都是正确的。这个应用程序看起来就像我把所有东西都搬过来之前一样。
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);
WeightText
和HeightText
中的一个是TextView
。
问题内容: 您好,我是学习android的新手,我正尝试在学习Android时制作一个半有用的应用程序(对我自己),基本上我是在第一时间接受有用的Java代码(ping X,端口扫描)并添加将它们添加到我的应用中,我无法弄清楚 为什么 会发生此错误,我确实认为这与代码的第77行有关,但是我想了解为什么它无法正常工作,因此下次我可以更好地帮助自己。很抱歉出现罗word的问题,我不知道什么时候闭嘴。
我正在构建一个简单的客户机-服务器系统。我构建了一个Java服务器&一个运行在mine设备上的android客户端(两者连接到同一个LAN)。 当我试图建立一个连接(通过单击按钮)时,客户端失败并出现错误 Android客户端代码: manifest.xml: 布局文件:
xml文件 误差 05-21 21:31:39.665 135 9-1359/com.example.champ.remodme e/androidruntime:致命异常:main process:com.example.champ.remodme,pid:1359 java.lang.illegalstateException:无法执行Android方法:onclick at android.
null java.lang.IllegalStateException:在onSaveInstanceState e/androidRuntime(9008):at android.support.v4.app.fragmentManagerImpl.checkStateLoss(fragmentManager.java:1354)e/androidRuntime(9008):at androi
当我创建地图谷歌地图活动在android Studio...创建后,需要时间同步渐变...则显示以下错误....