0

私は単純なアンドロイド ListView デモを作成しましたが、私のコードは以下のように動作しません:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="20sp"
    android:textStyle="bold" >
</TextView>

main.java

    package com.example.mylist;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ListActivity;
    import android.database.DataSetObserver;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends ListActivity {
        static final String[] list = new String[] { "Awesome", "New", "Retro",
                "Rock", "Pop", "Bollywood", "hollywood" };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_main,
                    list));
            ListView listvw = getListView();
            listvw.setTextFilterEnabled(true);

            listvw.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(),
                            ((TextView) v).getText(), Toast.LENGTH_SHORT).show();

                }
            });

        }

    }

ログキャット

07-29 14:16:21.335: E/AndroidRuntime(27347): FATAL EXCEPTION: main
07-29 14:16:21.335: E/AndroidRuntime(27347): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mylist/com.example.mylist.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.os.Looper.loop(Looper.java:137)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.ActivityThread.main(ActivityThread.java:4424)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at java.lang.reflect.Method.invokeNative(Native Method)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at java.lang.reflect.Method.invoke(Method.java:511)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at dalvik.system.NativeStart.main(Native Method)
07-29 14:16:21.335: E/AndroidRuntime(27347): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.ListActivity.onContentChanged(ListActivity.java:243)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at android.app.Activity.setContentView(Activity.java:1862)
07-29 14:16:21.335: E/AndroidRuntime(27347):    at com.example.mylist.MainActivity.onCreate(MainActivity.java:20)

助けてください...!

4

4 に答える 4

1

コードでこの行をコメントするだけです。それから私はそれがうまくいくことを願っています

// setContentView(R.layout.activity_main);

onclickを呼び出さないでください この方法を試してください

listvw.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // When clicked, show a toast with the TextView text
        Toast.makeText(getApplicationContext(),
        ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
    }
});

この2つをインポートします

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
于 2013-07-29T07:08:53.140 に答える
0
    package com.ListView;

    public class ListViewActivity extends Activity implements OnItemSelectedListener{
   /** Called when the activity is first created. */
    private String[] 
    Name={"Destop","CPU","Pendrive","Mouse","Keyboard","Harddisk","Cardreader","MotherBord"};
    ListView l1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    l1=(ListView)findViewById(R.id.listView1);
    l1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Name));
    l1.setOnItemSelectedListener(this);
   }

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

    Toast.makeText(this, Name[arg2].toString(), Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    Toast.makeText(this, "Item Not Selected", Toast.LENGTH_LONG).show();
         }
     }
于 2013-07-29T07:36:09.740 に答える
0
   <?xml version="1.0" encoding="utf-8" ?> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> 
    <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" /> 
  </LinearLayout>
于 2013-07-29T07:39:58.383 に答える