I have a big problem with my app as it crashes often but never at the same spot.
Before it was working fine but I had a method repeated in every app that checked for the language. I have decided to put the method in a static class called Utils and maybe this is the problem???
here is how all my activities start:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set up idioma
sharedPrefs =PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
idioma = sharedPrefs.getString("listPref2", "catala");
idioma=Utils.updateLanguage(getApplicationContext(), idioma);
setContentView(R.layout.list_event);
}
does this do memory leaks? should I avoid using the static class for this? how could I don not to repeat the same code 20 times?
the errors I get:
06-28 15:24:31.411: INFO/ActivityManager(52): Starting activity: Intent { act=android.intent.action.VIEW cmp=com.tresipunt.butxaca.com/com.tresipunt.butxaca.AllEventsDetailsTabs (has extras) }
06-28 15:24:31.551: ERROR/dalvikvm-heap(341): 2160000-byte external allocation too large for this process.
06-28 15:24:31.551: ERROR/(341): VM won't let us allocate 2160000 bytes
06-28 15:24:31.551: DEBUG/AndroidRuntime(341): Shutting down VM
06-28 15:24:31.551: WARN/dalvikvm(341): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
06-28 15:24:31.561: ERROR/AndroidRuntime(341): Uncaught handler: thread main exiting due to uncaught exception
06-28 15:24:31.592: ERROR/AndroidRuntime(341): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tresipunt.butxaca.com/com.tresipunt.butxaca.AllEventsDetailsTabs}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tresipunt.butxaca.com/com.tresipunt.butxaca.AllEventsDetailsTabList}: android.view.InflateException: Binary XML file line #21: Error inflating class java.lang.reflect.Constructor
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.os.Looper.loop(Looper.java:123)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.ActivityThread.main(ActivityThread.java:4203)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at java.lang.reflect.Method.invoke(Method.java:521)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at dalvik.system.NativeStart.main(Native Method)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tresipunt.butxaca.com/com.tresipunt.butxaca.AllEventsDetailsTabList}: android.view.InflateException: Binary XML file line #21: Error inflating class java.lang.reflect.Constructor
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2242)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:631)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.widget.TabHost.setCurrentTab(TabHost.java:317)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.widget.TabHost.addTab(TabHost.java:210)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at com.tresipunt.butxaca.AllEventsDetailsTabs.onCreate(AllEventsDetailsTabs.java:101)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): ... 11 more
06-28 15:24:31.592: ERROR/AndroidRuntime(341): Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class java.lang.reflect.Constructor
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.createView(LayoutInflater.java:512)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:562)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.Activity.setContentView(Activity.java:1620)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at com.tresipunt.butxaca.AllEventsDetailsTabList.onCreate(AllEventsDetailsTabList.java:117)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): ... 20 more
06-28 15:24:31.592: ERROR/AndroidRuntime(341): Caused by: java.lang.reflect.InvocationTargetException
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.widget.ListView.<init>(ListView.java:148)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at java.lang.reflect.Constructor.constructNative(Native Method)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.createView(LayoutInflater.java:499)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): ... 31 more
06-28 15:24:31.592: ERROR/AndroidRuntime(341): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.graphics.Bitmap.nativeCreate(Native Method)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:476)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:322)
06-28 15:24:31.592: ERROR/AndroidRuntime(341): at android.graphics.drawable.Drawable.createFromResourceStr
it is talking about a bitmap but the nly bitmap here can be the background image which is the same for all activities but how could I solve that?
thank you
I think it’s ok to use a static class but you have to keep in mind, that the context you use use should only be used within the lifetime of the application or activity object where you got it from. This means if you access the context object and the original object it is attached to is already destroyed, you might get a null exception.
One way to do this is to create a base Activity class which contains all of the common things that need to be done in all your activities. And then, just have each of your unique activities extend this common Activity class.
For example, in your common Activity class:
And in your activity classes: