我是Java和Android的新手。我正在尝试创建一个计算圆柱体中液体体积的应用程序,然后将结果返回到textView框。但现在当我点击计算按钮时,它正在崩溃,我不知道我错过了什么。一切都很好。
package com.somename.fuel.volume.calculator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
public class CalculateCylinder extends Activity {
EditText length, width, height, depth;
TextView result;
Button calculate;
double firstNum, secNum, thirdNum, volume, area;
double liqVolume;
double radius;
double a, l, aSide,aTop,b,Arc,FluidSurfaceAreaTotal,r,d,v,h,fsal;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate_cylinder);
calculate = (Button) findViewById(R.id.buttonCylCalc);
length = (EditText) findViewById(R.id.editCylLength);
height = (EditText) findViewById(R.id.editCylHeight);
depth = (EditText) findViewById(R.id.editCylDepth);
result = (TextView) findViewById(R.id.buttonCylResult);
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), (CharSequence) result, Toast.LENGTH_LONG).show();
try {
calculate();
} catch (Exception e) {
e.printStackTrace();
}
result.setText((CharSequence) result);
}
public TextView calculate() throws Exception {
// TODO Auto-generated method stub
double firstNum = Double.valueOf(length.getText().toString());
double secNum = Double.valueOf(height.getText().toString());
double thirdNum = Double.valueOf(depth.getText().toString());
radius = (secNum/2);
area = ((secNum*secNum)*3.14);
//liqVolume = (firstNum) * (radius*radius)(acos(radius-depth/radius))
// Cubic Volume
v = aSide * l;
// Fluid surface Area, Side
aSide = (r*r)*(3.14 / 2 -Math.acos(1-h/r)) - (r-h)*Math.sqrt(h*(2*r-h));
// Fluid Surface Area Top
aTop = 2*Math.sqrt((r*r)-((r-h)*(r-h) * l));
// Fluid Surface Area Bottom
b = fsal*l;
// Fluid Surface Area Total
FluidSurfaceAreaTotal = 2*aSide+aTop+b;
// Cylinder Radius
r = d/2;
// Cylinder Diameter
d = 2*r;
if (h <= r )
// Fluid Side Arc Length
fsal = 2*r*Math.acos((r-h)/r);
else if (h > r)
fsal = 2*r*(3.14-Math.acos((h-r)/r));
return result;
}
});
}
}
这是一个logcat:
10-18 05:24:38.893: E/AndroidRuntime(964): FATAL EXCEPTION: main
10-18 05:24:38.893: E/AndroidRuntime(964): java.lang.ClassCastException: android.widget.Button
10-18 05:24:38.893: E/AndroidRuntime(964): at com.dbryant423.fuel.volume.calculator.CalculateCylinder$1.onClick(CalculateCylinder.java:55 )
10-18 05:24:38.893: E/AndroidRuntime(964): at android.view.View.performClick(View.java:2408)
10-18 05:24:38.893: E/AndroidRuntime(964): at android.view.View$PerformClick.run(View.java:8816)
10-18 05:24:38.893: E/AndroidRuntime(964): at android.os.Handler.handleCallback(Handler.java:587)
10-18 05:24:38.893: E/AndroidRuntime(964): at android.os.Handler.dispatchMessage(Handler.java:92)
10-18 05:24:38.893: E/AndroidRuntime(964): at android.os.Looper.loop(Looper.java:123)
10-18 05:24:38.893: E/AndroidRuntime(964): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-18 05:24:38.893: E/AndroidRuntime(964): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 05:24:38.893: E/AndroidRuntime(964): at java.lang.reflect.Method.invoke(Method.java:521)
10-18 05:24:38.893: E/AndroidRuntime(964): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-18 05:24:38.893: E/AndroidRuntime(964): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-18 05:24:38.893: E/AndroidRuntime(964): at dalvik.system.NativeStart.main(Native Method)
我一直在绞尽脑汁试图弄清楚如何对此进行数学计算,而我却无法找到解决原因的原因。我确定这可能是我在某处遗漏的简单事情。如果您需要其他文件中的更多信息,请告诉我,但这个问题似乎是唯一存在问题的信息。我也知道我还没有删除未使用的变量。
答案 0 :(得分:0)
result.setText((CharSequence) result);
此行导致例外,因为您要将Android View
类投射到CharSequence
类。
可能的解决方案:
将方法public TextView calculate()
的返回类型更改为
public String calculate()
现在方法public String calculate()
返回结果字符串 fsal
你的按钮Click()
就像,
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), (CharSequence) result, Toast.LENGTH_LONG).show();
String resultValue = "";
try {
resultValue = calculate();
} catch (Exception e) {
e.printStackTrace();
}
result.setText( resultValue);
}
答案 1 :(得分:0)
result = (TextView) findViewById(R.id.buttonCylResult);
你是否正确投射?根据命名,我会说你是从一个按钮转换到TextView。尝试更改为,
result = (Button) findViewById(R.id.buttonCylResult);