Reputation: 69
My application ran fine the first few times and now has decided to stop "unexpectedly." I have tried the following methods to see if I could resolve it, but to no avail:
I notice that half of the time, Eclipse always seems to get hung up in trying to launch HOME because of some disconnection problem (why it keeps doing this is becoming aggravating), so i always end up hitting Run again and starts another emulator (see Console dialog below).
[2012-11-06 11:58:03 - CostALoad] ------------------------------
[2012-11-06 11:58:03 - CostALoad] Android Launch!
[2012-11-06 11:58:03 - CostALoad] adb is running normally.
[2012-11-06 11:58:03 - CostALoad] Performing com.example.costaload.MainActivity activity launch
[2012-11-06 11:58:04 - CostALoad] Automatic Target Mode: launching new emulator with compatible AVD 'myavd'
[2012-11-06 11:58:04 - CostALoad] Launching a new emulator with Virtual Device 'myavd'
[2012-11-06 11:58:06 - CostALoad] New emulator found: emulator-5554
[2012-11-06 11:58:06 - CostALoad] Waiting for HOME ('android.process.acore') to be launched...
[2012-11-06 11:58:40 - CostALoad] HOME is up on device 'emulator-5554'
[2012-11-06 11:58:40 - CostALoad] Uploading CostALoad.apk onto device 'emulator-5554'
[2012-11-06 11:58:42 - CostALoad] Installing CostALoad.apk...
[2012-11-06 11:59:01 - CostALoad] Success!
[2012-11-06 11:59:02 - CostALoad] Starting activity com.example.costaload.MainActivity on device
emulator- 5554
[2012-11-06 11:59:04 - CostALoad] ActivityManager: Starting: Intent { act=android.intent.action.MAIN
cat=[android.intent.category.LAUNCHER] cmp=com.example.costaload/.MainActivity }
[2012-11-06 12:17:00 - CostALoad] ------------------------------
[2012-11-06 12:17:00 - CostALoad] Android Launch!
[2012-11-06 12:17:00 - CostALoad] adb is running normally.
[2012-11-06 12:17:00 - CostALoad] Performing com.example.costaload.MainActivity activity launch
[2012-11-06 12:17:01 - CostALoad] Automatic Target Mode: launching new emulator with compatible AVD 'myavd'
[2012-11-06 12:17:01 - CostALoad] Launching a new emulator with Virtual Device 'myavd'
[2012-11-06 12:17:25 - CostALoad] New emulator found: emulator-5554
[2012-11-06 12:17:25 - CostALoad] Waiting for HOME ('android.process.acore') to be launched...
[2012-11-06 12:18:43 - CostALoad] emulator-5554 disconnected! Cancelling 'com.example.costaload.MainActivity
activity launch'!
[2012-11-06 12:20:07 - CostALoad] ------------------------------
[2012-11-06 12:20:07 - CostALoad] Android Launch!
[2012-11-06 12:20:07 - CostALoad] adb is running normally.
[2012-11-06 12:20:07 - CostALoad] Performing com.example.costaload.MainActivity activity launch
[2012-11-06 12:20:07 - CostALoad] Automatic Target Mode: launching new emulator with compatible AVD 'myavd'
[2012-11-06 12:20:07 - CostALoad] Launching a new emulator with Virtual Device 'myavd'
[2012-11-06 12:20:22 - Emulator] WARNING: Data partition already in use. Changes will not persist!
[2012-11-06 12:20:22 - Emulator] WARNING: Cache partition already in use. Changes will not persist!
[2012-11-06 12:20:23 - CostALoad] New emulator found: emulator-5556
[2012-11-06 12:20:23 - CostALoad] Waiting for HOME ('android.process.acore') to be launched...
[2012-11-06 12:22:07 - CostALoad] HOME is up on device 'emulator-5556'
[2012-11-06 12:22:07 - CostALoad] Uploading CostALoad.apk onto device 'emulator-5556'
[2012-11-06 12:22:08 - CostALoad] Installing CostALoad.apk...
[2012-11-06 12:22:26 - CostALoad] Success!
[2012-11-06 12:22:27 - CostALoad] Starting activity com.example.costaload.MainActivity on device
emulator-5556
[2012-11-06 12:22:31 - CostALoad] ActivityManager: Starting: Intent { act=android.intent.action.MAIN
cat=[android.intent.category.LAUNCHER] cmp=com.example.costaload/.MainActivity }
So, you can see where I hit RUN and then 5556 started, uploaded and then stopped. This has been going on all morning and none of the steps that I described above hasn't seemed to have stopped the problem.
When I look at LOGCAT, which I admit I am not great at trying to figure out since there's stuff on there that I can't identify as I am a beginner (example: "zygote"??, looper, etc.). In other words, I can't understand how the code worked fine the first couple of times and now are throwing what is shown in the logs below. The only thing I recognize is the android button widget and it's some kind of cast exception. Odd since it's only a button listener and case structure to listen for which button is pressed. Beyond that, I have no idea and the line numbers are for files that I don't even know about.
11-06 12:22:33.080: D/ddm-heap(223): Got feature list request
11-06 12:22:34.350: D/AndroidRuntime(223): Shutting down VM
11-06 12:22:34.350: W/dalvikvm(223): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
11-06 12:22:34.359: E/AndroidRuntime(223): Uncaught handler: thread main exiting due to uncaught exception
11-06 12:22:34.369: E/AndroidRuntime(223): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.costaload/com.example.costaload.MainActivity}: java.lang.ClassCastException:
android.widget.Button
11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2496)
11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2512)
11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.ActivityThread.access$2200
(ActivityThread.java:119)
11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1863)
11-06 12:22:34.369: E/AndroidRuntime(223): at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 12:22:34.369: E/AndroidRuntime(223): at android.os.Looper.loop(Looper.java:123)
11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-06 12:22:34.369: E/AndroidRuntime(223): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 12:22:34.369: E/AndroidRuntime(223): at java.lang.reflect.Method.invoke(Method.java:521)
11-06 12:22:34.369: E/AndroidRuntime(223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:860)
11-06 12:22:34.369: E/AndroidRuntime(223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-06 12:22:34.369: E/AndroidRuntime(223): at dalvik.system.NativeStart.main(Native Method)
11-06 12:22:34.369: E/AndroidRuntime(223): Caused by: java.lang.ClassCastException: android.widget.Button
11-06 12:22:34.369: E/AndroidRuntime(223): at com.example.costaload.MainActivity.onCreate
(MainActivity.java:28)
11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1047)
11-06 12:22:34.369: E/AndroidRuntime(223): at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2459)
11-06 12:22:34.369: E/AndroidRuntime(223): ... 11 more
11-06 12:22:34.419: I/dalvikvm(223): threadid=7: reacting to signal 3
11-06 12:22:34.419: E/dalvikvm(223): Unable to open stack trace file '/data/anr/traces.txt':
Permission denied
11-06 12:27:34.529: I/Process(223): Sending signal. PID: 223 SIG: 9
Try as I am to interpret the errors, the best guess I have is that it has something to do with the button widget and the rest looks like it's Java itself taking a nosedive because of something. LOGCAT is referencing things which I have no idea what it is or have never touched. What else can I do to stop all this from happening? I have included my code below and, like I said, it had run before and now I can't see what the trouble is or if there's something in my project files that is causing it:
package com.example.costaload;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends Activity {
EditText mile,diesel;
Button button1, button2;
TextView tv, tv2, tv3;
private double x, y, z, costper, gallon, litres, ophours, stopdrive;
CheckBox checkBox1, checkBox2, checkBox3, checkBox4;
NumberFormat format = NumberFormat.getCurrencyInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mile= (EditText) findViewById(R.id.mile);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
tv = (TextView) findViewById(R.id.cost);
tv2 = (TextView) findViewById(R.id.cpm);
tv3 = (TextView) findViewById(R.id.gallons);
diesel= (EditText) findViewById(R.id.diesel);
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.button1:
x=Double.parseDouble(mile.getText().toString());
y=Double.parseDouble(diesel.getText().toString());
if (checkBox2.isChecked()) {
x=x*2;
}
if (checkBox1.isChecked()) {
x=x*0.62137;
}
ophours = 0;
ophours = (x/55)+2;
if (ophours>=11){
stopdrive = (ophours/10)-1;
if (stopdrive>1){
ophours = ophours + (stopdrive*10);
}
gallon = x/5.5;
if (checkBox4.isChecked()) {
gallon = gallon + (ophours*1.1);
}
if (checkBox3.isChecked()) {
litres = gallon*3.785;
tv3.setText(new DecimalFormat("####.##").format(litres)+"L");
}
z=(gallon*y)+(x*0.655);
costper=z/x;
tv.setText(format.format(z));
tv2.setText(format.format(costper)+"/mile");
tv3.setText(new DecimalFormat("####.##").format(gallon)+"gal.");
break;}
case R.id.button2:
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Url.parse("http://www.ratenroll.com"));
startActivity(browserIntent);
break;
}
};
};
}
Here is the XML for the main activity and I'm stumped because all the buttons are buttons and so on:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="# of miles"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="40dp"
android:text="Price of diesel"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/diesel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/mile"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/mile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/checkBox3"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Visit www.ratenroll.com" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/diesel"
android:layout_marginTop="14dp"
android:text="Kilometres (not miles)" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignParentRight="true"
android:text="Round trip" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/checkBox1"
android:text="Litres (not gallons)" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox3"
android:layout_alignBottom="@+id/checkBox3"
android:layout_alignParentRight="true"
android:text="Reefer load" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox3"
android:layout_marginTop="50dp"
android:layout_toRightOf="@+id/textView2"
android:text="Compute" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="26dp"
android:text="Total Cost"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView3"
android:text="Cost per mile"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView4"
android:layout_alignParentRight="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/cpm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignParentRight="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView4"
android:layout_below="@+id/textView4"
android:text="Fuel required"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/gallons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_alignParentRight="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Thank you for having a look.
Upvotes: 0
Views: 2318
Reputation: 4641
You can find the line that's causing the error by looking at the LogCat line that directly follows the line starting with Caused by:
Basically the format is such:
Caused by: (error type)
at:problematic method:line number
So for you the answer lies here:
11-06 12:22:34.369: E/AndroidRuntime(223): Caused by: java.lang.ClassCastException: android.widget.Button
11-06 12:22:34.369: E/AndroidRuntime(223): at com.example.costaload.MainActivity.onCreate (MainActivity.java:28)
Line 28 of your onCreate method in your MainActivity class is the problem.
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
Class cast exception? That view that you identified is not a CheckBox.
Your MainActivity doesn't seem to import the R class (which you're using). I'm not sure which version of Eclipse you have or if it handles this in some odd fashion, but it may be defaulting to some value you don't want.
Put this at the top of your code with your other imports
import com.example.costaload.R;
Another thing: I'm not sure if this is your intent but the break;
command for the first case
in your switch
statement is inside an if block. If ophours
is less than 11, your switch statement will fall through to the next case.
Upvotes: 2
Reputation: 1433
Have you checked that in the activity_main layout you have the button1 and button2 elements with Button type? If so, try to clean the project in order to make ADT rewrite the R file.
This kind of problems happen frequently when you start learning Android programming (such as forgetting to change the manifest, etc).
Upvotes: 0
Reputation: 3
Essentially, (probably) one of the "Button"s you have isn't actually a button, make sure than button1 and button2 are in fact buttons in your layout.xml file. You can't cast (for instance) a TextView to a Button.
Upvotes: 0