ListView onItemClick"遗憾的是myapp已停止"错误android

时间:2017-05-26 22:59:52

标签: java android listview

我在我的布局上使用了ListView但是在ItemClick事件中它显示了这个错误:"遗憾的是myappname已停止"

这是我的java代码booking.java OnCreate方法:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.booking);
        lv = (ListView) findViewById(R.id.list);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(), "Hello " + i, Toast.LENGTH_SHORT).show();
            }
        });

    }

我的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/booking"

    >


    <TextView
        android:id="@+id/textView"
        android:layout_width="302dp"
        android:layout_height="25dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="32dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="60dp"
        android:text="TextView"
        android:textAlignment="textStart"
        android:textColor="#f0f0f0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.011" />

    <ListView
        android:id="@+id/list"
        android:layout_width="302dp"
        android:layout_height="398dp"
        android:layout_marginLeft="32dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginTop="11dp"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_editor_absoluteY="97dp" />
</android.support.constraint.ConstraintLayout>

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

尝试使用setOnItemSelectedListener而不是onItemClickedListener。

答案 1 :(得分:0)

试试这个

listView1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {

                String item = ((TextView)view).getText().toString();

                Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

            }
        });