申请人关闭

时间:2012-11-20 13:31:21

标签: android-layout

  

我无法理解logcat中的这个错误

     

logcat错误:

11-20 18:45:42.125:E / AndroidRuntime(458):致命异常:主要 11-20 18:45:42.125:E / AndroidRuntime(458):java.lang.ClassCastException:android.widget.ImageView 11-20 18:45:42.125:E / AndroidRuntime(458):at com.example.orderplacemnet.Product_Details $ MyProductAdapter.getView(Product_Details.java:75) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.widget.AbsListView.obtainView(AbsListView.java:1315) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.widget.ListView.measureHeightOfChildren(ListView.java:1198) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.widget.ListView.onMeasure(ListView.java:1109) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.view.View.measure(View.java:8171) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.view.View.measure(View.java:8171) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.view.View.measure(View.java:8171) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.widget.LinearLayout.measureVertical(LinearLayout.java:526) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.view.View.measure(View.java:8171) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.view.View.measure(View.java:8171) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.view.ViewRoot.performTraversals(ViewRoot.java:801) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.os.Handler.dispatchMessage(Handler.java:99) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.os.Looper.loop(Looper.java:123) 11-20 18:45:42.125:E / AndroidRuntime(458):在android.app.ActivityThread.main(ActivityThread.java:4627) 11-20 18:45:42.125:E / AndroidRuntime(458):at java.lang.reflect.Method.invokeNative(Native Method) 11-20 18:45:42.125:E / AndroidRuntime(458):at java.lang.reflect.Method.invoke(Method.java:521) 11-20 18:45:42.125:E / AndroidRuntime(458):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 11-20 18:45:42.125:E / AndroidRuntime(458):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-20 18:45:42.125:E / AndroidRuntime(458):at dalvik.system.NativeStart.main(Native Method)

package com.example.orderplacemnet;


import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Product_Details extends Activity
{ 
            String[] text = { "One", "Two", "Three", "Four", "Five"};

                int[] image = { R.drawable.rose_five, R.drawable.rose, R.drawable.rose_two,
                                                R.drawable.rose_three, R.drawable.rose_four};



                public void onCreate(Bundle savedInstanceState)
                {

                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_product__details);

                                ListView l1 = (ListView) findViewById(R.id.ListView01);
                                l1.setAdapter(new MyProductAdapter(text,image));

                }
 class MyProductAdapter extends BaseAdapter
{

                                String[] data_text;
                                int[] data_image;

                                MyProductAdapter()
                                {
                                                data_text = null;
                                                data_image = null;
                                }

                                MyProductAdapter(String[] text, int[] image)
                                {
                                                data_text = text;
                                                data_image = image;
                                }

                                public int getCount()
                                {
                                                return data_text.length;
                                }

                                public String getItem(int position)
                                {
                                                return null;
                                }

                                public long getItemId(int position)
                                {
                                                return position;
                                }

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

                                                LayoutInflater inflater = getLayoutInflater();
                                                View row;

                                                row = inflater.inflate(R.layout.listview, parent, false);

                                                TextView textview = (TextView) row.findViewById(R.id.TextView01);
                                                ImageView imageview = (ImageView) row.findViewById(R.id.ImageView01);

                                                textview.setText(data_text[position]);
                                                imageview.setImageResource(data_image[position]);

                                                return (row);

                                }
    }
}
  

这是我的listrow布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fff200"
    android:gravity="left|center"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingTop="5dp" >


    <TextView
        android:id="@+id/TextView01"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="10dp"
        android:textSize="20dp"
        android:textStyle="bold" >

    </TextView>

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.76"
        android:contentDescription="@string/productlist" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我在您的代码中发现了2个问题。 首先,ImageView中的layout_weight属性。您也应该指定TextView layout_weight。我建议你将imageview权重设置为3,将textview设置为1,这样就不需要十进制数字了。

第二个问题是纯猜:在适配器的getView方法中,尝试在没有父级的情况下进行膨胀:

row = inflater.inflate(R.layout.listview, null);

希望有所帮助