我在创建ListActivity时出错

时间:2014-12-26 11:02:18

标签: java android

错误是 -

541-541/com.example.anabil.globaloilenergy E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

这是我的代码:

MainList.java


package com.example.anabil.globaloilenergy.MainMenuList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.example.anabil.globaloilenergy.R;

import java.lang.reflect.Array;


public class MainList extends ArrayAdapter<String> {
    private final Context context;
    //private final int res;
    private final String [] list;


    public MainList(Context context, String[] objects) {
        super(context, R.layout.activity_main_list, objects);
        this.context = context;
        //this.res = res;
        this.list = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.activity_main_list,parent,false );
        View row = inflater.inflate(R.layout.activity_main_list , parent , false);
        TextView tv = (TextView) row.findViewById(R.id.title);
        ImageView iv = (ImageView) row.findViewById(R.id.nextArrow);
        String option = list[position];
        if (option.equals("item1")){
            tv.setText("About Us");
            iv.setImageResource(R.drawable.gonext); }
        else if (option.equals("item2")){
            tv.setText("Contact Us");
            iv.setImageResource(R.drawable.gonext); }
        else if (option.equals("item3")){
            tv.setText("Contact Us");
            iv.setImageResource(R.drawable.gonext); }
        else if (option.equals("item4")){
            tv.setText("Contact Us");
            iv.setImageResource(R.drawable.gonext); }
        else if (option.equals("item5")){
            tv.setText("Contact Us");
            iv.setImageResource(R.drawable.gonext); }
        return super.getView(position, convertView, parent);
    }
}

activity_main_list.xml

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:orientation="horizontal"
    android:weightSum="100"
    tools:context=".MainListActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/nextArrow"
        />


</LinearLayout>

Main.java

package com.example.anabil.globaloilenergy;


import android.app.ListActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.anabil.globaloilenergy.MainMenuList.MainList;


public class Main extends ListActivity {
    static String [] Menu = {"item1","item2","item3","item4","item5"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new MainList(getBaseContext(), Menu));
    }

    @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }
}

2 个答案:

答案 0 :(得分:0)

进行以下更改

convertView = inflater.inflate(R.layout.activity_main_list , parent , false);  

return convertView;

而不是

return super.getView(position, convertView, parent);

答案 1 :(得分:0)

像这样修改您的适配器类

package com.example.anabil.globaloilenergy.MainMenuList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.example.anabil.globaloilenergy.R;

import java.lang.reflect.Array;


public class MainList extends ArrayAdapter<String> {
    private final Context context;
    //private final int res;
    private final String [] list;
 LayoutInflater inflater;

    public MainList(Context context, String[] objects) {
        super(context, R.layout.activity_main_list, objects);
        this.context = context;
        //this.res = res;
        this.list = objects;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

       View row = convertView;
        if (row == null)
            row = inflater.inflate(R.layout.activity_main_list, null);

        TextView tv = (TextView) row.findViewById(R.id.title);
        ImageView iv = (ImageView) row.findViewById(R.id.nextArrow);
        String option = list[position];
        if (option.equals("item1")){
            tv.setText("About Us");
            iv.setImageResource(R.drawable.gonext); }
        else if (option.equals("item2")){
            tv.setText("Contact Us");
            iv.setImageResource(R.drawable.gonext); }
        else if (option.equals("item3")){
            tv.setText("Contact Us");
            iv.setImageResource(R.drawable.gonext); }
        else if (option.equals("item4")){
            tv.setText("Contact Us");
            iv.setImageResource(R.drawable.gonext); }
        else if (option.equals("item5")){
            tv.setText("Contact Us");
            iv.setImageResource(R.drawable.gonext); }
        return row;
    }
}