我有一个XML布局,其中包含一个EditText,用户可以在其中输入个人名称,然后是一个按钮,它将在数据库上进行搜索,然后在其下方的ListView中显示结果。

我在OnClickListener上遇到错误:

searchButton.setOnClickListener(this);


我已阅读过需要使用其他“ OnClickListener”导入的信息吗?但是我已经更改了它,它仍然给我NullPointerException错误。

谁能告诉我如何使这种布局协同工作?

这是我的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/inputSearchName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/txtsearch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Type Name To Search:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/inputName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btnSearchName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Search" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>


我的搜索类别:

package com.example.flybase2;

import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;

// Imported OnClickListener
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;


//Extends ListActivity, Implements OnClickListener. Above Import for OnClickListener in     imports.
public class search extends ListActivity implements OnClickListener {

Button searchButton;
EditText searchName;
ListView searchedListResults;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    searchButton = (Button) findViewById(R.id.btnSearch);
    searchName = (EditText) findViewById(R.id.inputName);

    //ERROR: OnClickListener is causing a crash.
    searchButton.setOnClickListener(this);

}


@Override
public void onClick(View clickedSearchButton) {

String searchedName = searchName.getText().toString();

searchedListResults = (ListView)findViewById(android.R.id.list);

DBHandler DBsearchRef = new DBHandler(this, null, null);

DBHandler search = new DBHandler(this, null, null);

search.open();
Cursor cursor = search.searchOnName(searchedName);

startManagingCursor(cursor);

@SuppressWarnings("static-access")
String [] from = new String [] {DBsearchRef.KEY_NAME, DBsearchRef.KEY_TEL, DBsearchRef.KEY_EMAIL, DBsearchRef.KEY_COMMENTS};
int [] to = new int [] {R.id.txtNameList, R.id.txtTelList, R.id.txtEmailList, R.id.txtCommentsList};

@SuppressWarnings("deprecation")
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.search, cursor, from, to);
searchedListResults.setAdapter(cursorAdapter);




}


}

最佳答案

在您的onCreate中,您必须进行更改

searchButton = (Button) findViewById(R.id.btnSearch);




searchButton = (Button) findViewById(R.id.btnSearchName);

10-08 18:31