i am working on an app that has running many async tasks that bring data from server and approx 20 async task are running at a time and there are some threads which validate the data simulatenously.
I have made data models which update my Collections in which data is being stored.
The structure to save data resides on Hashmap that contains an arraylist
So my Question is
Is Android Context ThreadSafe
Are Hashmaps in android ThreadSafe
I am using a Handler to Communicate messages so in this case Handler has a Message Queue is that threadsafe.
because sometimes one out of 10 times my app crashes giving
this is just one logcat in this case it happens while iterating, there are cases in which one task is reading hashmap and other is updating it this is the case in which mostly app crashes.
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): FATAL EXCEPTION: main
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): java.util.ConcurrentModificationException
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at com.Juggle2.Panel.onTouchEvent(Panel.java:823)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at android.view.View.dispatchTouchEvent(View.java:3766)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1767)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1119)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1751)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at android.os.Looper.loop(Looper.java:123)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at java.lang.reflect.Method.invoke(Method.java:521)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): at dalvik.system.NativeStart.main(Native Method)
which is clear indication that when async tasks and threads are trying to update arrayList / hashmaps they give this concurrency exception.
Array Lists and Hashmaps are not thread safe. I would suggest using mutex / synchronized blocks to ensure that only one thread is reading / writing to your collections at a time. Here is a small example to get you started.
public class SomeClass {
private final Object mutex = new Object();
public void someMethodThatNeedsAMutex() {
synchronized(mutex) {
//here you hold the mutex
}
}
}
Related
I have built cocos2d-x game using Android API 21 but it is not working on lower API version, game crashes on startup. These are the logs:
07-28 22:09:47.358: E/dalvikvm(6483): dlopen("/data/app-lib/com.pld.readingrace1a-1/libcocos2dcpp.so") failed: Cannot load library: soinfo_relocate(linker.cpp:975): cannot locate symbol "rand" referenced by "libcocos2dcpp.so"...
07-28 22:09:47.366: E/AndroidRuntime(6483): FATAL EXCEPTION: main
07-28 22:09:47.366: E/AndroidRuntime(6483): java.lang.ExceptionInInitializerError
07-28 22:09:47.366: E/AndroidRuntime(6483): at java.lang.Class.newInstanceImpl(Native Method)
07-28 22:09:47.366: E/AndroidRuntime(6483): at java.lang.Class.newInstance(Class.java:1319)
07-28 22:09:47.366: E/AndroidRuntime(6483): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-28 22:09:47.366: E/AndroidRuntime(6483): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-28 22:09:47.366: E/AndroidRuntime(6483): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-28 22:09:47.366: E/AndroidRuntime(6483): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-28 22:09:47.366: E/AndroidRuntime(6483): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-28 22:09:47.366: E/AndroidRuntime(6483): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 22:09:47.366: E/AndroidRuntime(6483): at android.os.Looper.loop(Looper.java:137)
07-28 22:09:47.366: E/AndroidRuntime(6483): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-28 22:09:47.366: E/AndroidRuntime(6483): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 22:09:47.366: E/AndroidRuntime(6483): at java.lang.reflect.Method.invoke(Method.java:511)
07-28 22:09:47.366: E/AndroidRuntime(6483): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-28 22:09:47.366: E/AndroidRuntime(6483): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-28 22:09:47.366: E/AndroidRuntime(6483): at java.lang.Runtime.loadLibrary(Runtime.java:371)
07-28 22:09:47.366: E/AndroidRuntime(6483): at java.lang.System.loadLibrary(System.java:535)
07-28 22:09:47.366: E/AndroidRuntime(6483): at org.cocos2dx.cpp.AppActivity.<clinit>(AppActivity.java:272)
I have searched and found some answers on stack overflow like
this one
but I want to build my game on API 21 because I am using some new features and I want it to work on older API versions. Can someone help in this regard. Thanks.
Make sure you set your TARGET SDK to a lower version. You should still be able to access most of the API 21 features.
I have integrated facebook unity sdk v5.1.0 in my game I am also using Google playservices for leaderboard.
My game works great on all phone except on kit kat, Moto E, nexus 4, nexus 5. The strange thing is that sometimes it run. but most of the time it crashes
The catch is that I have released the same build with no issues and it was working fine even on kitkat.
But recently my eclipse crashed and I downloaded ADT bundle, it has on androidW SDK and L privew.
I know that something went wrong during the build phase.
So i uninstalled all the sdk and installled only 4.4.2(API level 19)
I am using Unity Ver 4.5.2f1
I have only 4.4.2 (API level 19) in my sdk manager
logs are
07-28 18:54:35.664: E/AndroidRuntime(14925): FATAL EXCEPTION: main
07-28 18:54:35.664: E/AndroidRuntime(14925): Process: com.itecno.cowboysvszombies, PID: 14925
07-28 18:54:35.664: E/AndroidRuntime(14925): java.lang.Error: FATAL EXCEPTION [main]
07-28 18:54:35.664: E/AndroidRuntime(14925): Unity version : 4.5.2f1
07-28 18:54:35.664: E/AndroidRuntime(14925): Device model : motorola XT1022
07-28 18:54:35.664: E/AndroidRuntime(14925): Device fingerprint: motorola/condor_retaildsds/condor_umtsds:4.4.4/KXC21.5-40/46:user/release-keys
07-28 18:54:35.664: E/AndroidRuntime(14925): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.itecno.cowboysvszombies/com.facebook.unity.FBUnityLoginActivity}: java.lang.NullPointerException
07-28 18:54:35.664: E/AndroidRuntime(14925): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
07-28 18:54:35.664: E/AndroidRuntime(14925): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
07-28 18:54:35.664: E/AndroidRuntime(14925): at android.app.ActivityThread.access$800(ActivityThread.java:139)
07-28 18:54:35.664: E/AndroidRuntime(14925): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
07-28 18:54:35.664: E/AndroidRuntime(14925): at android.os.Handler.dispatchMessage(Handler.java:102)
07-28 18:54:35.664: E/AndroidRuntime(14925): at android.os.Looper.loop(Looper.java:136)
07-28 18:54:35.664: E/AndroidRuntime(14925): at android.app.ActivityThread.main(ActivityThread.java:5086)
07-28 18:54:35.664: E/AndroidRuntime(14925): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 18:54:35.664: E/AndroidRuntime(14925): at java.lang.reflect.Method.invoke(Method.java:515)
07-28 18:54:35.664: E/AndroidRuntime(14925): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-28 18:54:35.664: E/AndroidRuntime(14925): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-28 18:54:35.664: E/AndroidRuntime(14925): at dalvik.system.NativeStart.main(Native Method)
07-28 18:54:35.664: E/AndroidRuntime(14925): Caused by: java.lang.NullPointerException
07-28 18:54:35.664: E/AndroidRuntime(14925): at com.facebook.unity.FBUnityLoginActivity.onCreate(FBUnityLoginActivity.java:17)
07-28 18:54:35.664: E/AndroidRuntime(14925): at android.app.Activity.performCreate(Activity.java:5248)
07-28 18:54:35.664: E/AndroidRuntime(14925): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
07-28 18:54:35.664: E/AndroidRuntime(14925): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
Great help if someone can point me in the right direction.
Actually the issue was that i was switching from one scene to another before the call was complete.
And also there are also some clashes with sdk that I am using for ads and analytics
I m using 5 sdk so there were some clashes ....
sorry for bothering u guys and Facebook support too...where I have filed a bug in the SDK...he he
Decided to use Scandit SDK to get the barcode scanning feature in my app. Unfortunately, since I'm relatively new to Android development and Java in general I've run into a few issues that I can't seem to work out. The demo that Scandit provided doesn't give any errors(runs fine) but crashes when I try to start it up (pressing a button from one activity is meant to start it up). I've tried reading the logcat and googling a solution to the problems but I'm not getting the right solutions. Does anyone have an idea of what I'm doing wrong?
Logcat is provided below:
10-08 23:30:00.807 21563-21563/com.kwesimbia.management D/AndroidRuntime? Shutting down VM
10-08 23:30:00.807 21563-21563/com.kwesimbia.management W/dalvikvm? threadid=1: thread exiting with uncaught exception (group=0x40aa4228)
10-08 23:30:00.817 21563-21563/com.kwesimbia.management E/AndroidRuntime? FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3082)
at android.view.View.performClick(View.java:3549)
at android.view.View$PerformClick.run(View.java:14393)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4944)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3077)
at android.view.View.performClick(View.java:3549)
at android.view.View$PerformClick.run(View.java:14393)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4944)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.kwesimbia.management.ScanditSDKDemoSimple
at com.kwesimbia.management.Activity_D.initiateCodeScan(Activity_D.java:63)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3077)
at android.view.View.performClick(View.java:3549)
at android.view.View$PerformClick.run(View.java:14393)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4944)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
10-08 23:30:00.817 32726-587/? E/EmbeddedLogger? App crashed!
Process: com.kwesimbia.management
10-08 23:30:00.817 32726-587/? E/EmbeddedLogger? App crashed!
Package: com.kwesimbia.management v1 (1.0)
10-08 23:30:00.817 32726-587/? E/EmbeddedLogger?
Application Label: firstapp
10-08 23:30:00.827 32726-587/? W/ActivityManager?
Force finishing activity com.kwesimbia.management/.Activity_D
I came across this problem as well, my solution was to extract the library files and put them as their files in the lib folder
The library basically isn't being exported with it
EDIT
So I checked out my project, basically I did these things:
In the libs folder I have a new folder called armeabi and inside that I have libscanditsdk-android-3.3.1.so
Then in my build path I have also referenced the jar file that they provide:
<classpathentry exported="true" kind="lib" path="C:/GIT/Android/FwayScannerProject/FwayScanner/libs/scanditsdk-barcodepicker-android-3.3.1.jar"/>
I can't make it work.
I found many similar question here, examples. But nothing help and nothing work.
Does anyone have a working Fragments in Android 2.2 with android.support.v4 library? (If you are please upload it somewhere.)
I'm almost crying because I can't find what's wrong!
The error is the same as many of people have:
05-15 18:20:20.583: W/dalvikvm(1521): Unable to resolve superclass of Lmy/fragment/test/FragmentTestActivity; (8)
05-15 18:20:20.583: W/dalvikvm(1521): Link of class 'Lmy/fragment/test/FragmentTestActivity;' failed
05-15 18:20:20.593: D/AndroidRuntime(1521): Shutting down VM
05-15 18:20:20.593: W/dalvikvm(1521): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-15 18:20:20.703: E/AndroidRuntime(1521): FATAL EXCEPTION: main
05-15 18:20:20.703: E/AndroidRuntime(1521): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{my.fragment.test/my.fragment.test.FragmentTestActivity}: java.lang.ClassNotFoundException: my.fragment.test.FragmentTestActivity in loader dalvik.system.PathClassLoader[/data/app/my.fragment.test-1.apk]
05-15 18:20:20.703: E/AndroidRuntime(1521): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-15 18:20:20.703: E/AndroidRuntime(1521): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-15 18:20:20.703: E/AndroidRuntime(1521): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-15 18:20:20.703: E/AndroidRuntime(1521): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-15 18:20:20.703: E/AndroidRuntime(1521): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 18:20:20.703: E/AndroidRuntime(1521): at android.os.Looper.loop(Looper.java:123)
05-15 18:20:20.703: E/AndroidRuntime(1521): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-15 18:20:20.703: E/AndroidRuntime(1521): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 18:20:20.703: E/AndroidRuntime(1521): at java.lang.reflect.Method.invoke(Method.java:521)
05-15 18:20:20.703: E/AndroidRuntime(1521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-15 18:20:20.703: E/AndroidRuntime(1521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-15 18:20:20.703: E/AndroidRuntime(1521): at dalvik.system.NativeStart.main(Native Method)
05-15 18:20:20.703: E/AndroidRuntime(1521): Caused by: java.lang.ClassNotFoundException: my.fragment.test.FragmentTestActivity in loader dalvik.system.PathClassLoader[/data/app/my.fragment.test-1.apk]
05-15 18:20:20.703: E/AndroidRuntime(1521): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-15 18:20:20.703: E/AndroidRuntime(1521): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-15 18:20:20.703: E/AndroidRuntime(1521): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-15 18:20:20.703: E/AndroidRuntime(1521): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-15 18:20:20.703: E/AndroidRuntime(1521): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-15 18:20:20.703: E/AndroidRuntime(1521): ... 11 more
05-15 18:20:23.423: I/Process(1521): Sending signal. PID: 1521 SIG: 9
SOLVED!!!! Who have this warnings before error:
05-16 10:08:00.033: W/dalvikvm(1695): Unable to resolve superclass of Lmy/fragment/test/FragmentTestActivity; (7)
05-16 10:08:00.076: W/dalvikvm(1695): Link of class 'Lmy/fragment/test/FragmentTestActivity;' failed
Should export android-support-v4 to right to your app:
Configure Java Build Path -> Order And Export tab -> set checkbox android-support-v4.jar
(Maybe need to remove existing one firstly)
it will moves this library to your apk (as I understand it)
You should have 1 FragmentActivity with fragments inside. What you are doing is trying to put activities (fragmentactivity) inside xml. That wont work. Try to only have 1 top level fragmentactivity and then make the other into fragments and then put those fragments inside your xml.
Since you are obviously just learning about this I'll expand on Warpzit's answer to try and help you out (you should accept his answer so he gets credit, since it is the correct one and he answered first).
This:
public class DetailsActivity extends FragmentActivity
Needs to be this:
public class DetailsActivity extends Fragment
As do any other tabs you want to display as part of that first activity.
There should only be one FragmentActivity unless you are going to start a second activity (and leave the first) that will also have fragments, then the same applies.
why do you have two activities named FragmentTestActivity? or is just writing error?
have you declared it in manifest?
Try to replace android:name="classpath" with class=""classpath.
I am getting Fatal Exception while running the application and in Emulator it gives "Force Close".
Here is the LogCat content,
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): FATAL EXCEPTION: main
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demotab/com.demotab.DemotabActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at android.os.Looper.loop(Looper.java:123)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at java.lang.reflect.Method.invoke(Method.java:521)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at dalvik.system.NativeStart.main(Native Method)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:649)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at android.widget.TabHost.addTab(TabHost.java:213)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at com.demotab.DemotabActivity.onCreate(DemotabActivity.java:36)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-28 22:01:14.567: ERROR/AndroidRuntime(11421): ... 11 more
Please guide me how can I resolve it.
Got the solution,
extended my class with ActivityGroup instead of Activity and added
tabHost.setup(this.getLocalActivityManager());