0

我正在尝试使用 Java 使用 ListViews。我正在为安卓开发一个应用程序。当我在下面运行代码时,它会使我的应用程序崩溃并且无法运行。这是我的代码

package com.zafar.myapp
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Myapp extends ListActivity {
CheckBox cbSetAlarm;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    setListAdapter(new ArrayAdapter<String>(this, R.layout.main, Food));

    ListView list = getListView();
    list.setTextFilterEnabled(true);
    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(), Toast.LENGTH_SHORT).show();
        }

    });
}

static final String[] Food = new String[] {
    "Ice Cream"
};
}

你能帮我在哪里犯错吗?为什么它不运行?

提前致谢

更新 Logcat

这是我的日志猫

 05-01 15:16:45.505: D/dalvikvm(21776): GC_EXTERNAL_ALLOC freed 50K, 50% free 2725K/5379K, external 0K/0K, paused 51ms
05-01 15:16:45.640: D/dalvikvm(21776): GC_EXTERNAL_ALLOC freed 12K, 49% free 2759K/5379K, external 504K/518K, paused 55ms
05-01 15:16:45.645: E/ArrayAdapter(21776): You must supply a resource ID for a TextView
05-01 15:16:45.645: D/AndroidRuntime(21776): Shutting down VM
05-01 15:16:45.645: W/dalvikvm(21776): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-01 15:16:45.655: E/AndroidRuntime(21776): FATAL EXCEPTION: main
05-01 15:16:45.655: E/AndroidRuntime(21776): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.AbsListView.obtainView(AbsListView.java:1598)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ListView.makeAndAddView(ListView.java:1778)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ListView.fillDown(ListView.java:701)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ListView.fillFromTop(ListView.java:758)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ListView.layoutChildren(ListView.java:1629)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.AbsListView.onLayout(AbsListView.java:1428)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.View.layout(View.java:7228)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.View.layout(View.java:7228)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.View.layout(View.java:7228)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.View.layout(View.java:7228)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1148)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.os.Looper.loop(Looper.java:130)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at java.lang.reflect.Method.invokeNative(Native Method)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at java.lang.reflect.Method.invoke(Method.java:507)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at dalvik.system.NativeStart.main(Native Method)
05-01 15:16:45.655: E/AndroidRuntime(21776): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
05-01 15:16:45.655: E/AndroidRuntime(21776):    ... 26 more
05-01 15:16:45.505: D/dalvikvm(21776): GC_EXTERNAL_ALLOC freed 50K, 50% free 2725K/5379K, external 0K/0K, paused 51ms
05-01 15:16:45.640: D/dalvikvm(21776): GC_EXTERNAL_ALLOC freed 12K, 49% free 2759K/5379K, external 504K/518K, paused 55ms
05-01 15:16:45.645: E/ArrayAdapter(21776): You must supply a resource ID for a TextView
05-01 15:16:45.645: D/AndroidRuntime(21776): Shutting down VM
05-01 15:16:45.645: W/dalvikvm(21776): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-01 15:16:45.655: E/AndroidRuntime(21776): FATAL EXCEPTION: main
05-01 15:16:45.655: E/AndroidRuntime(21776): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.AbsListView.obtainView(AbsListView.java:1598)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ListView.makeAndAddView(ListView.java:1778)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ListView.fillDown(ListView.java:701)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ListView.fillFromTop(ListView.java:758)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ListView.layoutChildren(ListView.java:1629)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.AbsListView.onLayout(AbsListView.java:1428)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.View.layout(View.java:7228)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.View.layout(View.java:7228)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.View.layout(View.java:7228)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.View.layout(View.java:7228)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1148)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.os.Looper.loop(Looper.java:130)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at java.lang.reflect.Method.invokeNative(Native Method)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at java.lang.reflect.Method.invoke(Method.java:507)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at dalvik.system.NativeStart.main(Native Method)
05-01 15:16:45.655: E/AndroidRuntime(21776): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
05-01 15:16:45.655: E/AndroidRuntime(21776):    ... 26 more
05-01 15:16:45.505: D/dalvikvm(21776): GC_EXTERNAL_ALLOC freed 50K, 50% free 2725K/5379K, external 0K/0K, paused 51ms
05-01 15:16:45.640: D/dalvikvm(21776): GC_EXTERNAL_ALLOC freed 12K, 49% free 2759K/5379K, external 504K/518K, paused 55ms
05-01 15:16:45.645: E/ArrayAdapter(21776): You must supply a resource ID for a TextView
05-01 15:16:45.645: D/AndroidRuntime(21776): Shutting down VM
05-01 15:16:45.645: W/dalvikvm(21776): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-01 15:16:45.655: E/AndroidRuntime(21776): FATAL EXCEPTION: main
05-01 15:16:45.655: E/AndroidRuntime(21776): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.AbsListView.obtainView(AbsListView.java:1598)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ListView.makeAndAddView(ListView.java:1778)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ListView.fillDown(ListView.java:701)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ListView.fillFromTop(ListView.java:758)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ListView.layoutChildren(ListView.java:1629)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.AbsListView.onLayout(AbsListView.java:1428)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.View.layout(View.java:7228)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.View.layout(View.java:7228)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.View.layout(View.java:7228)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.View.layout(View.java:7228)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1148)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.os.Looper.loop(Looper.java:130)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at java.lang.reflect.Method.invokeNative(Native Method)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at java.lang.reflect.Method.invoke(Method.java:507)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-01 15:16:45.655: E/AndroidRuntime(21776):    at dalvik.system.NativeStart.main(Native Method)
05-01 15:16:45.655: E/AndroidRuntime(21776): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
05-01 15:16:45.655: E/AndroidRuntime(21776):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
05-01 15:16:45.655: E/AndroidRuntime(21776):    ... 26 more
4

2 回答 2

1

这是完整的代码:

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class Test![enter image description here][1]Activity extends ListActivity {

CheckBox cbSetAlarm;
static final String[] Food = new String[] { "Choose alarm for battery charged completed" };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // setContentView(R.layout.main);

    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Food));

    ListView list = getListView();
    list.setTextFilterEnabled(true);
    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0 , View arg1 , int arg2 ,
                long arg3) {

            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(),
                    ((TextView) arg1).getText(), Toast.LENGTH_SHORT).show();
        }

    });
}
}

截屏:

在此处输入图像描述

于 2012-05-01T11:57:08.010 回答
1

1)因为您正在扩展 ListActivity 尝试覆盖 onListItemClick 而不是 new OnItemClickListener():

@Override
protected void onListItemClick(ListView parent, View view, int position,
        long id) {
    //...
}

2)或者你可能需要清理你的项目:Eclipse Menu-> Project -> Clean

于 2012-05-01T11:59:19.583 回答