ArrayAdapter需要资源ID为TextView,为什么?

时间:2016-04-10 18:50:59

标签: android listview android-arrayadapter

突破已经有几个主题,但找不到问题的答案 - 出了什么问题? 我的MenuFragment.java(使用ListView)

public class MenuFragment extends ListFragment {

    private ArrayList<Recipe.RecipeType> mUsedRecipeType;
    private ArrayList<Recipe> mRecipes;
    private ListView mRecipeTypeListView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.menu_fragment, container, false);

        mRecipeTypeListView = (ListView) view.findViewById(android.R.id.list);
        mRecipeTypeListView.setEmptyView(view.findViewById(android.R.id.empty));


        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRecipes = RecipeLab.get(getActivity()).getRecipes();
        mUsedRecipeType = new ArrayList<>();
        calculateTypes();

        Log.d(MainActivity.TAG, "length arr " + mUsedRecipeType.size());

        ArrayAdapter<Recipe.RecipeType> adapter = new ArrayAdapter<Recipe.RecipeType>(getActivity(),
                android.R.layout.simple_list_item_1, mUsedRecipeType);
        setListAdapter(adapter);

//        RecipeTypeAdapter recipeTypeAdapter = new RecipeTypeAdapter(mUsedRecipeType);
//        setListAdapter(recipeTypeAdapter);

        setRetainInstance(true);
    }

//    private class RecipeTypeAdapter extends ArrayAdapter<Recipe.RecipeType> {
//
//        public RecipeTypeAdapter(ArrayList<Recipe.RecipeType> recipeTypes) {
//            super(getActivity(), 0, recipeTypes);
//        }
//
//        @Override
//        public View getView(int position, View convertView, ViewGroup parent) {
//            if (convertView == null)
//                convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_recipe_type, parent, false);
//
//            Recipe.RecipeType recipeType = getItem(position);
//
//            TextView titleTextView = (TextView) convertView.findViewById(R.id.recipe_type_list_item_titleTextView);
//            titleTextView.setText(recipeType.toString());
//
//            return convertView;
//        }
//    }

    private void calculateTypes() {

        for (Recipe recipe: mRecipes) {
            if (!mUsedRecipeType.contains(recipe.getRecipeType()))
                mUsedRecipeType.add(recipe.getRecipeType());
        }
    }
}

我的list_item_recipe_type.xml(使用TextView)

<?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:background="@drawable/background_activated_recipe_type"
              android:orientation="vertical">

    <TextView
        android:id="@+id/recipe_type_list_item_titleTextView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:textStyle="bold"
        android:gravity="center"/>

</LinearLayout>

我有一个错误堆栈

04-10 21:44:15.206 29961-29980/? I/OpenGLRenderer: Initialized EGL, version 1.4
04-10 21:44:15.263 29961-29961/? E/ArrayAdapter: You must supply a resource ID for a TextView
04-10 21:44:15.263 29961-29961/? D/AndroidRuntime: Shutting down VM
04-10 21:44:15.265 29961-29961/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.luxary_team.simpleeat, PID: 29961
                                                   java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
                                                       at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:393)
                                                       at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
                                                       at android.widget.AbsListView.obtainView(AbsListView.java:2346)
                                                       at android.widget.ListView.makeAndAddView(ListView.java:1876)
                                                       at android.widget.ListView.fillDown(ListView.java:702)
                                                       at android.widget.ListView.fillFromTop(ListView.java:763)
                                                       at android.widget.ListView.layoutChildren(ListView.java:1671)
                                                       at android.widget.AbsListView.onLayout(AbsListView.java:2148)
                                                       at android.view.View.layout(View.java:16636)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1207)
                                                       at android.view.View.layout(View.java:16636)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                       at android.view.View.layout(View.java:16636)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
                                                       at android.view.View.layout(View.java:16636)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                       at android.view.View.layout(View.java:16636)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                       at android.view.View.layout(View.java:16636)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                       at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
                                                       at android.view.View.layout(View.java:16636)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                       at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                       at android.os.Looper.loop(Looper.java:148)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                    Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
                                                       at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
                                                       at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369) 
                                                       at android.widget.AbsListView.obtainView(AbsListView.java:2346) 
                                                       at android.widget.ListView.makeAndAddView(ListView.java:1876) 
                                                       at android.widget.ListView.fillDown(ListView.java:702) 
                                                       at android.widget.ListView.fillFromTop(ListView.java:763) 
                                                       at android.widget.ListView.layoutChildren(ListView.java:1671) 
                                                       at android.widget.AbsListView.onLayout(AbsListView.java:2148) 
                                                       at android.view.View.layout(View.java:16636) 
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                                                       at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1207) 
                                                       at android.view.View.layout(View.java:16636) 
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 
                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273) 
                                                       at android.view.View.layout(View.java:16636) 
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                                                       at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435) 
                                                       at android.view.View.layout(View.java:16636) 
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 
                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273) 
                                                       at android.view.View.layout(View.java:16636) 
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) 
                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) 
                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) 
                                                       at android.view.View.layout(View.java:16636) 
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 
                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273) 
                                                       at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678) 
                                                       at android.view.View.layout(View.java:16636) 
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437) 
                                                       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171) 
                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931) 
                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 
                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) 
                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
                                                       at android.view.Choreographer.doFrame(Choreographer.java:606) 
                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
                                                       at android.os.Handler.handleCallback(Handler.java:739) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                       at android.os.Looper.loop(Looper.java:148) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

首先,从代码注释中可以看出,我想使用我的ArrayAdapter,但在收到错误后决定使用该标准,然后是标准的标准list_item.xml。但是所有的时间都存在同一个错误。请告诉我,出了什么问题?

2 个答案:

答案 0 :(得分:1)

默认情况下,ArrayAdapter会尝试查找带有id="@android:id/text1"的文本视图,并在其中设置数组中的文本值。如果指定自定义布局,则必须确保textview还具有android:id="@android:id/text1"或使用指定textViewResourceId的ArrayAdapter的其他构造函数。所以改为

<?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:background="@drawable/background_activated_recipe_type"
          android:orientation="vertical">

<TextView
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:textStyle="bold"
    android:gravity="center"/>

</LinearLayout>

应该工作

答案 1 :(得分:-1)

你需要一个带有textview的自定义布局,可以尝试使用分拣机ID和offcourse android.R.layout.simple_list_item_1做它R.layout。(id_of_textview)