首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android异常

Android异常
EN

Stack Overflow用户
提问于 2013-01-04 21:29:50
回答 4查看 266关注 0票数 1

我尝试实现一个项目,在这个项目中,我将在列表视图中加载许多图片。我用一个懒惰的实例实现了这一点。我还用一个适配器和下面的方法扩充了这个列表:

代码语言:javascript
运行
复制
 static class ViewHolder {
    ImageView imageView;
    TextView brand;
    TextView text;
    TextView cost;
    TextView saleCost;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    ViewHolder holder;
    // Set the product.
    Product currentProduct = getItem(position);

    if(convertView ==null){
        Activity activity = (Activity)getContext();
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();

        // Extract the ImageView from the row.
        holder.imageView = (ImageView) convertView.findViewById(R.id.image);

        holder.text = (TextView) convertView.findViewById(R.id.text);
        holder.brand = (TextView) convertView.findViewById(R.id.brand);

        holder.cost = (TextView) convertView.findViewById(R.id.cost);
        holder.saleCost = (TextView) convertView.findViewById(R.id.saleCost);

        convertView.setTag(holder);

    }else {
        holder = (ViewHolder) convertView.getTag();
    } 

    //here I set the values

list_item:

代码语言:javascript
运行
复制
<?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="horizontal"
android:padding="10dp" >

<!-- Image Thumbnail -->

<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp" />

<!-- Name, Brand, and Cost -->

<LinearLayout
    android:id="@+id/nameAlign"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:paddingLeft="10dip" >

    <TextView
        android:id="@+id/brand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/cost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/saleCost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
  </LinearLayout>

</LinearLayout>

I会出现以下错误:

代码语言:javascript
运行
复制
 01-03 17:31:13.055: E/AndroidRuntime(22860): FATAL EXCEPTION: main
 01-03 17:31:13.055: E/AndroidRuntime(22860): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nn/com.nn.HomeScreen}: android.view.InflateException:    Binary XML file line #22: Error inflating class <unknown>
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.os.Handler.dispatchMessage(Handler.java:99)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.os.Looper.loop(Looper.java:130)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.app.ActivityThread.main(ActivityThread.java:3683)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at java.lang.reflect.Method.invokeNative(Native Method)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at java.lang.reflect.Method.invoke(Method.java:507)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at dalvik.system.NativeStart.main(Native Method)
 01-03 17:31:13.055: E/AndroidRuntime(22860): Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class <unknown>
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.view.LayoutInflater.createView(LayoutInflater.java:518)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at com.actionbarsherlock.internal.ActionBarSherlockCompat.setContentView(ActionBarSherlockCompat.java:857)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at com.actionbarsherlock.app.SherlockActivity.setContentView(SherlockActivity.java:218)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at com.nn.HomeScreen.onCreate(HomeScreen.java:205)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   ... 11 more
 01-03 17:31:13.055: E/AndroidRuntime(22860): Caused by: java.lang.reflect.InvocationTargetException
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at java.lang.reflect.Constructor.constructNative(Native Method)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.view.LayoutInflater.createView(LayoutInflater.java:505)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   ... 24 more
 01-03 17:31:13.055: E/AndroidRuntime(22860): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.graphics.Bitmap.nativeCreate(Native Method)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.content.res.Resources.loadDrawable(Resources.java:1709)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.widget.ImageView.<init>(ImageView.java:118)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   at android.widget.ImageView.<init>(ImageView.java:108)
 01-03 17:31:13.055: E/AndroidRuntime(22860):   ... 27 more

有人能给我解释一下日志文件吗?因为我不明白主要问题是什么。

EN

Stack Overflow用户

回答已采纳

发布于 2013-01-04 21:36:42

请按照以下方法进行尝试。,还提供了有关listitem.xml的详细信息

代码语言:javascript
运行
复制
public View getView(final int position, View convertView, ViewGroup parent) {
            System.out.println("***********IngetView************");
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            //View Listview;
            final ViewHolder viewHolder ;

            if (convertView == null) {
                //viewHolder = new ViewHolder();
                //Listview = new View(context);
                viewHolder = new ViewHolder();
                // get layout from gridlayout.xml
                convertView = inflater.inflate(R.layout.list_item, null);

            } else {
                //Listview = (View) convertView;
                viewHolder = (ViewHolder) convertView.getTag();

            }
票数 1
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14158116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档