我正在编写我的第一个Android应用程序,我遇到了一个问题:在我尝试打开一个新的Activity之后,应用程序关闭了。
这是活动代码:
package com.WNF;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.ListActivity;
import android.content.Intent;
public class Actiemenu extends ListActivity{
String[] array = { "","henk","henk2" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.actiemenu);
setListAdapter(new ArrayAdapter<String> (this, R.layout.actiemenu, array));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if (position == 0) {
Intent een = new Intent(Actiemenu.this, Acties.class);
startActivity(een);
}
else if (position == 1) {
Intent twee = new Intent(Actiemenu.this, Acties2.class);
startActivity(twee);
}
}
});
getIntent();
}
}
这是logcat的日志:
06-13 06:50:13.920: E/AndroidRuntime(335): FATAL EXCEPTION: main
06-13 06:50:13.920: E/AndroidRuntime(335): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.ListView.makeAndAddView(ListView.java:1745)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.ListView.fillDown(ListView.java:670)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.ListView.fillFromTop(ListView.java:727)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.ListView.layoutChildren(ListView.java:1598)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.view.View.layout(View.java:7175)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.view.View.layout(View.java:7175)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.view.View.layout(View.java:7175)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.view.View.layout(View.java:7175)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.view.View.layout(View.java:7175)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.os.Looper.loop(Looper.java:123)
06-13 06:50:13.920: E/AndroidRuntime(335): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-13 06:50:13.920: E/AndroidRuntime(335): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 06:50:13.920: E/AndroidRuntime(335): at java.lang.reflect.Method.invoke(Method.java:507)
06-13 06:50:13.920: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-13 06:50:13.920: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-13 06:50:13.920: E/AndroidRuntime(335): at dalvik.system.NativeStart.main(Native Method)
06-13 06:50:13.920: E/AndroidRuntime(335): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
06-13 06:50:13.920: E/AndroidRuntime(335): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
06-13 06:50:13.920: E/AndroidRuntime(335): ... 30 more
答案 0 :(得分:2)
只需更仔细地查看您的异常,就可以得出您的活动引发异常的答案:ArrayAdapter requires the resource ID to be a TextView
。因此,您必须将代码更改为:setListAdapter(new ArrayAdapter<String> (this, R.id.yourTextViewId, array));
您可以在此处找到有关ArrayAdapter的更多信息。
答案 1 :(得分:0)
在android中,ArrayAdapter是预定义的适配器,它已经定义了它的get函数。因此,它需要一些prededinfed id,如“text:is for text view。所以它提供了一些预定义的XML布局。所以要么使用它们,要么......”
因此,如果想要使用自己的ID,只需使用自定义适配器扩展ArrayAdapter并覆盖getview函数......