2013-01-13 75 views
0

我有一個XML佈局,它具有用戶可以輸入人名的EditText,然後是一個按鈕,它將在數據庫中搜索此對象,然後將結果顯示在其正下方的ListView中。帶有按鈕和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

發佈您的logcat。 – Kanth

回答

1

在你的onCreate,你必須改變

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

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