我正在尝试创建一个简单的频率提问应用程序。第一个活动应该显示字符串数组“问题”中所有问题的列表视图。第二个活动应该在单击问题时打开,并显示字符串数组“答案”中问题的答案。(我遵循了关于在活动之间传递数据的指南。)
该程序编译并且在eclipse中没有显示任何错误,但是当我尝试运行它时,我不断收到“程序意外关闭”错误。不确定是什么问题。
提问活动:
public class FreqActivity extends ListActivity {
String[] ans;
String[] questions;
View tempView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ans = getResources().getStringArray(R.array.answers);
questions = getResources().getStringArray(R.array.questions);
setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
R.array.questions, R.layout.main));
}
public void onListItemClick(ListView l, View v, int position, long id) {
//...
}
}
日志错误:
09-21 20:21:31.722: D/dalvikvm(333): GC_EXTERNAL_ALLOC freed 51K, 53% free 2564K/5379K, external 2108K/2137K, paused 66ms
09-21 20:21:31.792: E/ArrayAdapter(333): You must supply a resource ID for a TextView
09-21 20:21:31.792: D/AndroidRuntime(333): Shutting down VM
09-21 20:21:31.792: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-21 20:21:31.913: E/AndroidRuntime(333): FATAL EXCEPTION: main
09-21 20:21:31.913: E/AndroidRuntime(333): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.ListView.makeAndAddView(ListView.java:1745)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.ListView.fillDown(ListView.java:670)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.ListView.fillFromTop(ListView.java:727)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.ListView.layoutChildren(ListView.java:1598)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.view.View.layout(View.java:7175)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.view.View.layout(View.java:7175)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.view.View.layout(View.java:7175)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.view.View.layout(View.java:7175)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.os.Looper.loop(Looper.java:123)
09-21 20:21:31.913: E/AndroidRuntime(333): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-21 20:21:31.913: E/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 20:21:31.913: E/AndroidRuntime(333): at java.lang.reflect.Method.invoke(Method.java:507)
09-21 20:21:31.913: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-21 20:21:31.913: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-21 20:21:31.913: E/AndroidRuntime(333): at dalvik.system.NativeStart.main(Native Method)
09-21 20:21:31.913: E/AndroidRuntime(333): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
09-21 20:21:31.913: E/AndroidRuntime(333): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
09-21 20:21:31.913: E/AndroidRuntime(333): ... 26 more
09-21 20:23:04.262: I/Process(333): Sending signal. PID: 333 SIG: 9