用于arrayadapter的SetAdapter正在崩溃

时间:2013-05-03 02:47:03

标签: java android crash runtime-error android-arrayadapter

每当我的setadapter函数被调用时,我的应用程序都会崩溃,我不知道为什么,有人可以帮忙吗?我正在运行的模拟器只是说应用程序意外停止了。

这是我的主要

public class MainActivity extends Activity {

    private ListView listView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Ride ride_data[] = new Ride[]
        {
                new Ride("LA Trip", "la man"),
                new Ride("SD Trip", "sd man"),
                new Ride("SF Trip", "sf man")
        };

        RideAdapter adapter = new RideAdapter(this,
                R.layout.listview_item_row,ride_data);

        listView1 = (ListView)findViewById(R.id.listView1);

        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        listView1.addHeaderView(header);

        listView1.setAdapter(adapter);

    }

}

这是我的适配器

public class RideAdapter extends ArrayAdapter<Ride>{

    Context context;
    int layoutResourceId;
    Ride data[] = null;

    public RideAdapter(Context context, int layoutResourceId, Ride[] data)
    {
        super(context,layoutResourceId,data);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        RideHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new RideHolder();
            holder.rideName = (TextView)row.findViewById(R.id.txtRideName);
            holder.personName = (TextView)row.findViewById(R.id.txtPersonName);

            row.setTag(holder);
        }
        else
        {
            holder = (RideHolder)row.getTag();
        }

        Ride ride = data[position];
        holder.rideName.setText(ride.rideName);
        holder.personName.setText(ride.personName);

        return row;

    }

    public static class RideHolder
    {
        TextView rideName;
        TextView personName;
    }
}

这是我的骑车课

public class Ride {
    public String rideName;
    public String personName;

    public Ride()
    {

    }

    public Ride(String rideName, String personName)
    {
        super();
        this.rideName=rideName;
        this.personName=personName;
    }
}

最后这是我的列表项目视图

<?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="fill_parent"
    android:orientation="vertical"
    android:padding="10dp"
     >

    <Textview
        android:id="@+id/txtRideName"
        android:layout_width ="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        />
    <Textview
        android:id="@+id/txtPersonName"
        android:layout_width ="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        />

</LinearLayout>

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

    <Textview
            android:id="@+id/txtRideName"
            android:layout_width ="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center_horizontal"
            />
    <Textview
            android:id="@+id/txtPersonName"
            android:layout_width ="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            />

Textview应该是TextView而不是Textview。

你应该学会在logcat中阅读Crash。

android.view.InflateException:二进制XML文件行#8:错误导致类Textview

    05-03 11:18:29.399: E/AndroidRuntime(12890): FATAL EXCEPTION: main
    05-03 11:18:29.399: E/AndroidRuntime(12890): android.view.InflateException: Binary XML file line #8: Error inflating class Textview
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at com.example.hello1.RideAdapter.getView(RideAdapter.java:30)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.AbsListView.obtainView(AbsListView.java:2468)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.ListView.makeAndAddView(ListView.java:1775)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.ListView.fillDown(ListView.java:678)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.ListView.fillFromTop(ListView.java:739)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.ListView.layoutChildren(ListView.java:1628)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.AbsListView.onLayout(AbsListView.java:2303)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.View.layout(View.java:14063)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.ViewGroup.layout(ViewGroup.java:4655)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.View.layout(View.java:14063)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.ViewGroup.layout(ViewGroup.java:4655)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.View.layout(View.java:14063)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.ViewGroup.layout(ViewGroup.java:4655)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.View.layout(View.java:14063)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.ViewGroup.layout(ViewGroup.java:4655)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.View.layout(View.java:14063)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.ViewGroup.layout(ViewGroup.java:4655)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2017)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1838)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1131)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4611)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.Choreographer.doFrame(Choreographer.java:525)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.os.Handler.handleCallback(Handler.java:615)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.os.Handler.dispatchMessage(Handler.java:92)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.os.Looper.loop(Looper.java:137)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.app.ActivityThread.main(ActivityThread.java:4898)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at java.lang.reflect.Method.invokeNative(Native Method)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at java.lang.reflect.Method.invoke(Method.java:511)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at dalvik.system.NativeStart.main(Native Method)
    05-03 11:18:29.399: E/AndroidRuntime(12890): Caused by: java.lang.ClassNotFoundException: android.view.Textview
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
    05-03 11:18:29.399: E/AndroidRuntime(12890):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)