如何修复此 ArrayAdapter 需要资源 ID 为 TextView
Posted
技术标签:
【中文标题】如何修复此 ArrayAdapter 需要资源 ID 为 TextView【英文标题】:How to fix this ArrayAdapter requires the resource ID to be a TextView 【发布时间】:2016-03-20 20:02:23 【问题描述】:我是 android 新手,我正在尝试使用 json 开发一个带有 gridview 和 listview 的应用程序。通过 json 我在 gridview 中显示了一组图像,并根据 gridview 的位置,我在下一页列表视图中显示结果。这是我的 Sub_Category.java(第二页 java 文件)
package com.javatechig.gridviewexample;
public class Sub_Categories extends ActionBarActivity
private ArrayList<GridItem> SubCatgyData;
private static final String TAG = Sub_Categories.class.getSimpleName();
public String TAG1 = "Value for ID";
public String TAG2 = "Value for subCategory";
public String TAG4 = "Value for subCategory in main method";
public String TAG3 = "DONE";
public String categoryID;
private ProgressBar mProgressBar;
private ListView listView;
public String subURL = "http://www.prateektechnosoft.com/justcall/cities/gridsubcategories/chennai/";
public String URLwithID;
private ListAdapter mListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub__categories);
categoryID = getIntent().getStringExtra("ID");
Log.e(TAG1, categoryID);
SubCatgyData = new ArrayList<>();
URLwithID = subURL + categoryID;
mProgressBar = (ProgressBar) findViewById(R.id.progressBar2);
Log.e(TAG3, "Before Execute");
new AsyncHttpTask().execute(URLwithID);
Log.d(TAG4, String.valueOf(SubCatgyData.size()));
Log.e(TAG3, "After Execute");
mProgressBar.setVisibility(View.VISIBLE);
listView=(ListView) findViewById(R.id.listView2);
ArrayAdapter adapter = new ArrayAdapter(Sub_Categories.this,android.R.layout.simple_list_item_2,SubCatgyData);
listView.setAdapter(adapter);
public class AsyncHttpTask extends AsyncTask<String, Void, Integer>
@Override
protected Integer doInBackground(String... params)
Integer result = 0;
try
// Create Apache HttpClient
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(new HttpGet(params[0]));
int statusCode = httpResponse.getStatusLine().getStatusCode();
// 200 represents HTTP OK
if (statusCode == 200)
String response = streamToString(httpResponse.getEntity().getContent());
parseResult(response);
result = 1; // Successful
else
result = 0; //"Failed
catch (Exception e)
Log.d(TAG, e.getLocalizedMessage());
runOnUiThread(new Runnable()
public void run()
listView = (ListView) findViewById(R.id.listView2);
ArrayAdapter adapter = new ArrayAdapter(Sub_Categories.this, android.R.layout.list_content, SubCatgyData);
listView.setAdapter(adapter);
);
Log.e(TAG3, "Done in do background methid");
return result;
@Override
protected void onPostExecute(Integer result)
// Download complete. Lets update UI
if (result == 0)
Toast.makeText(Sub_Categories.this, "Failed to fetch data!", Toast.LENGTH_SHORT).show();
//Hide progressbar
mProgressBar.setVisibility(View.GONE);
Log.e(TAG3, "Done in onPostExecute methid");
String streamToString(InputStream stream) throws IOException
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
String line;
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
// Close stream
if (null != stream)
stream.close();
Log.e(TAG3, "Done in streamTOString methid");
return result;
private void parseResult(String result)
try
//JSONObject response = new JSONObject(result);
JSONArray posts = new JSONArray(result);
GridItem item;
for (int i = 0; i < posts.length(); i++)
JSONObject post = posts.optJSONObject(i);
String catid = post.optString("suB_CATEGORYNAME");
item = new GridItem();
item.setID(catid);
SubCatgyData.add(item);
Log.d(TAG2, String.valueOf(SubCatgyData.size()));
catch (JSONException e)
e.printStackTrace();
Log.e(TAG3, "Done in parseResult methid");
return;
这是我的布局文件。
<?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: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.javatechig.gridviewexample.Sub_Categories">
<ListView
android:layout_
android:layout_
android:id="@+id/listView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ProgressBar
android:layout_
android:layout_
android:id="@+id/progressBar2"
android:layout_centerInParent="true"
android:visibility="gone"/>
</RelativeLayout>
这是我的日志。我无法修复这个错误。请帮我。
12-15 11:44:33.528 25575-25575/com.javatechig.gridviewexample E/ArrayAdapter: You must supply a resource ID for a TextView
12-15 11:44:33.528 25575-25575/com.javatechig.gridviewexample D/AndroidRuntime: Shutting down VM
12-15 11:44:33.529 25575-25575/com.javatechig.gridviewexample E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.javatechig.gridviewexample, PID: 25575
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.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
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.FrameLayout 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.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
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 适配器=new ArrayAdapter(Sub_Categories.this, android.R.layout.list_content, android.R.id.text1, SubCatgyData);而不是 ArrayAdapter 适配器 = new ArrayAdapter(Sub_Categories.this, android.R.layout.list_content, SubCatgyData) 【参考方案1】:由于您使用的是默认的 ListView 和 ArrayAdapter,并且您只有一个数据列表,即 SubCatgyData,您应该使用 android.R.layout.simple_list_item_1 而不是 android.R .layout.simple_list_item_2.
两者的区别是android.R.layout.simple_list_item_1里面只有一个TextView,而android.R.layout.simple_list_item_2里面有两个TextView。
ArrayAdapter adapter = new ArrayAdapter(Sub_Categories.this,android.R.layout.simple_list_item_1,SubCatgyData);
【讨论】:
如果我的回答解决了您的疑问,您能否将此答案标记为正确。【参考方案2】:您需要覆盖 [ArrayAdapter.getView(int, View, ViewGroup)](http://developer.android.com/reference/android/widget/ArrayAdapter.html#getView(int, android.view.View, android.view.ViewGroup))。见http://developer.android.com/reference/android/widget/ArrayAdapter.html
要使用 TextViews 以外的东西来显示数组,例如 ImageViews,或者要让 toString() 结果之外的一些数据填充视图,请覆盖 getView(int, View, ViewGroup) 以返回您的视图类型想要。
【讨论】:
【参考方案3】:为适配器创建自定义布局。您正在使用 android 提供的一种。查看您用于布局的 android 前缀。
【讨论】:
以上是关于如何修复此 ArrayAdapter 需要资源 ID 为 TextView的主要内容,如果未能解决你的问题,请参考以下文章
错误:ArrayAdapter 要求资源 ID 为 TextView