your content must have a listview whose id attribute is ' android.r.id.list ' .

蹲街弑〆低调 提交于 2019-11-28 13:54:09

compare your code nothing difference....

mainActivity

package com.example.testdemo;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    private TextView text;
    private static final String[] items = { "test", "test1", "test2", "test3",
            "test4", "test5", "test6" };

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_);



        setListAdapter(new ArrayAdapter<String>(this, R.layout.lable,
                R.id.label, items));
        text = (TextView) findViewById(R.id.selection);

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        text.setText("" + items[position]);
        super.onListItemClick(l, v, position, id);
    }


    }
}

activity_main.xml

<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:id="@+id/selection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ListView

        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" />

</LinearLayout>

row.xml

<?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="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dip"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Please extend Activity instead of ListActivity,

Then get its view from layout file

ListView listView = (ListView) findViewById(R.id.listView);

Then call listView.setAdapter(/*agruments*/); method

instead of calling setListAdapter().

you have to get the instance of a listview like

ListView lv=getListView();

Change this line

 android:id="@android:id/list"

to

 android:id="@+id/list"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!