BadTokenException - android

i start a activity,i need to show a alertdialog,so i write a dialog util class(dialoghandler) ,i pass the context to the dialoghandler ,the first start is ok,but when i start the second ,it happend the exception.what should i do,ps help me.
here is the bug report:
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): FATAL EXCEPTION: main
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at android.view.ViewRoot.setView(ViewRoot.java:509)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at android.app.Dialog.show(Dialog.java:241)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at com.eoemobile.api.update.EoeSdkUpdate.onCacheFetched(EoeSdkUpdate.java:170)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at com.eoemobile.api.net.CacheManager.handleCallBackMag(CacheManager.java:362)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at com.eoemobile.api.net.CacheManager.access$1(CacheManager.java:358)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at com.eoemobile.api.net.CacheManager$CallbackHandler.handleMessage(CacheManager.java:375)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at android.os.Looper.loop(Looper.java:123)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at java.lang.reflect.Method.invoke(Method.java:521)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-03 07:23:26.000: ERROR/AndroidRuntime(21525): at dalvik.system.NativeStart.main(Native Method)

You cannot display an application window/dialog through a Context that is not an Activity. Check please that every time you start a dialog it gets correct Context.

Related

PhoneGap - Error occurs when running the example of PhoneGap in Eclipse

Following this tutorial: Getting started with Android.
I try to create my first phonegap project using Eclipse. However, when clicking on the icon of "Cordova Example"(name of the application from the example of PhoneGap) on the screen of device. An error named : "the application Cordova Example (process org.apache.cordova.example) has stopped unexpectedly. Please try again" occurs.
This is the log file when error occurs:
01-02 09:40:46.280: D/AndroidRuntime(310): Shutting down VM
01-02 09:40:46.280: W/dalvikvm(310): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-02 09:40:46.308: E/AndroidRuntime(310): FATAL EXCEPTION: main
01-02 09:40:46.308: E/AndroidRuntime(310): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.apache.cordova.example/org.apache.cordova.example.cordovaExample}: java.lang.ClassNotFoundException: org.apache.cordova.example.cordovaExample in loader dalvik.system.PathClassLoader[/data/app/org.apache.cordova.example-2.apk]
01-02 09:40:46.308: E/AndroidRuntime(310): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
01-02 09:40:46.308: E/AndroidRuntime(310): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-02 09:40:46.308: E/AndroidRuntime(310): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-02 09:40:46.308: E/AndroidRuntime(310): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-02 09:40:46.308: E/AndroidRuntime(310): at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 09:40:46.308: E/AndroidRuntime(310): at android.os.Looper.loop(Looper.java:123)
01-02 09:40:46.308: E/AndroidRuntime(310): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-02 09:40:46.308: E/AndroidRuntime(310): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 09:40:46.308: E/AndroidRuntime(310): at java.lang.reflect.Method.invoke(Method.java:521)
01-02 09:40:46.308: E/AndroidRuntime(310): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-02 09:40:46.308: E/AndroidRuntime(310): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-02 09:40:46.308: E/AndroidRuntime(310): at dalvik.system.NativeStart.main(Native Method)
01-02 09:40:46.308: E/AndroidRuntime(310): Caused by: java.lang.ClassNotFoundException: org.apache.cordova.example.cordovaExample in loader dalvik.system.PathClassLoader[/data/app/org.apache.cordova.example-2.apk]
01-02 09:40:46.308: E/AndroidRuntime(310): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
01-02 09:40:46.308: E/AndroidRuntime(310): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
01-02 09:40:46.308: E/AndroidRuntime(310): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
01-02 09:40:46.308: E/AndroidRuntime(310): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-02 09:40:46.308: E/AndroidRuntime(310): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
01-02 09:40:46.308: E/AndroidRuntime(310): ... 11 more
The java.lang.ClassNotFoundException seems like can't find your Main Class, it could be a name problem of your java main class or in config.xml or in manifest, check all names are correctly.
Regards.

Bluetooth App Crashes with error FATAL EXCEPTION: main

My Bluetooth application's can already auto request to enable bluetooth but when I did some functionality on my option menu the application will crash right after the bluetooth was turned on.
These were the messages found in the LogCat:
12-21 15:20:08.531: D/BluetoothCommandService(1203): start
12-21 15:20:08.531: D/BluetoothCommandService(1203): setState() 0 -> 1
12-21 15:20:08.581: W/dalvikvm(1203): threadid=1: thread exiting with uncaught exception (group=0x40028870)
12-21 15:20:08.851: E/AndroidRuntime(1203): FATAL EXCEPTION: main
12-21 15:20:08.851: E/AndroidRuntime(1203): java.lang.NullPointerException
12-21 15:20:08.851: E/AndroidRuntime(1203): at com.example.javac101.MainActivity$1.handleMessage(MainActivity.java:171)
12-21 15:20:08.851: E/AndroidRuntime(1203): at android.os.Handler.dispatchMessage(Handler.java:95)
12-21 15:20:08.851: E/AndroidRuntime(1203): at android.os.Looper.loop(Looper.java:123)
12-21 15:20:08.851: E/AndroidRuntime(1203): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-21 15:20:08.851: E/AndroidRuntime(1203): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 15:20:08.851: E/AndroidRuntime(1203): at java.lang.reflect.Method.invoke(Method.java:521)
12-21 15:20:08.851: E/AndroidRuntime(1203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-21 15:20:08.851: E/AndroidRuntime(1203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-21 15:20:08.851: E/AndroidRuntime(1203): at dalvik.system.NativeStart.main(Native Method)
What would be the possible main cause of this errors?
title might be null here.
Also change
title.setText(R.string.title_notConnected);
to
title.setText(getString(R.string.title_notConnected));

Multiple Activities in Android with 3 Activites

I am creating multiple activities in Android, this is my logcat error output.
log.txt
05-03 03:17:23.295: E/PhonePolicy(1854): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
05-03 03:17:37.044: W/dalvikvm(1854): threadid=1: thread exiting with uncaught exception (group=0x409db1f8)
05-03 03:17:37.044: E/AndroidRuntime(1854): FATAL EXCEPTION: main
05-03 03:17:37.044: E/AndroidRuntime(1854): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jumoun.itemp/com.jumoun.itemp.Converter}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
05-03 03:17:37.044: E/AndroidRuntime(1854): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-03 03:17:37.044: E/AndroidRuntime(1854): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-03 03:17:37.044: E/AndroidRuntime(1854): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-03 03:17:37.044: E/AndroidRuntime(1854): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-03 03:17:37.044: E/AndroidRuntime(1854): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 03:17:37.044: E/AndroidRuntime(1854): at android.os.Looper.loop(Looper.java:137)
05-03 03:17:37.044: E/AndroidRuntime(1854): at android.app.ActivityThread.main(ActivityThread.java:4427)
05-03 03:17:37.044: E/AndroidRuntime(1854): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 03:17:37.044: E/AndroidRuntime(1854): at java.lang.reflect.Method.invoke(Method.java:511)
05-03 03:17:37.044: E/AndroidRuntime(1854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-03 03:17:37.044: E/AndroidRuntime(1854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-03 03:17:37.044: E/AndroidRuntime(1854): at dalvik.system.NativeStart.main(Native Method)
05-03 03:17:37.044: E/AndroidRuntime(1854): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
05-03 03:17:37.044: E/AndroidRuntime(1854): at com.jumoun.itemp.Converter.onCreate(Converter.java:41)
05-03 03:17:37.044: E/AndroidRuntime(1854): at android.app.Activity.performCreate(Activity.java:4465)
05-03 03:17:37.044: E/AndroidRuntime(1854): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-03 03:17:37.044: E/AndroidRuntime(1854): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-03 03:17:37.044: E/AndroidRuntime(1854): ... 11 more
05-03 03:17:38.704: I/Process(1854): Sending signal. PID: 1854 SIG: 9
This is my java file Converter.java
http://pastebin.com/VNNPy7D5
Thanks guys :)
If I counted right this
ibHome3 = (Button) findViewById(R.id.ibHome3);
is causing your problem. You have it as an ImageButton in your xml but Button in your java declaration. Just change it to
ImageButton ibHome3;
About Logcat
Also, just a little advice on logcat. If you find the first line that says Caused By after Fatal Exception you can track down your problem easier. Here it is
Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
this gives the exception
java.lang.ClassCastException
then find the first line after that which references your package. Here
at com.jumoun.itemp.Converter.onCreate(Converter.java:41)
this tells us the problem starts in Converter.java at line 41
You have two buttons in your layout file, right? They looks to be defined as ImageButton in the XML file. In your Activity, you declare it as Button and try to convert with a cast to (Button). Change your type to ImageButton on your Activity file. It may solve your prolbem.
On line 41, you have to cast your button to ImageButton and not Button :
ibHome3 = (ImageButton) findViewById(R.id.ibHome3);

Android setdrawable from another class

Here's my code. I am trying to set background of a linear layout. But this xml (rowback) is not my main activity content, so gives errors. how can I set some XML from the class which doesn't have that XML in its content?
setContentView(R.layout.activity_quick_search_products);
...
rowb = (LinearLayout)findViewById(R.id.rowback);
...
final Drawable d = getResources().getDrawable(R.drawable.denemebut3);
d.setAlpha(100);
rowb.setBackgroundDrawable(d);
Logcat:
03-30 02:52:50.822: D/AndroidRuntime(14677): Shutting down VM
03-30 02:52:50.822: W/dalvikvm(14677): threadid=1: thread exiting with uncaught exception (group=0x40c841f8)
03-30 02:52:50.822: E/AndroidRuntime(14677): FATAL EXCEPTION: main
03-30 02:52:50.822: E/AndroidRuntime(14677): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vitrinn/com.vitrinn.QuickSearchProducts}: java.lang.NullPointerException
03-30 02:52:50.822: E/AndroidRuntime(14677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
03-30 02:52:50.822: E/AndroidRuntime(14677): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
03-30 02:52:50.822: E/AndroidRuntime(14677): at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-30 02:52:50.822: E/AndroidRuntime(14677): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
03-30 02:52:50.822: E/AndroidRuntime(14677): at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 02:52:50.822: E/AndroidRuntime(14677): at android.os.Looper.loop(Looper.java:137)
03-30 02:52:50.822: E/AndroidRuntime(14677): at android.app.ActivityThread.main(ActivityThread.java:4507)
03-30 02:52:50.822: E/AndroidRuntime(14677): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 02:52:50.822: E/AndroidRuntime(14677): at java.lang.reflect.Method.invoke(Method.java:511)
03-30 02:52:50.822: E/AndroidRuntime(14677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
03-30 02:52:50.822: E/AndroidRuntime(14677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
03-30 02:52:50.822: E/AndroidRuntime(14677): at dalvik.system.NativeStart.main(Native Method)
03-30 02:52:50.822: E/AndroidRuntime(14677): Caused by: java.lang.NullPointerException
03-30 02:52:50.822: E/AndroidRuntime(14677): at com.vitrinn.QuickSearchProducts.onCreate(QuickSearchProducts.java:69)
03-30 02:52:50.822: E/AndroidRuntime(14677): at android.app.Activity.performCreate(Activity.java:4469)
03-30 02:52:50.822: E/AndroidRuntime(14677): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
03-30 02:52:50.822: E/AndroidRuntime(14677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
03-30 02:52:50.822: E/AndroidRuntime(14677): ... 11 more
If R.id.rowback doesn't belong to your main activity xml resource, then you should inflate that resource manually using:
LinearLayout rowb = (LinearLayout) getLayoutInflater().inflate(XML file that contains R.id.rowback, null);
then you can safely do
rowb.setBackgroundDrawable(d);
As an extra, if you have nested elements in rowback then you can invoke:
rowb.findViewById(R.id.Something) safely.
Or if your class is not exteding from any Activity, you can always receive a Context object in its constructor, and then use
LayoutInflater.from(context)
to get a LayoutInflater object so you can inflate any XML resource you need that is not actually in your main content.
Hope this clarifies...

Android app crashes as i try to use kSoap2

Hi im new to Android development.. Sorry.
My Android App crashes as i am trying to use kSoap2 to connect the app to a web service:
My Error log is as follows:
2-05 12:48:05.100: E/dalvikvm(749): Could not find class 'org.ksoap2.serialization.SoapObject', referenced from method com.example.mytestws.MainActivity.onCreate
12-05 12:48:05.100: W/dalvikvm(749): VFY: unable to resolve new-instance 548 (Lorg/ksoap2/serialization/SoapObject;) in Lcom/example/mytestws/MainActivity;
12-05 12:48:05.100: D/dalvikvm(749): VFY: replacing opcode 0x22 at 0x0012
12-05 12:48:05.100: D/dalvikvm(749): VFY: dead code 0x0014-0057 in Lcom/example/mytestws/MainActivity;.onCreate (Landroid/os/Bundle;)V
12-05 12:48:05.250: D/AndroidRuntime(749): Shutting down VM
12-05 12:48:05.250: W/dalvikvm(749): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-05 12:48:05.261: E/AndroidRuntime(749): FATAL EXCEPTION: main
12-05 12:48:05.261: E/AndroidRuntime(749): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
12-05 12:48:05.261: E/AndroidRuntime(749): at com.example.mytestws.MainActivity.onCreate(MainActivity.java:29)
12-05 12:48:05.261: E/AndroidRuntime(749): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-05 12:48:05.261: E/AndroidRuntime(749): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-05 12:48:05.261: E/AndroidRuntime(749): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-05 12:48:05.261: E/AndroidRuntime(749): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-05 12:48:05.261: E/AndroidRuntime(749): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-05 12:48:05.261: E/AndroidRuntime(749): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 12:48:05.261: E/AndroidRuntime(749): at android.os.Looper.loop(Looper.java:123)
12-05 12:48:05.261: E/AndroidRuntime(749): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-05 12:48:05.261: E/AndroidRuntime(749): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 12:48:05.261: E/AndroidRuntime(749): at java.lang.reflect.Method.invoke(Method.java:521)
12-05 12:48:05.261: E/AndroidRuntime(749): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-05 12:48:05.261: E/AndroidRuntime(749): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-05 12:48:05.261: E/AndroidRuntime(749): at dalvik.system.NativeStart.main(Native Method)
You have to add ksoap2 library to your classpath as an external library and also copy the .jar into libs folder of the project.

Categories

Resources