简体   繁体   English

无法在Android中使用imageView调用片段

[英]Unable to call the fragment with imageView in android

Today is my first time to be using the fragments. 今天是我第一次使用这些片段。 And I was able to make a simple app that utilizes the fragment, however, there is one problem that I cannot understand or hadn't noticed yet. 我能够制作一个利用该片段的简单应用程序,但是,有一个我无法理解或尚未注意到的问题。 The problem is the app crashes when I add the fragment with the imageView. 问题是当我使用imageView添加片段时,应用程序崩溃了。 Everything works fine when I use simple texts or other widgets. 当我使用简单的文本或其他小部件时,一切正常。 I've set the sample code below. 我在下面设置了示例代码。

I'm calling the fragment from Activity1 like this. 我这样称呼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();

This is the layout that fills the fragment 这是填充片段的布局

 <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
</LinearLayout>

This is the simple Activity2 class that extends the fragment 这是扩展片段的简单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;
    }
}

this is the layout for the class that extends the fragment . 这是扩展片段的类的布局。 Ps: The imageview in here causes the error, however, the settings in the fragment class may be the problem. ps:此处的imageview会导致错误,但是,片段类中的设置可能是问题所在。 I need to display the 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>

These are the error I get and I'm not sure how these errors mean, however the error is pointing to the Fragment 这些是我得到的错误,我不确定这些错误的含义,但是该错误指向片段

at com.trymeagain.Activity2.onCreateView(Activity2.java:13) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458) 在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)

you getting the exception in your imageview where your image playimage2 is too large and the heap memory is not enough to allocate ... 您在imageview中遇到异常,其中image playimage2太大,堆内存不足以分配...

solution- 解-

case1 : if size need to increase on higher resolution device 情况1:如果需要在高分辨率设备上增加尺寸

if playimage2 is possible to patch-then try to reduce the size of the image..you can use tool like photoshop to do so.and then patch the image 如果可以修补playimage2,则尝试减小图像的大小。.您可以使用诸如photoshop之类的工具进行修补,然后对图像进行修补

case2 : if size need to be constant as written in xml 50dp*50dp case2:如果大小需要保持不变,如xml 50dp * 50dp中所述

Just try to reduce the size of the image 只是尝试减小图像的大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM