2011-06-28 112 views
1

我的應用程序有一個很大的問題,因爲它經常崩潰,但從來沒有在同一個地方。內存泄漏android

它工作正常之前,但我有檢查語言的每個應用程序中重複的方法。我決定把方法放在一個名爲Utils的靜態類中,也許這就是問題所在?

這裏是我所有的活動是如何開始:

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); 

}

這是否做內存泄漏?我應該避免使用這個靜態類嗎?我怎麼能不重複相同的代碼20次?

的錯誤,我得到:

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 

它是在談論一個位圖,但這裏的唯一一句位圖可以是背景圖片是所有的活動一樣,但我怎麼能解決?

謝謝

回答

0

我應該避免使用這個靜態類嗎?

我認爲使用靜態類是可以的,但是您必須記住,您使用的上下文只能在應用程序或活動對象的生命週期內使用。這意味着,如果您訪問上下文對象並且它所連接的原始對象已被銷燬,您可能會得到空例外。

我怎麼不不重複相同的代碼20次?

這樣做的一種方法是創建一個基本的Activity類,它包含所有活動中需要完成的所有常見事項。然後,讓每個獨特的活動擴展這個常見的Activity類。

例如,在您共同活動類:

public CommonActivityBase extends Activity { 
    public onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //set up idioma 
    sharedPrefs =PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
    idioma = sharedPrefs.getString("listPref2", "catala"); 
    idioma=Utils.updateLanguage(getApplicationContext(), idioma); 

    } 
    ... 
} 

而在你的活動課:

public ActivityA extends CommonActivityBase { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstance); 
     .... // do other stuff specific to this activity 
     setContentView(R.layout.list_event); 
    } 
    .... 
} 

public ActivityB extends CommonActivityBase { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstance); 
     .... // do other stuff specific to this activity 
     setContentView(R.layout.list_for_activity_b); 
    } 
    .... 
} 
0

後審查日誌,我想有一些問題與您tyring膨脹的XML文件。

+0

是的,但不應該有。它實際上說錯誤是可繪製的第21行,第21行是這樣的:android:background =「@ drawable/fonsbutxacadroid2」應該沒有任何問題,並且在它工作正常之前。 – vallllll

+0

嗯......重新清理它之後重新編譯你的項目......可能這可以解決問題 –

+1

我的問題並不完全在於此,我想知道我的代碼是否會使用靜態類和傳遞上下文來激發內存泄漏到它?謝謝 – vallllll