的ListView onItemClick“不幸的myapp已停止”错误的Android

问题描述:

我使用的ListView我的布局,但在itemClick事件就说明这个错误:“不幸的是myappname已停止”的ListView onItemClick“不幸的myapp已停止”错误的Android

这是我的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> 

谁能帮我解决这个问题?

+0

请给我一个日志详细 – redAllocator

试试这个

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(); 

      } 
     }); 

尝试setOnItemSelectedListener而不是onItemClickedListener。