• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"Application has unexpectedly stopped error" when adding onClickListener

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a very simple setup right now where I am experimenting with onClickListeners, however I am getting the "Application has unexpectedly stopped error" and nothing is being reported in logcat and no errors are showing up. Where could the error lie?

package com.example.tabssample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HomeActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TextView textview = new TextView(this);
setContentView(R.layout.home);

Button ask = (Button) findViewById(R.id.ButtonAsk);
ask.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), AskActivity.class);
startActivityForResult(myIntent, 0);
}

});


}
}

package com.example.tabssample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AskActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TextView textview = new TextView(this);
textview.setText("This is the Ask Page");
setContentView(textview);
}
}

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@+drawable/bg_main">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="50px">
<ImageButton
android:id = "@+id/ButtonAsk"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/button_ask_home" />

BTW, my xml file ends properly, I just omitted the rest. Also, if I removed the Button ask and its function, the program runs without a hitch. Any suggestions?

Thanks.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the logcat output - it should have a full stack trace that helps pinpoint the problem.
 
Dave Doonger
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally I would do that but for some reason, the logcat is empty in this case. It's puzzling.
 
Dave Doonger
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Restarted Eclipse and got the logcat to work:

12-16 18:02:30.079: DEBUG/AndroidRuntime(242): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
12-16 18:02:30.079: DEBUG/AndroidRuntime(242): CheckJNI is ON
12-16 18:02:30.389: DEBUG/AndroidRuntime(242): --- registering native functions ---
12-16 18:02:30.778: DEBUG/ddm-heap(242): Got feature list request
12-16 18:02:31.349: DEBUG/PackageParser(51): Scanning package: /data/app/vmdl46047.tmp
12-16 18:02:31.609: INFO/PackageManager(51): Removing non-system package:com.example.tabssample
12-16 18:02:31.609: DEBUG/PackageManager(51): Removing package com.example.tabssample
12-16 18:02:31.609: DEBUG/PackageManager(51): Activities: com.example.tabssample.HelloTabWidget com.example.tabssample.AskActivity com.example.tabssample.HomeActivity com.example.tabssample.VoteActivity com.example.tabssample.ResultsActivity com.example.tabssample.ProfileActivity
12-16 18:02:31.709: DEBUG/PackageManager(51): Scanning package com.example.tabssample
12-16 18:02:31.709: INFO/PackageManager(51): /data/app/vmdl46047.tmp changed; unpacking
12-16 18:02:31.728: DEBUG/installd(31): DexInv: --- BEGIN '/data/app/vmdl46047.tmp' ---
12-16 18:02:32.029: DEBUG/dalvikvm(248): DexOpt: load 64ms, verify 79ms, opt 2ms
12-16 18:02:32.049: DEBUG/installd(31): DexInv: --- END '/data/app/vmdl46047.tmp' (success) ---
12-16 18:02:32.049: DEBUG/PackageManager(51): Activities: com.example.tabssample.HelloTabWidget com.example.tabssample.AskActivity com.example.tabssample.HomeActivity com.example.tabssample.VoteActivity com.example.tabssample.ResultsActivity com.example.tabssample.ProfileActivity
12-16 18:02:32.069: DEBUG/ActivityManager(51): Uninstalling process com.example.tabssample
12-16 18:02:32.069: DEBUG/ActivityManager(51): Force removing process ProcessRecord{43d9dc08 232:com.example.tabssample/10026} (com.example.tabssample/10026)
12-16 18:02:32.099: INFO/Process(51): Sending signal. PID: 232 SIG: 9
12-16 18:02:32.119: INFO/WindowManager(51): WIN DEATH: Window{43caed80 com.example.tabssample/com.example.tabssample.HelloTabWidget paused=false}
12-16 18:02:32.159: INFO/UsageStats(51): Unexpected resume of com.android.launcher while already resumed in com.example.tabssample
12-16 18:02:32.219: WARN/InputManagerService(51): Got RemoteException sending setActive(false) notification to pid 232 uid 10026
12-16 18:02:32.259: DEBUG/ActivityManager(51): Received spurious death notification for thread android.os.BinderProxy@43ca45d0
12-16 18:02:32.379: INFO/ActivityManager(51): Displayed activity com.android.launcher/.Launcher: 253999 ms (total 253999 ms)
12-16 18:02:32.579: INFO/installd(31): move /data/dalvik-cache/data@app@vmdl46047.tmp@classes.dex -> /data/dalvik-cache/data@app@com.example.tabssample.apk@classes.dex
12-16 18:02:32.588: DEBUG/PackageManager(51): New package installed in /data/app/com.example.tabssample.apk
12-16 18:02:32.698: DEBUG/AndroidRuntime(242): Shutting down VM
12-16 18:02:32.698: DEBUG/dalvikvm(242): DestroyJavaVM waiting for non-daemon threads to exit
12-16 18:02:32.708: DEBUG/dalvikvm(242): DestroyJavaVM shutting VM down
12-16 18:02:32.718: DEBUG/dalvikvm(242): HeapWorker thread shutting down
12-16 18:02:32.718: DEBUG/dalvikvm(242): HeapWorker thread has shut down
12-16 18:02:32.718: DEBUG/jdwp(242): JDWP shutting down net...
12-16 18:02:32.718: INFO/dalvikvm(242): Debugger has detached; object registry had 1 entries
12-16 18:02:32.728: DEBUG/dalvikvm(242): VM cleaning up
12-16 18:02:32.829: ERROR/AndroidRuntime(242): ERROR: thread attach failed
12-16 18:02:32.839: DEBUG/ActivityManager(51): Uninstalling process com.example.tabssample
12-16 18:02:32.851: DEBUG/dalvikvm(242): LinearAlloc 0x0 used 623012 of 5242880 (11%)
12-16 18:02:33.029: WARN/ResourceType(51): Resources don't contain package for resource number 0x7f0700e5
12-16 18:02:33.029: WARN/ResourceType(51): Resources don't contain package for resource number 0x7f020031
12-16 18:02:33.029: WARN/ResourceType(51): Resources don't contain package for resource number 0x7f020030
12-16 18:02:33.029: WARN/ResourceType(51): Resources don't contain package for resource number 0x7f050000
12-16 18:02:33.319: DEBUG/dalvikvm(103): GC freed 4312 objects / 242136 bytes in 262ms
12-16 18:02:33.459: DEBUG/dalvikvm(51): GC freed 10076 objects / 649008 bytes in 362ms
12-16 18:02:33.489: WARN/ResourceType(51): Resources don't contain package for resource number 0x7f060001
12-16 18:02:33.518: WARN/ResourceType(51): Resources don't contain package for resource number 0x7f060000
12-16 18:02:34.159: WARN/ResourceType(51): Resources don't contain package for resource number 0x7f0700e5
12-16 18:02:34.189: WARN/ResourceType(51): Resources don't contain package for resource number 0x7f020031
12-16 18:02:34.189: WARN/ResourceType(51): Resources don't contain package for resource number 0x7f020030
12-16 18:02:34.189: WARN/ResourceType(51): Resources don't contain package for resource number 0x7f050000
12-16 18:02:34.199: WARN/ResourceType(51): Resources don't contain package for resource number 0x7f060001
12-16 18:02:34.219: WARN/ResourceType(51): Resources don't contain package for resource number 0x7f060000
12-16 18:02:34.758: DEBUG/AndroidRuntime(253): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
12-16 18:02:34.769: DEBUG/AndroidRuntime(253): CheckJNI is ON
12-16 18:02:34.999: DEBUG/AndroidRuntime(253): --- registering native functions ---
12-16 18:02:35.359: DEBUG/ddm-heap(253): Got feature list request
12-16 18:02:35.829: INFO/ActivityManager(51): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.tabssample/.HelloTabWidget }
12-16 18:02:35.899: INFO/ActivityManager(51): Start proc com.example.tabssample for activity com.example.tabssample/.HelloTabWidget: pid=259 uid=10026 gids={}
12-16 18:02:35.929: DEBUG/AndroidRuntime(253): Shutting down VM
12-16 18:02:35.929: DEBUG/dalvikvm(253): DestroyJavaVM waiting for non-daemon threads to exit
12-16 18:02:35.949: DEBUG/dalvikvm(253): DestroyJavaVM shutting VM down
12-16 18:02:35.949: DEBUG/dalvikvm(253): HeapWorker thread shutting down
12-16 18:02:35.949: DEBUG/dalvikvm(253): HeapWorker thread has shut down
12-16 18:02:35.959: DEBUG/jdwp(253): JDWP shutting down net...
12-16 18:02:35.959: INFO/dalvikvm(253): Debugger has detached; object registry had 1 entries
12-16 18:02:35.969: DEBUG/dalvikvm(253): VM cleaning up
12-16 18:02:35.989: ERROR/AndroidRuntime(253): ERROR: thread attach failed
12-16 18:02:36.039: DEBUG/dalvikvm(253): LinearAlloc 0x0 used 638596 of 5242880 (12%)
12-16 18:02:36.249: DEBUG/ddm-heap(259): Got feature list request
12-16 18:02:36.408: INFO/ARMAssembler(51): generated scanline__00000177:03515104_00000001_00000000 [ 73 ipp] (95 ins) at [0x35bce0:0x35be5c] in 1209000 ns
12-16 18:02:36.509: INFO/ARMAssembler(51): generated scanline__00000077:03545404_00000004_00000000 [ 47 ipp] (67 ins) at [0x35be60:0x35bf6c] in 618000 ns
12-16 18:02:37.099: DEBUG/AndroidRuntime(259): Shutting down VM
12-16 18:02:37.099: WARN/dalvikvm(259): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-16 18:02:37.099: ERROR/AndroidRuntime(259): Uncaught handler: thread main exiting due to uncaught exception
12-16 18:02:37.139: ERROR/AndroidRuntime(259): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabssample/com.example.tabssample.HelloTabWidget}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabssample/com.example.tabssample.HomeActivity}: java.lang.ClassCastException: android.widget.ImageButton
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.os.Looper.loop(Looper.java:123)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at java.lang.reflect.Method.invoke(Method.java:521)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at dalvik.system.NativeStart.main(Native Method)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabssample/com.example.tabssample.HomeActivity}: java.lang.ClassCastException: android.widget.ImageButton
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.widget.TabHost.setCurrentTab(TabHost.java:320)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.widget.TabHost.addTab(TabHost.java:213)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at com.example.tabssample.HelloTabWidget.onCreate(HelloTabWidget.java:28)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): ... 11 more
12-16 18:02:37.139: ERROR/AndroidRuntime(259): Caused by: java.lang.ClassCastException: android.widget.ImageButton
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at com.example.tabssample.HomeActivity.onCreate(HomeActivity.java:25)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
12-16 18:02:37.139: ERROR/AndroidRuntime(259): ... 20 more
12-16 18:02:37.169: INFO/Process(51): Sending signal. PID: 259 SIG: 3
12-16 18:02:37.169: INFO/dalvikvm(259): threadid=7: reacting to signal 3
12-16 18:02:37.179: INFO/dalvikvm(259): Wrote stack trace to '/data/anr/traces.txt'
12-16 18:02:37.418: INFO/ARMAssembler(51): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x434e20:0x434edc] in 572000 ns
12-16 18:02:37.458: INFO/ARMAssembler(51): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x434ee0:0x4350a8] in 642000 ns
12-16 18:02:45.892: WARN/ActivityManager(51): Launch timeout has expired, giving up wake lock!
12-16 18:02:46.349: WARN/ActivityManager(51): Activity idle timeout for HistoryRecord{43bc2328 com.example.tabssample/.HelloTabWidget}
12-16 18:02:51.458: DEBUG/dalvikvm(103): GC freed 2351 objects / 135392 bytes in 93ms
12-16 18:02:56.509: DEBUG/dalvikvm(219): GC freed 488 objects / 44136 bytes in 140ms


It looks like I have to find a way to handle an exception for the onClick method I implemented. Any ideas as to how and why, because there are instances where nothing needs to be handled for onClickListeners?

Thanks.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont know that whether you have resolved the issue or not.
But issue is in Casting a class. In manifest file you are using ImageButton and in Activity you a casting it to Button so basically you are getting a class cast exception.

And also check that you have mentioned the activity in manifest file.


 
Dave Doonger
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Hardik, I should have posted it but I have already made those changes. It allowed the app to load now but I am still experiencing the same error upon ButtonClick. It definitely has something to do with how to handle an onClickListener exception...just not sure how or what it could be.
 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Intent myIntent = new Intent(view.getContext(), AskActivity.class);
startActivityForResult(myIntent, 0);

Change above to

Intent myIntent = new Intent(HomeActivity.this, AskActivity.class);
startActivity(myIntent);
 
Dave Doonger
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, same application termination error upon ButtonClick.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused. You can't cast an ImageButton to a Button, but after fixing that you're still getting a ClassCastException? What changes have you made? Seeing the stack trace along with relevant excerpts of the code and layout would help.
 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In layout file you wrote
android:background="@+drawable/bg_main"

What is that + sign in it. Remove that.

If still error comes.
Again post your whole code with the whatever changes you have made.
 
Ranch Hand
Posts: 56
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The casting error is because you give the intent the context of the view (ImageButton) while it should be the context of the application package.

But by making the changes Hardik suggested a few post back and change to:

this should be resolved.
So probably now you're getting a different error.
If you post a new logcat, just the "ERROR/" lines and a few lines above that will do, we can have a look at it.
 
Dave Doonger
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to reiterate, after the changes I've made, the app launches. However, after I click the AskButton it crashes with "App has unexpectedly stopped" error instead of bringing up AskActivity.



Thanks again. BTW, the bg_main isn't causing the issue. It's the background image and it loads fine. Also, do you guys usually copy the entire logcat or just the 'error' sections. I wasn't sure so I posted the whole thing. I want to adhere by the normal standards and not post unnecessary data. Thanks.
 
Misha van Tol
Ranch Hand
Posts: 56
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As said, just the last /ERROR lines and say 10-15 lines above it is enough.

First you should remove the <TabHost> from the XML, you're not using / implementing it completely now. This is probably responsible for the NullPointerExecption.
Besides that, you're still creating the Intent wrong, it should be like this:
 
Dave Doonger
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been trying a lot of different variations including your suggestions in changing how my intent is used. I'm still getting an error. It is:

 
Misha van Tol
Ranch Hand
Posts: 56
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't removed the TabHost as I suggested. In the logcat you can see it's the TabHost that's throwing the NullPointerException. (see line 3)
You either implement the entire TabHost completely (if you want a tabbed view) or remove it if you're not using it.
 
Dave Doonger
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, I haven't removed it from my main.xml because I am utilizing it to open a Tabbed Layout. I did, however, remove it from the ask.xml because it's already created through another class (using the main.xml). I have researched this issue for a few days and it seems that it is a pervasive issue in using TabHost. There are a few suggestions in using ActivityGroup within TabHost as way to deal with this, but I am still fairly new and will keep reading up on this.

The issue is, how can a button within one tab link to a focus of a different tab with a different activity and still prevent the Tabs from disappearing. I can do it successfully by just calling the Activity associated with the ButtonClick but the Tabs disappear. On the other hand, it is not possible to embed a TabHost in the new Activity's xml and extend with TabActivity because it is already used initially on application launch.

I think my logic is on track, but please direct me if I'm mistaken. Thanks again everyone.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic