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 の更新

ここに私の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 を拡張しているため、new OnItemClickListener() の代わりに onListItemClick をオーバーライドしてみてください。

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

2) または、プロジェクトをクリーンアップする必要があるかもしれません: Eclipse メニュー -> プロジェクト -> クリーン

于 2012-05-01T11:59:19.583 に答える