Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 按钮setOnclickListener无法使用可展开的listview_Android - Fatal编程技术网

Android 按钮setOnclickListener无法使用可展开的listview

Android 按钮setOnclickListener无法使用可展开的listview,android,Android,我有两个xml用于可扩展列表视图和listitem 1) list.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_bg" android:orientat

我有两个xml用于可扩展列表视图和listitem

1) list.xml
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_bg"
android:orientation="vertical" >

<ExpandableListView
    android:id="@+id/lvExp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

   </LinearLayout>

2)list_group.xml
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent">

  <TextView
    android:id="@+id/lbl"
    android:layout_width="wrap_content"
    />

<Button
    android:id="@+id/btn_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:text="Login"
    android:focusable="false" />

  </RelativeLayout>
所有的工作都很完美,但当我为那个按钮编写setOnClickListener时,我发现了下面的错误

Caused by: java.lang.NullPointerException
    at com.example.test.list.onCreate(list.java:45)

那么我如何解决这个问题呢?

您需要在listview的适配器中处理列表项按钮的onClickListener。

将您的按钮变量编辑为btn\u print而不是btn\u login。。Btn_登录是您的xml按钮id

按钮btn_打印

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

 btn_print = (Button) findViewById(R.id.btn_login);

    btn_print.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
});

在适配器类中,您必须访问按钮并将onclicklistener设置为“btn_print”,其中位于xml中。为什么使用这个?抱歉,这是btn_login.list.java在哪里?仍然有异常???
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

 btn_print = (Button) findViewById(R.id.btn_login);

    btn_print.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
});