在android中运行自定义列表视图时出错

时间:2016-03-18 14:27:22

标签: android android-studio

执行自定义列表视图文件时出错。这是完整的错误描述:

03-18 19:08:38.974 16659-16659/? I/art: Late-enabling -Xcheck:jni
03-18 19:08:38.993 16659-16669/? I/art: Debugger is no longer active
03-18 19:08:39.108 16659-16677/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-18 19:08:39.114 16659-16659/? D/Atlas: Validating map...
03-18 19:08:39.136 16659-16677/? I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017_msm8916_64_LA.BR.1.1.3_RB1__release_AU (Iac7c2e2837)
                                               OpenGL ES Shader Compiler Version: E031.25.03.04
                                               Build Date: 06/11/15 Thu
                                               Local Branch: mybranch10882158
                                               Remote Branch: quic/LA.BR.1.1.3_rb1.16
                                               Local Patches: NONE
                                               Reconstruct Branch: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017 + 26a3cba + 6f69ea6 + 8bc2bc8 + 649fcde + a52cccf + dbf281f + 15f0bf8 + 8d02f76 + 9b2cb1a + 25f3b04 + 7cd8c84 + b54906e + 675fd74 + 7c22ef4 +  NOTHING
03-18 19:08:39.137 16659-16677/? I/OpenGLRenderer: Initialized EGL, version 1.4
03-18 19:08:39.154 16659-16677/? D/OpenGLRenderer: Enabling debug mode 0
03-18 19:08:39.205 16659-16659/? I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@145b271c time:11764592
03-18 19:08:42.103 16659-16659/com.example.aurora.soctomatic I/Timeline: Timeline: Activity_launch_request id:com.example.aurora.soctomatic time:11767491
03-18 19:08:42.184 16659-16659/com.example.aurora.soctomatic I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@1c8c8867 time:11767572
03-18 19:08:44.417 16659-16659/com.example.aurora.soctomatic I/Timeline: Timeline: Activity_launch_request id:com.example.aurora.soctomatic time:11769804
03-18 19:08:44.439 16659-16659/com.example.aurora.soctomatic D/AndroidRuntime: Shutting down VM
03-18 19:08:44.440 16659-16659/com.example.aurora.soctomatic E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.aurora.soctomatic, PID: 16659
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aurora.soctomatic/com.example.aurora.soctomatic.Ecommerce}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
                                                                                   at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                                                Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
                                                                                   at android.app.ListActivity.onContentChanged(ListActivity.java:243)
                                                                                   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:433)
                                                                                   at android.app.Activity.setContentView(Activity.java:2145)
                                                                                   at com.example.aurora.soctomatic.Ecommerce.onCreate(Ecommerce.java:47)
                                                                                   at android.app.Activity.performCreate(Activity.java:5990)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
                                                                                   at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:135) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

主文件:

package com.example.aurora.soctomatic;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;


import java.util.List;

public class Ecommerce extends ListActivity {

    ListView listview;

    String[] name = {
            "Samsung Galaxy S7",
            "I-phone 5s",
            "Hair Dryer",
            "Ceiling Fan"
    };

    String[] brand = {
            "Samsung",
            "Apple",
            "Philips",
            "Luminous"
    };

    Integer[] price = {
            49000,
            18900,
            750,
            880
    };

    Integer[] ivicon = {
            R.drawable.s7,
            R.drawable.iphone5s,
            R.drawable.hair,
            R.drawable.fan
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.print("bdcjhsgbjhcfdgbjhfgbjhgfjhdgjfhgvjhgfvcjhgbjhfvgejhvcfjhghfedghjkfvghfgdjhkgh");
        setContentView(R.layout.activity_ecommerce);

        System.out.print("1bdcjhsgbjhcfdgbjhfgbjhgfjhdgjfhgvjhgfvcjhgbjhfvgejhvcfjhghfedghjkfvghfgdjhkgh");

        ProductAdapter adapter = new ProductAdapter(this, name, brand, price, ivicon);
        System.out.print("2bdcjhsgbjhcfdgbjhfgbjhgfjhdgjfhgvjhgfvcjhgbjhfvgejhvcfjhghfedghjkfvghfgdjhkgh");
        listview = (ListView) findViewById(R.id.ecom_layout);
        System.out.print("3bdcjhsgbjhcfdgbjhfgbjhgfjhdgjfhgvjhgfvcjhgbjhfvgejhvcfjhghfedghjkfvghfgdjhkgh");
        listview.setAdapter(adapter);
        System.out.print("4bdcjhsgbjhcfdgbjhfgbjhgfjhdgjfhgvjhgfvcjhgbjhfvgejhvcfjhghfedghjkfvghfgdjhkgh");


    }

}

适配器文件:

package com.example.aurora.soctomatic;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ProductAdapter extends ArrayAdapter {

    private final Activity context;
    private final String[] name;
    private final String[] brand;
    private final Integer[] price;
    private final Integer[] ivicon;

    public ProductAdapter(Activity context,String[] name, String [] brand, Integer[] price, Integer[] ivicon ) {
        super(context, R.layout.row, name);
        this.context = context;
        this.name = name;
        this.brand = brand;
        this.price = price;
        this.ivicon = ivicon;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.row, null, true);
        TextView txtName = (TextView)rowView.findViewById(R.id.name);
        TextView txtBrand = (TextView)rowView.findViewById(R.id.brand);
        TextView txtPrice = (TextView)rowView.findViewById(R.id.price);
        ImageView img = (ImageView)rowView.findViewById(R.id.ivicon);

        txtName.setText(name[position]);
        txtBrand.setText(brand[position]);
        txtPrice.setText(price[position]);
        img.setImageResource(ivicon[position]);
        return rowView;
    }
}

辅助布局文件:

    <?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:weightSum="1"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ImageView
                    android:layout_width="140dp"
                    android:layout_height="140dp"
                    android:padding="5dp"
                    android:src="@drawable/s7"
                    android:id="@+id/ivicon" />
            </FrameLayout>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="140dp"
                android:paddingLeft="30dp"
                android:paddingRight="30dp"
                android:background="@drawable/bg_layout">


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="Product Name"
                    android:id="@+id/name"
                    android:layout_margin="5dp"
                    android:textSize="30dp"
                    android:textStyle="italic|bold"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Brand Name"
                    android:id="@+id/brand"
                    android:layout_margin="6dp"
                    android:textSize="20dp"
                    android:textStyle="italic"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="Price in Rs."
                    android:id="@+id/price"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:textSize="25dp"
                    android:textStyle="bold"
                    android:textColor="#000000"/>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp">
            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp">


                <TextView
                    android:layout_width="146dp"
                    android:layout_height="60dp"
                    android:text="+ Wishlist"
                    android:background="@drawable/bg_layout"
                    android:textAlignment="center"
                    android:textSize="20dp"
                    android:textColor="#000"
                    android:padding="15dp"
                    android:id="@+id/wish" />
            </FrameLayout>

            <TextView
                android:layout_width="230dp"
                android:layout_height="match_parent"
                android:textColor="#000000"
                android:textSize="20sp"
                android:background="@drawable/bg_layout"
                android:text="Add to Cart"
                android:id="@+id/atc"
                android:textAlignment="center"
                android:padding="15dp" />
        </LinearLayout>
    </LinearLayout>

主要布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.aurora.soctomatic.Ecommerce">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ecom_layout"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

请各位帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

根据此处的文档将Listview ID从“ecom_layout”更改为“list”:http://developer.android.com/reference/android/app/ListActivity.html您应该可以使其工作