[英]Unable to call the fragment with imageView in android
今天是我第一次使用這些片段。 我能夠制作一個利用該片段的簡單應用程序,但是,有一個我無法理解或尚未注意到的問題。 問題是當我使用imageView添加片段時,應用程序崩潰了。 當我使用簡單的文本或其他小部件時,一切正常。 我在下面設置了示例代碼。
我這樣稱呼Activity1中的片段。
android.support.v4.app.FragmentManager fragmentManager2 = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
TestFragment fragment = new TestFragment();
fragmentTransaction2.add(R.id.fragment_container, fragment,"HELLO");
fragmentTransaction2.commit();
這是填充片段的布局
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
這是擴展片段的簡單Activity2類
public class Activity2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/** Inflating the layout for this fragment **/
View v = inflater.inflate(R.layout.fragment_class2, null);
return v;
}
}
這是擴展片段的類的布局。 ps:此處的imageview會導致錯誤,但是,片段類中的設置可能是問題所在。 我需要顯示imageView。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/imaew2"
android:src="@drawable/playimage2"
/>
</LinearLayout>
這些是我得到的錯誤,我不確定這些錯誤的含義,但是該錯誤指向片段
在com.trymeagain.Activity2.onCreateView(Activity2.java:13)在android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java) :955),位於android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138),位於android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740),位於android.support.v4.app。 android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:458)上的FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
Added new Error
05-07 17:56:20.701 3173-3173/com.trymeagain E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 74649612 byte allocation with 1048576 free bytes and 63MB until OOM"
05-07 17:56:20.703 3173-3173/com.trymeagain D/skia﹕ --- allocation failed for scaled bitmap
05-07 17:56:20.704 3173-3173/com.trymeagain D/AndroidRuntime﹕ Shutting down VM
05-07 17:56:20.704 3173-3173/com.trymeagain E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.trymeagain, PID: 3173
android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.trymeagain.PlusOneFragment.onCreateView(Activity2.java:13)
您在imageview中遇到異常,其中image playimage2太大,堆內存不足以分配...
解-
情況1:如果需要在高分辨率設備上增加尺寸
如果可以修補playimage2,則嘗試減小圖像的大小。.您可以使用諸如photoshop之類的工具進行修補,然后對圖像進行修補
case2:如果大小需要保持不變,如xml 50dp * 50dp中所述
只是嘗試減小圖像的大小
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.