我运行了一次我的代码很好,我再次运行它,现在它每次都崩溃了. LogCat给了我这个:
04-11 03:15:57.293: D/dalvikvm(344): GC_EXTERNAL_ALLOC freed 68K, 52% free 2588K/5379K, external 1907K/2137K, paused 65ms04-11 03:15:57.497: E/ArrayAdapter(344): You must supply a resource ID for a TextVIEw04-11 03:15:57.497: D/AndroIDRuntime(344): Shutting down VM04-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: main04-11 03:15:57.573: E/AndroIDRuntime(344): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextVIEw04-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.relativeLayout04-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 more04-11 03:17:31.694: I/Process(344): Sending signal. PID: 344 SIG: 9
我有三个活动:
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)); } } }); }}
这个:
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)); } } }); }}
还有这个:
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; }}
有人能告诉我这段代码有什么问题吗?就像我之前说过的,我所做的就是重启它.这些是您需要解决的唯一问题,也许是清单?
编辑: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>
解决方法:
您必须指定在R.layout.menu中使用的TextVIEw ID
改变这个:
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items);
对此:
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, R.ID.YOUR_TEXT_VIEW, items);
总结 以上是内存溢出为你收集整理的java – IllegalStateException:ArrayAdapter要求资源ID为TextView全部内容,希望文章能够帮你解决java – IllegalStateException:ArrayAdapter要求资源ID为TextView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)