单击列表视图中的组标题时出错

时间:2018-08-03 08:28:37

标签: android listview

首先,抱歉手指粗大。

我有一个header.xmldetail.xml根据数据填充我的列表视图。现在我注意到Listivew点击事件。如果我单击ListView中的“组头”,则会发生错误。我该如何解决?

这是我的代码。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    TextView pos_remarks = (view.findViewById(R.id.list_remarks));
   final String l_remarks = pos_remarks.getText().toString();
});

对不起,忘记了错误

Process: com.main, PID: 5539
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
        at com.main.fragment.fragment_plan$1.onItemClick(fragment_plan.java:71)
        at android.widget.AdapterView.performItemClick(AdapterView.java:315)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1209)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3331)
        at android.widget.AbsListView.onTouchUp(AbsListView.java:4403)
        at android.widget.AbsListView.onTouchEvent(AbsListView.java:4103)
        at android.view.View.dispatchTouchEvent(View.java:10062)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2776)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2408)

Header.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:id="@+id/line_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:orientation="horizontal"
        android:paddingBottom="8sp"
        android:paddingLeft="25sp"
        android:paddingRight="25sp"
        android:clickable="false"
        android:paddingTop="8sp">

        <TextView
            android:id="@+id/list_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/tahoma"
            android:text="Date"
            android:textSize="15sp"
            android:clickable="false"
            android:textStyle="bold" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

`details.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="4.5"
        android:orientation="vertical">

        <TextView
            android:id="@+id/list_remarks"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:ellipsize="end"
            android:fontFamily="@font/tahoma"
            android:paddingLeft="5sp"
            android:singleLine="true"
            android:text=""
            android:textSize="14sp" />
    </LinearLayout>
</LinearLayout>`

1 个答案:

答案 0 :(得分:0)

尝试一下

if(pos_remarks!=null){
    final String l_remarks = pos_remarks.getText().toString();
}