Java – IllegalStateException: arrayadapter requires the resource ID to be textview
•
Android
I ran my code once. It's good. I ran it again. Now it crashes every time. Logcat gave me this:
04-11 03:15:57.293: D/dalvikvm(344): GC_EXTERNAL_ALLOC freed 68K, 52% free 2588K/5379K, external 1907K/2137K, paused 65ms
04-11 03:15:57.497: E/ArrayAdapter(344): You must supply a resource ID for a TextView
04-11 03:15:57.497: D/AndroidRuntime(344): Shutting down VM
04-11 03:15:57.497: W/dalvikvm(344): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-11 03:15:57.573: E/AndroidRuntime(344): FATAL EXCEPTION: main
04-11 03:15:57.573: E/AndroidRuntime(344): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.ListView.onMeasure(ListView.java:1127)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.os.Looper.loop(Looper.java:123)
04-11 03:15:57.573: E/AndroidRuntime(344): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-11 03:15:57.573: E/AndroidRuntime(344): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 03:15:57.573: E/AndroidRuntime(344): at java.lang.reflect.Method.invoke(Method.java:507)
04-11 03:15:57.573: E/AndroidRuntime(344): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-11 03:15:57.573: E/AndroidRuntime(344): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-11 03:15:57.573: E/AndroidRuntime(344): at dalvik.system.NativeStart.main(Native Method)
04-11 03:15:57.573: E/AndroidRuntime(344): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
04-11 03:15:57.573: E/AndroidRuntime(344): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
04-11 03:15:57.573: E/AndroidRuntime(344): ... 35 more
04-11 03:17:31.694: I/Process(344): Sending signal. PID: 344 SIG: 9
I have three activities:
package com.androidbook.triviaquiz;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class QuizMenuActivity extends QuizActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ListView menuList = (ListView) findViewById(R.id.listView1);
String[] items = { getResources().getString(R.string.itemplay),
getResources().getString(R.string.itemscores),
getResources().getString(R.string.itemsettings),
getResources().getString(R.string.itemhelp) };
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items);
menuList.setAdapter(adapt);
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
// Note: if the list was built "by hand" the id Could be used.
// As-is, though, each item has the same id
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
if (strText.equalsIgnoreCase(getResources().getString(R.string.itemplay))) {
// Launch the Game Activity
startActivity(new Intent(QuizMenuActivity.this, QuizGameActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemhelp))) {
// Launch the Help Activity
startActivity(new Intent(QuizMenuActivity.this, QuizHelpActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemsettings))) {
// Launch the Settings Activity
startActivity(new Intent(QuizMenuActivity.this, QuizSettingsActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemscores))) {
// Launch the scores Activity
startActivity(new Intent(QuizMenuActivity.this, QuizscoresActivity.class));
}
}
});
}
}
This:
package com.androidbook.triviaquiz;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class QuizMenuActivity extends QuizActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ListView menuList = (ListView) findViewById(R.id.listView1);
String[] items = { getResources().getString(R.string.itemplay),
getResources().getString(R.string.itemscores),
getResources().getString(R.string.itemsettings),
getResources().getString(R.string.itemhelp) };
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items);
menuList.setAdapter(adapt);
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
// Note: if the list was built "by hand" the id Could be used.
// As-is, though, each item has the same id
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
if (strText.equalsIgnoreCase(getResources().getString(R.string.itemplay))) {
// Launch the Game Activity
startActivity(new Intent(QuizMenuActivity.this, QuizGameActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemhelp))) {
// Launch the Help Activity
startActivity(new Intent(QuizMenuActivity.this, QuizHelpActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemsettings))) {
// Launch the Settings Activity
startActivity(new Intent(QuizMenuActivity.this, QuizSettingsActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemscores))) {
// Launch the scores Activity
startActivity(new Intent(QuizMenuActivity.this, QuizscoresActivity.class));
}
}
});
}
}
And this:
package com.androidbook.triviaquiz;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class QuizGameActivity extends QuizActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.gameoptions, menu);
menu.findItem(R.id.help_menu_item).setIntent(new Intent(this, QuizHelpActivity.class));
menu.findItem(R.id.settings_menu_item).setIntent(new Intent(this, QuizSettingsActivity.class));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
startActivity(item.getIntent());
return true;
}
}
Can someone tell me what's wrong with this code? As I said before, all I do is restart it. These are the only problems you need to solve, maybe the list?
Edit: RES / layout / menu
<RelativeLayout 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: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=".QuizSplashActivity" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/th"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#000000" >
<ImageView
android:id="@+id/ImageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/quizicon" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="MAIN MENU"
android:textColor="#ffffff"
android:textSize="20pt" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/quizicon" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center">
</ListView>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
resolvent:
You must specify the textview ID used in r.layout.menu
Change this:
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items);
In this regard:
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, R.id.YOUR_TEXT_VIEW, items);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码