带有按钮和EditText的ListView

时间:2013-01-13 13:04:34

标签: java android xml

我有一个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);




}

}

1 个答案:

答案 0 :(得分:1)

在你的onCreate中你必须改变

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

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