ArrayAdapter 要求资源 ID 为 TextView,ListView 无法转换为 TextView

Posted

技术标签:

【中文标题】ArrayAdapter 要求资源 ID 为 TextView,ListView 无法转换为 TextView【英文标题】:ArrayAdapter requires the resource ID to be a TextView, ListView cannot be cast to TextView 【发布时间】:2015-10-15 21:41:33 【问题描述】:

我创建了两个片段,一个片段使用 listview 显示项目,一个片段在单击另一个片段的项目时使用 textview 显示文本。在主要活动中,我使用这两个片段一个低于一个。但我有两个错误,即

1)java.lang.IllegalStateException: ArrayAdapter 要求资源 ID 为 TextView 2) java.lang.ClassCastException: android.widget.ListView 不能转换为 android.widget.TextView

MainActivity.java

package perception.vicky.fragments;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements platformsfragment.ListSelectionListener 

public static String[] titles;
public static String[] details;
private detailsfragment detailsreference;

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

    // Get the string arrays with the titles and details
    titles = getResources().getStringArray(R.array.Titles);
    details = getResources().getStringArray(R.array.Quotes);

    // Get a reference to the detailsfragment
    detailsreference = (detailsfragment) getFragmentManager().findFragmentById(R.id.details);


@Override
public boolean onCreateOptionsMenu(Menu menu) 
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;


@Override
public boolean onOptionsItemSelected(MenuItem item) 
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) 
        return true;
    

    return super.onOptionsItemSelected(item);


// Called when the user selects an item in the platformsfragment
@Override
public void onListSelection(int index) 

    if (detailsreference.getShownIndex() != index) 

        // Tell the detailsfragment to show the quote string at position index
        detailsreference.showQuoteAtIndex(index);
    



platformsfragment.java

package perception.vicky.fragments;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
* Created by Vicky on 7/23/2015.
*/
public class platformsfragment extends ListFragment 

private ListSelectionListener platformlistener = null;
private View view;

// Callback interface that allows this Fragment to notify the MainActivity when
// user clicks on a List Item
public interface ListSelectionListener 
    void onListSelection(int index);


// Called when the user selects an item from the List
@Override
public void onListItemClick(ListView l, View v, int position, long id) 
    super.onListItemClick(l, v, position, id);

    // Indicates the selected item has been checked
    getListView().setItemChecked(position, true);

    // Inform the MainActivity that the item in position pos has been selected
    platformlistener.onListSelection(position);


@Override
public void onAttach(Activity activity) 
    super.onAttach(activity);

    try 

        // Set the ListSelectionListener for communicating with the MainActivity
        platformlistener = (ListSelectionListener) activity;

     catch (ClassCastException e) 
        throw new ClassCastException(activity.toString()
                + " must implement OnArticleSelectedListener");
    


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 

    view = inflater.inflate(R.layout.platformsfragment, container, false);
    return view;


@Override
public void onActivityCreated(Bundle savedInstanceState) 
    super.onActivityCreated(savedInstanceState);
    // Set the list choice mode to allow only one selection at a time
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    // Set the list adapter for the ListView
    // Discussed in more detail in the user interface classes lesson
    setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.platformsfragment, android.R.id.list, MainActivity.titles));


detailsfragment.java

package perception.vicky.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
* Created by Vicky on 7/23/2015.
*/
public class detailsfragment extends Fragment 

private TextView detailstext=null;
private int DetailsArrayLen;
private int Indx = -1;
private View view;

public detailsfragment() 


public int getShownIndex() 
    return Indx;


// Show the Quote string at position newIndex
public void showQuoteAtIndex(int newIndex) 
    if (newIndex < 0 || newIndex >= DetailsArrayLen)
        return;
    Indx = newIndex;
    detailstext.setText(String.valueOf(MainActivity.details[Indx]));


@Override
public void onAttach(Activity activity) 
    super.onAttach(activity);


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



// Called to create the content view for this Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 

    // Inflate the layout defined in detailsfragment.xml
    // The last parameter is false because the returned view does not need to be attached to the container ViewGroup
    view = inflater.inflate(R.layout.detailsfragment, container, false);
    return view;


@Override
public void onActivityCreated(Bundle savedInstanceState) 
    super.onActivityCreated(savedInstanceState);

    //ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),R.layout.detailsfragment,R.id.textId,MainActivity.details);
    //setListAdapter(adapter);
    detailstext = (TextView) getActivity().findViewById(R.id.textId);
    DetailsArrayLen = MainActivity.details.length;



activity_main.xml

<?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_
android:layout_ android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<fragment
    android:layout_
    android:layout_
    class="perception.vicky.fragments.platformsfragment"
    tools:layout="@layout/platformsfragment"
    android:id="@+id/platforms"
    android:layout_above="@+id/details"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<fragment
    android:layout_
    android:layout_
    class="perception.vicky.fragments.detailsfragment"
    tools:layout="@layout/detailsfragment"
    android:id="@+id/details"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="false"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="false" />

</RelativeLayout>

platformsfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_>

<ListView
    android:layout_
    android:layout_
    android:id="@android:id/list"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:padding="5dip"
    android:textSize="32sp"/>

</RelativeLayout>

detailsfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_>

<TextView
    android:layout_
    android:id="@+id/textId"
    android:layout_
    android:padding="5dip"
    android:textSize="32sp"
    android:layout_alignParentTop="true" />

</RelativeLayout>

【问题讨论】:

您必须在ArrayAdapter 中提供TextView 的资源ID 才能绑定其值。您提供了android.R.id.list。将其替换为应在platformsfragment.xml 中定义的TextView 【参考方案1】:

我认为问题出在这里:

new ArrayAdapter<String>(getActivity(), R.layout.platformsfragment, android.R.id.list, MainActivity.titles)

尝试将此代码替换为:

new ArrayAdapter<String>(getActivity(), R.layout.listitem, android.R.id.text1, MainActivity.titles)

listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical" >

<TextView
    android:id="@+id/textview"
    android:layout_
    android:layout_ >
</TextView></LinearLayout>

【讨论】:

谢谢先生,这两个错误已经消失了,但现在我又遇到了两个错误:java.lang.RuntimeException: Unable to start activity ComponentInfoperception.vicky.fragments/perception.vicky.fragments. MainActivity: android.view.InflateException: Binary XML file line #9: Error inflating class fragment, 原因:android.view.InflateException: Binary XML file line #9: Error inflating class fragment @peter 得到了输出先生,我将 android.R.id.text1 更改为 R.id.textview 并且我在 onCreateView 中使用了现有的返回方法先生,非常感谢先生,我一直在尝试这个两天... :-)【参考方案2】:

你这行写错了:

new ArrayAdapter<String>(getActivity(), R.layout.platformsfragment, android.R.id.list, MainActivity.titles)

如果您查阅文档,您会发现您将错误的值传递给构造函数。检查此链接:ArrayAdapter

第二个参数是列表项的布局用户的id,而不是片段的布局。 第三个参数是 TextView 的 id,在列表项布局中,应该用于显示每个项目的信息,而不是您要填充的列表的 id。

【讨论】:

谢谢先生,这两个错误已经消失了,但现在我又遇到了两个错误:java.lang.RuntimeException: Unable to start activity ComponentInfoperception.vicky.fragments/perception.vicky.fragments. MainActivity: android.view.InflateException: Binary XML file line #9: Error inflating class fragment, Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment 这意味着在尝试扩充您的平台片段时发生了异常。我建议你调试它的生命周期方法并跟踪错误。 得到了输出先生,我将 android.R.id.text1 更改为 R.id.textview 并且我在 onCreateView 中使用了现有的返回方法先生,非常感谢先生,我一直在尝试这个两天... :-)

以上是关于ArrayAdapter 要求资源 ID 为 TextView,ListView 无法转换为 TextView的主要内容,如果未能解决你的问题,请参考以下文章

ArrayAdapter 要求资源 ID 为 TextView,ListView 无法转换为 TextView

ArrayAdapter 要求 ID 为 TextView 错误

如何修复此 ArrayAdapter 需要资源 ID 为 TextView

两种配置adpater的方式

如何使用 ArrayAdapter 为 ListView 编写自定义过滤器

连续按钮的自定义arrayadapter和onclicklistener