我最近修改了我的应用。我添加了大约30个不同的片段(但同时使用的片段不超过2个)。我不知道这是否会耗尽内存并导致问题。无论如何,只要我打开这个片段,应用程序就会崩溃。我用log来找出问题所在。但我不知道 问题是什么。
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
(这是导航到这个特定片段的导航)之前,这个确切的代码工作得很好。
答案 0 :(得分:2)
@GopalRao解决了这个问题!我的照片显然太大了。我将它们的大小调整到大约40%的大小,现在它的工作完好无损!