1

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

 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

列表项:

<?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>

我犯了以下错误:

 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

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

4

4 回答 4

1

主要问题是

 java.lang.OutOfMemoryError: bitmap size exceeds VM budget

只有一定数量的内存分配给您的应用程序用于加载位图。

于 2013-01-04T13:32:20.203 回答
1

请尝试如下方法。并提供有关 listitem.xml 的详细信息

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();

            }
于 2013-01-04T13:36:41.690 回答
0

您的位图太大:

01-03 17:31:13.055:E/AndroidRuntime(22860):原因:java.lang.OutOfMemoryError: 位图大小超出 VM 预算

于 2013-01-04T13:33:42.637 回答
-1

日志中有“Caused by:”语句,主要问题是这样的:

Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class <unknown>

这意味着您在 xml 文件中,并且 xml inflater 不知道Something对应的类。

于 2013-01-04T13:31:37.907 回答