我从这个应用程序中使用相同的简单算术函数(乘法和除法)的类中复制了这个类。然而,该应用程序的那部分工作正常,这一个一直在崩溃我。 Eclipse没有提示任何错误,代码的设置方式与其他活动相同......但它崩溃了。我试图评论部分,看看它是否是一条线而不是另一条线,它似乎不是。
我意识到编写我编写的代码(一个数组或类似的东西)可能有一种更简单的方法,但就像我是一个初学者一样,我正在慢慢地努力实现这个目标。
您可以提供的任何帮助将不胜感激,谢谢!
logcat的:
06-15 16:43:10.051: W/KeyCharacterMap(318): No keyboard for id 0
06-15 16:43:10.051: W/KeyCharacterMap(318): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-15 16:43:48.091: D/AndroidRuntime(318): Shutting down VM
06-15 16:43:48.091: W/dalvikvm(318): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-15 16:43:48.111: E/AndroidRuntime(318): FATAL EXCEPTION: main
06-15 16:43:48.111: E/AndroidRuntime(318): java.lang.ClassCastException: android.widget.TextView
06-15 16:43:48.111: E/AndroidRuntime(318): at com.tomcat.performance.TenLapAvg$1.onClick(TenLapAvg.java:46)
06-15 16:43:48.111: E/AndroidRuntime(318): at android.view.View.performClick(View.java:2408)
06-15 16:43:48.111: E/AndroidRuntime(318): at android.view.View$PerformClick.run(View.java:8816)
06-15 16:43:48.111: E/AndroidRuntime(318): at android.os.Handler.handleCallback(Handler.java:587)
06-15 16:43:48.111: E/AndroidRuntime(318): at android.os.Handler.dispatchMessage(Handler.java:92)
06-15 16:43:48.111: E/AndroidRuntime(318): at android.os.Looper.loop(Looper.java:123)
06-15 16:43:48.111: E/AndroidRuntime(318): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-15 16:43:48.111: E/AndroidRuntime(318): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 16:43:48.111: E/AndroidRuntime(318): at java.lang.reflect.Method.invoke(Method.java:521)
06-15 16:43:48.111: E/AndroidRuntime(318): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-15 16:43:48.111: E/AndroidRuntime(318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-15 16:43:48.111: E/AndroidRuntime(318): at dalvik.system.NativeStart.main(Native Method)
代码:
package com.tomcat.performance;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TenLapAvg extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.tenlapavg);
Button gen = ((Button) findViewById(R.id.submit));
gen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
EditText lap1, lap2, lap3, lap4, lap5, lap6, lap7, lap8, lap9, lap10, trackSize;
TextView tenLapAvg, avgSpeed1, avgSpeed2, avgSpeed3, avgSpeed4, avgSpeed5, avgSpeed6, avgSpeed7, avgSpeed8, avgSpeed9, avgSpeed10, avgSpeedTotal;
lap1 = ((EditText) findViewById(R.id.lap1));
lap2 = ((EditText) findViewById(R.id.lap2));
lap3 = ((EditText) findViewById(R.id.lap3));
lap4 = ((EditText) findViewById(R.id.lap4));
lap5 = ((EditText) findViewById(R.id.lap5));
lap6 = ((EditText) findViewById(R.id.lap6));
lap7 = ((EditText) findViewById(R.id.lap7));
lap8 = ((EditText) findViewById(R.id.lap8));
lap9 = ((EditText) findViewById(R.id.lap9));
lap10 = ((EditText) findViewById(R.id.lap10));
trackSize = ((EditText) findViewById(R.id.trackSize));
tenLapAvg = ((TextView) findViewById(R.id.tenLapAvg));
avgSpeed1 = ((TextView) findViewById(R.id.avgSpeed1));
avgSpeed2 = ((TextView) findViewById(R.id.avgSpeed2));
avgSpeed3 = ((TextView) findViewById(R.id.avgSpeed3));
avgSpeed4 = ((TextView) findViewById(R.id.avgSpeed4));
avgSpeed5 = ((TextView) findViewById(R.id.avgSpeed5));
avgSpeed6 = ((TextView) findViewById(R.id.avgSpeed6));
avgSpeed7 = ((TextView) findViewById(R.id.avgSpeed7));
avgSpeed8 = ((TextView) findViewById(R.id.avgSpeed8));
avgSpeed9 = ((TextView) findViewById(R.id.avgSpeed9));
avgSpeed10 = ((TextView) findViewById(R.id.avgSpeed10));
avgSpeedTotal = ((TextView) findViewById(R.id.avgSpeedTotal));
double trackSizeVar = Double.parseDouble(trackSize.getText().toString());
double lap1Var = Double.parseDouble(lap1.getText().toString());
double lap2Var = Double.parseDouble(lap2.getText().toString());
double lap3Var = Double.parseDouble(lap3.getText().toString());
double lap4Var = Double.parseDouble(lap4.getText().toString());
double lap5Var = Double.parseDouble(lap5.getText().toString());
double lap6Var = Double.parseDouble(lap6.getText().toString());
double lap7Var = Double.parseDouble(lap7.getText().toString());
double lap8Var = Double.parseDouble(lap8.getText().toString());
double lap9Var = Double.parseDouble(lap9.getText().toString());
double lap10Var = Double.parseDouble(lap10.getText().toString());
double tenLapAvgVar = ((lap1Var + lap2Var + lap3Var + lap4Var + lap5Var + lap6Var + lap7Var + lap8Var + lap9Var + lap10Var)/10);
double lap1Speed = ((trackSizeVar * 3600)/lap1Var);
double lap2Speed = ((trackSizeVar * 3600)/lap2Var);
double lap3Speed = ((trackSizeVar * 3600)/lap3Var);
double lap4Speed = ((trackSizeVar * 3600)/lap4Var);
double lap5Speed = ((trackSizeVar * 3600)/lap5Var);
double lap6Speed = ((trackSizeVar * 3600)/lap6Var);
double lap7Speed = ((trackSizeVar * 3600)/lap7Var);
double lap8Speed = ((trackSizeVar * 3600)/lap8Var);
double lap9Speed = ((trackSizeVar * 3600)/lap9Var);
double lap10Speed = ((trackSizeVar * 3600)/lap10Var);
double avgSpeedTotalVar = ((lap1Speed + lap2Speed + lap3Speed + lap4Speed + lap5Speed + lap6Speed + lap7Speed + lap8Speed + lap9Speed + lap10Speed)/10);
try{
tenLapAvg.setText(String.valueOf(tenLapAvgVar) + " sec");
//avgSpeed1.setText(String.valueOf(lap1Speed) + " mph");
//avgSpeed2.setText(String.valueOf(lap2Speed) + " mph");
//avgSpeed3.setText(String.valueOf(lap3Speed) + " mph");
//avgSpeed4.setText(String.valueOf(lap4Speed) + " mph");
//avgSpeed5.setText(String.valueOf(lap5Speed) + " mph");
//avgSpeed6.setText(String.valueOf(lap6Speed) + " mph");
//avgSpeed7.setText(String.valueOf(lap7Speed) + " mph");
//avgSpeed8.setText(String.valueOf(lap8Speed) + " mph");
//avgSpeed9.setText(String.valueOf(lap9Speed) + " mph");
//avgSpeed10.setText(String.valueOf(lap10Speed) + " mph");
//avgSpeedTotal.setText(String.valueOf(avgSpeedTotalVar) + " mph");
}
catch (ClassCastException e){
e.printStackTrace();}
}}
);
}
public void onClick(View v) {
}
}
XML文件(PER RAGHAV'S REQUEST)是的,我意识到文本全部向用户说“Lap 1”:) -
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lap Average Calculator"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter lap times to calculate the 10-lap average time and each lap's speeds."
android:textAppearance="?android:attr/textAppearanceMedium" />
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/trackSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Track Size: "
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/lap1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal">
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lap1"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/lap1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal">
</EditText>
<TextView
android:id="@+id/avgSpeed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lap1"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/lap2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="@+id/avgSpeed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lap1"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/lap3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="@+id/avgSpeed3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lap1"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/lap4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="@+id/avgSpeed4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lap1"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/lap5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="@+id/avgSpeed5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lap1"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/lap6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="@+id/avgSpeed6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lap1"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/lap7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="@+id/avgSpeed7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lap1"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/lap8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="@+id/avgSpeed8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lap1"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/lap9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="@+id/avgSpeed9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lap1"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/lap10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="@+id/avgSpeed10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<Button
android:id="@+id/submit"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Averages" />
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your 10-lap average time is: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tenLapAvg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000 sec"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your 10-lap average speed is: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/avgSpeedTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000 mph"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
</LinearLayout>
</ScrollView>
答案 0 :(得分:0)
您可能正在其中一个分配中获得ClassCastException:
avgSpeed8 = ((TextView) findViewById(R.id.avgSpeed8));
您将findViewById()
的结果投射到TextView
或EditText
。
仔细检查布局XML中UI组件的类型是否与您尝试将其转换为的类相匹配。我猜你有一个类型,并试图将TextView转换为EditText或将Button转换为EditText或类似的东西。
答案 1 :(得分:0)
基于您的logcat,我建议您确保在xml布局中定义的正确视图中投射TextView。我正在关注这一行:
java.lang.ClassCastException:android.widget.TextView 06-15 16:43:48.111:E / AndroidRuntime(318):at com.tomcat.performance.TenLapAvg $ 1.onClick(TenLapAvg.java:46 )06-15 16:43:48.111:
此外,在某些情况下,R类不会被重新编译,并且您的项目可能不同步(在这种情况下,您可以尝试重建所有类路径)。
答案 2 :(得分:0)
如果我算得正确,那么第46行就是......
avgSpeed3 = ((TextView) findViewById(R.id.avgSpeed3));
检查您的tenlapavg.xml布局文件,确保已正确声明为TextView
。如果我没有正确计算,它将是接近那个的小部件之一。