2014-02-18 40 views
0

我最近修改了我的應用程序。我添加了大約30個不同的片段(但不多於2個同時使用)。我不知道這是否會消耗內存並導致問題。無論如何,應用程序崩潰時,我打開這個片段。我用日誌來找出問題所在。但我不知道什麼的問題是。Android - OutOfMemoryError同時膨脹類com.hultan.formler.ResizableImageView

logcat的:

02-18 03:47:35.782: E/AndroidRuntime(1032): FATAL EXCEPTION: main 
02-18 03:47:35.782: E/AndroidRuntime(1032): android.view.InflateException: Binary XML file line #9: Error inflating class com.hultan.formler.ResizableImageView 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.view.LayoutInflater.createView(LayoutInflater.java:620) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at com.hultan.formler.AccelerationFragment.onCreateView(AccelerationFragment.java:32) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.app.Fragment.performCreateView(Fragment.java:1695) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.app.BackStackRecord.run(BackStackRecord.java:682) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.os.Handler.handleCallback(Handler.java:730) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.os.Handler.dispatchMessage(Handler.java:92) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.os.Looper.loop(Looper.java:137) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at java.lang.reflect.Method.invoke(Method.java:525) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at dalvik.system.NativeStart.main(Native Method) 
02-18 03:47:35.782: E/AndroidRuntime(1032): Caused by: java.lang.reflect.InvocationTargetException 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at java.lang.reflect.Constructor.constructNative(Native Method) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.view.LayoutInflater.createView(LayoutInflater.java:594) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  ... 20 more 
02-18 03:47:35.782: E/AndroidRuntime(1032): Caused by: java.lang.OutOfMemoryError 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.content.res.Resources.loadDrawable(Resources.java:2105) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.widget.ImageView.<init>(ImageView.java:127) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at android.widget.ImageView.<init>(ImageView.java:117) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  at com.hultan.formler.ResizableImageView.<init>(ResizableImageView.java:11) 
02-18 03:47:35.782: E/AndroidRuntime(1032):  ... 23 more 

我的片段

AccelerationFragment.java:

public class AccelerationFragment extends Fragment { 
int color; 
Button bAdd; 
String yes; 

//EDIT HERE 
String FILENAME ="acceleration.txt"; 
//EDIT HERE 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    //EDIT HERE 
    View rootView = inflater.inflate(R.layout.fragment_acceleration, container, false); 
    bAdd = (Button) rootView.findViewById(R.id.addbutton); 
    //EDIT HERE 
    String read = readfromfile(); 
    color = Integer.parseInt(read); 
    switch (color){ 
    case 0: 
     bAdd.setBackgroundResource(R.drawable.buttonstylegreen); 
     bAdd.setText("Lägg till i mina formler"); 
     break; 
    case 1: 
     bAdd.setBackgroundResource(R.drawable.buttonstylered); 
     bAdd.setText("Ta bort från mina formler"); 
     break; 
    } 

    bAdd.setOnClickListener(new OnClickListener() {@Override 
     public void onClick(View v) { 

     switch(color){ 
     case 0: 
      bAdd.setBackgroundResource(R.drawable.buttonstylered); 
      color=1; 
      yes = Integer.toString(color); 
      bAdd.setText("Ta bort från mina formler"); 
      writetofile(yes); 
      break; 
     case 1: 
      bAdd.setBackgroundResource(R.drawable.buttonstylegreen); 
      bAdd.setText("Lägg till i mina formler"); 
      color=0; 
      yes = Integer.toString(color); 
      writetofile(yes); 
      break; 
     } 
    }}); 
    return rootView; 
} 
public void onBackPressed() { 
    android.app.FragmentManager FragmentManager = getActivity().getFragmentManager(); 
    FragmentManager.popBackStack(); 
} 
private String readfromfile(){ 
    String ret =""; 
    try {InputStream inputStream = getActivity().openFileInput(FILENAME); 
     if (inputStream != null) { 
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
      String receiveString = ""; 
      StringBuilder stringBuilder = new StringBuilder(); 
      while ((receiveString = bufferedReader.readLine()) != null) { 
       stringBuilder.append(receiveString);} 
      inputStream.close(); 
      ret = stringBuilder.toString(); 
     }} 

    catch (FileNotFoundException e) { 
     ret="0"; 
    } catch (IOException e) { 
     ret="0";} 
    return ret;} 

public void writetofile(String content){ 
    FileOutputStream outputStream; 
    try {outputStream = getActivity().openFileOutput(FILENAME, Context.MODE_PRIVATE); 
     outputStream.write(content.getBytes()); 
     outputStream.close(); 
    } catch (Exception e) { 
     e.printStackTrace();}} 
} 

我的XML文件

fragment_acceleration.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp"> 

    <com.hultan.formler.ResizableImageView 
     android:src="@drawable/f1a2" 
     android:id="@+id/imageView1" 
     android:layout_width="match_parent" 
     android:layout_height="120dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:scaleType="centerCrop" /> 
<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/r2view" 
    android:layout_alignLeft="@+id/imageView1" 
    android:layout_alignRight="@+id/imageView1" 
    android:layout_below="@+id/imageView1" 
    android:id="@+id/sView" > 

    <TextView 
     android:text="@string/acceleration" 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/text_color" 
     android:textSize="20sp" /> 
    </ScrollView> 

<LinearLayout 
    android:layout_alignLeft="@+id/imageView1" 
    android:layout_alignRight="@+id/imageView1" 
    android:id="@+id/r2view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true"> 
    <Button 
     android:id="@+id/addbutton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/buttonstylegreen" 
     android:text="@string/add" 
     android:textSize="20sp" /> 
    </LinearLayout> 
</RelativeLayout> 

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.hultan.formler" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="18" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" 
    android:largeHeap="true" > 

    <activity 
     android:name="com.hultan.formler.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

惱人的是,這個確切的代碼工作,我加入之前完全正常的30幾乎相同fragments(這是一個導航,帶你到這個特定的片段)從未被使用過。

+1

看到錯誤'java.lang.OutOfMemoryError' ...我想你使用的是高分辨率的'Bitmap' ... –

+0

@GopalRao所以我使用的圖像應該更小? – HultBullt

+0

是的...嘗試使用更小的「位圖」或9貼片圖像... –

回答

2

@GopalRao解決了問題!我的圖像顯然太大了。我將它們全部調整到大小的40%,現在它工作得很好!