如何在Android

时间:2017-08-16 01:25:00

标签: android gridview out-of-memory kotlin

我是Android新手,我在the official android site遇到了有关如何创建GridView图片的教程。我使用kotlin作为首选语言。在扩展BaseAdapter的适配器类中,我正在尝试按照教程中的说明设置imageview layoutparams,但是我收到一条错误,指出GridView.LayoutParams是未解析的引用。以下是该类的完整代码:

class ImageAdapter(val context: Context) : BaseAdapter() {
    // references to our images
    val items: IntArray = intArrayOf(R.drawable.cat, R.drawable.daft_punk, R.drawable.gun, R.drawable.harvestors, R.drawable.intel, R.drawable.maserati, R.drawable.porsche, R.drawable.serveroom, R.drawable.spiderman, R.drawable.watch_dog)
    override fun getItem(p0: Int): Any {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getItemId(p0: Int): Long {
        return 0
    }

    override fun getCount(): Int {
        return items.size
    }

    // create a new ImageView for each item referenced by the Adapter
    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        var imageView: ImageView
        if (p1 == null) {
      // if it's not recycled, initialize some attributes
            imageView= ImageView(context)
            imageView.layoutParams= GridView.LayoutParams(85,85)
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
            imageView.setPadding(8, 8, 8, 8)
        } else {
            imageView=p1 as ImageView
        }
        imageView.setImageResource(items[p0])
        return imageView
    }

}

错误位于getView行的imageView.layoutParams=GridView.LayoutParams(85,85)方法中。 LayoutParams为红色,表示错误,当我将鼠标悬停在其上时,我收到消息:

  

未解决的参考:LayoutParams

如何成功设置这些LayoutParams?因为没有它们GridView中的图像显示在它们的全高,我需要一个小图像,就像图库应用程序在我们的手机中显示它们一样。

后续问题:

当我用imageView.layoutParams=GridView.LayoutParams(85,85)替换imageView.layoutParams=AbsListView.LayoutParams(85,85)时,我收到javaOutOfMemory错误。 以下是完整堆栈跟踪:

08-16 06:58:11.412 4543-4543/gallerie.classmite.com.imageviewer E/dalvikvm-heap: Out of memory on a 18662416-byte allocation.
08-16 06:58:11.431 4543-4543/gallerie.classmite.com.imageviewer E/AndroidRuntime: FATAL EXCEPTION: main

Process: gallerie.classmite.com.imageviewer, PID: 4543                                                                                  java.lang.OutOfMemoryError                                                                                                                                          
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)                                                                                      
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609                                                                                      
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)                                                                                     
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)                                                                                     
at android.content.res.Resources.loadDrawable(Resources.java:2166)                                                                                     
at android.content.res.Resources.getDrawable(Resources.java:710)                                                                                      
at android.widget.ImageView.resolveUri(ImageView.java:638)                                                                                      
at android.widget.ImageView.setImageResource(ImageView.java:367)                                                                                     
at gallerie.classmite.com.imageviewer.ImageAdapter.getView(ImageAdapter.kt:32)                                                                                       
at android.widget.AbsListView.obtainView(AbsListView.java:2372)                                                                                      
at android.widget.GridView.makeAndAddView(GridView.java:1345)                                                                                      
at android.widget.GridView.makeRow(GridView.java:345)                                                                                     
at android.widget.GridView.fillDown(GridView.java:287)                                                                                      
at android.widget.GridView.fillFromTop(GridView.java:421)                                                                                      
at android.widget.GridView.layoutChildren(GridView.java:1233)                                                                                      
at android.widget.AbsListView.onLayout(AbsListView.java:2166)                                                                                      
at android.view.View.layout(View.java:15127)                                                                                      
at android.view.ViewGroup.layout(ViewGroup.java:4862)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)                                                                                      
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)                                                                                     
at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)                                                                                     
at android.view.View.layout(View.java:15127)                                                                                      
at android.view.ViewGroup.layout(ViewGroup.java:4862)                                                                                     
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)                                                                                      
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)                                                                                      
at android.view.View.layout(View.java:15127)                                                                                      
at android.view.ViewGroup.layout(ViewGroup.java:4862)                                                                                      
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)                                                                                      
at android.view.View.layout(View.java:15127)                                                                                      
at android.view.ViewGroup.layout(ViewGroup.java:4862)                                                                                     
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)                                                                                      
at android.widget.FrameLayout.onLayout(FrameLayout.java:450)                                                                                     
at android.view.View.layout(View.java:15127)                                                                                     
at android.view.ViewGroup.layout(ViewGroup.java:4862)                                                                                     
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2315)                                                                                     
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2021)                                                                                     
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)                                                                                     
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6221)                                                                                     
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)                                                                                      
at android.view.Choreographer.doCallbacks(Choreographer.java:591)                                                                                     
at android.view.Choreographer.doFrame(Choreographer.java:560)                                                                                     
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)                                                                                     
at android.os.Handler.handleCallback(Handler.java:808)                                                                                      
at android.os.Handler.dispatchMessage(Handler.java:103)                                                                                      
at android.os.Looper.loop(Looper.java:193)                                                                                      
at android.app.ActivityThread.main(ActivityThread.java:5315)                                                                                      
at java.lang.reflect.Method.invokeNative(Native Method)                                                                                     
at java.lang.reflect.Method.invoke(Method.java:515)                                                                                   
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)                                                                                    
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)                                                                                     
at dalvik.system.NativeStart.main(Native Method)

只有当我在手机中运行应用程序时才会出现错误,但是当我在模拟器中运行应用程序时,在我上面进行替换后它会完美运行。

2 个答案:

答案 0 :(得分:3)

你可以试试这个:

<div class="clear property left">

    <div class="margintop">

        ...             
        <div class="propName"> #Here is the address and price info
            <a href="/Cebu-City-Philippines-Land-for-sale/pid/119211639" onclick="WC('119211639', '-1');"> &nbsp; Cebu City, Philippines <BR/> 1185000, PHP</a>
        </div>


            <div>PAYMENT SCHEMES:\r\rReservation Fee : P20,000 (non refundable)\r\r1. SCHEME 1\rCash - 100% with the following discounts\r* 10% for 7 days payment\r* 8%...&nbsp;</div>

        ...

    </div>
    <div class="clear"></div>
</div>
在检索孩子时,imageView.layoutParams = AbsListView.LayoutParams(85, 85) 使用

AbsListView.LayoutParams&#39; GridView

由于静态成员可以由子类in Java继承。致电LayoutParams时,实际上是GridView.LayoutParams。但是,在Kotlin中,你不能以同样的方式去做。在子类之外,您只能使用超类的名称引用超类的伴随对象的成员。

后续问题的更新:

AbsListView.LayoutParams错误表示您正在将大图片加载到OutOfMemory。为了防止这种情况,您必须在显示图像之前缩小图像,参考此tutorial。您还可以使用某些图像加载框架(例如Glide)来显示图像。

建议阅读:Strange out of memory issue while loading an image to a Bitmap object

答案 1 :(得分:0)

不要像你那样设置布局参数

正确的方法是 imageParams =(GridView.LayoutParams)imageView.getLayoutParams();

在上面的行“GridView”应该替换为imageView的父视图

快乐编码.. :)