I copied this class from a similar class I have in this app that uses the same simple arithmetic functions (multiplication and division). Yet that part of the app works fine and this one keeps crashing on me. Eclipse doesn’t hint that anything is wrong, the code is all set up the same way as the other activity… yet it crashes. I’ve tried to comment out parts to see if maybe it’s one line over another and it doesn’t seem to be.
I realize there’s probably a simpler way to write the code I’ve written (an array or something like that), but being as how I’m a beginner I’m slowly working up to that haha.
Any help you could provide would be appreciated, thanks!
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)
CODE:
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 FILE (PER RAGHAV’S REQUEST) And yes, I realize the text all says “Lap 1” to the user at the moment 🙂 —
<?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>
You are probably getting the ClassCastException in one of these assignments:
where you are casting the results of
findViewById()to aTextVieworEditText.Double check that the types of the UI components in your layout XML match the classes you are trying to cast them to. I’m guessing you have a type somewhere and are trying to cast a TextView to an EditText or a Button to an EditText or something like that.