我是Android的初学者。我想在ListView上面添加一个搜索组件,所以我按照这个example
不幸的是,我在这行上获得了JavaNullpointer:
lv1.setAdapter(adapter);
但是当我调试它时,我的arraylist就被填满了。
// Pass results to ListViewAdapter Class
arraylist = (ArrayList<Client_Pers>) dbcon.ReadData();
adapter = new CustomListAdapter(this, arraylist);
这是我的主要课程:
public class MainActivity extends Activity {
ArrayList<Client_Pers> arraylist = new ArrayList<Client_Pers>();
CustomListAdapter adapter;
SQLController dbcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv1 = (ListView) findViewById(R.id.theclient);
final EditText inputSearch = (EditText)findViewById(R.id.editText_Search);
dbcon = new SQLController(this);
dbcon.open();
dbcon.insertSomeItmes();
// Pass results to ListViewAdapter Class
arraylist = (ArrayList<Client_Pers>) dbcon.ReadData();
adapter = new CustomListAdapter(this, arraylist);
// Binds the Adapter to the ListView
lv1.setAdapter(adapter);
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
String text = inputSearch.getText().toString().toLowerCase(Locale.getDefault());
adapter.filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
dbcon.close();
}
....
我的控制器类:
public class SQLController {
private DBHelper dbhelper;
private Context ourcontext;
private SQLiteDatabase database;
private ArrayList<Client_Pers> results = new ArrayList<Client_Pers>();
public SQLController(Context c) {
ourcontext = c;
}
public SQLController open() throws SQLException {
dbhelper = new DBHelper(ourcontext);
database = dbhelper.getWritableDatabase();
return this;
}
public void close() {
dbhelper.close();
}
public ArrayList<Client_Pers> ReadData() {
try {
String SQL_syntax = "SELECT * FROM " + DBHelper.tableName;
// if (FilterName != null && !FilterName.isEmpty())
// {SQL_syntax = "SELECT * FROM " + DBHelper.tableName + "WHERE "+ DBHelper.KEY_RsNom + "LIKE '%" + FilterName + "%'";}
Cursor c = database.rawQuery(SQL_syntax, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
Client_Pers ClientItem = new Client_Pers();
String LeCodeClient = c.getString(c.getColumnIndex(DBHelper.KEY_CodeClient));
ClientItem.setLeCodeCl(LeCodeClient);
String LeRsNom = c.getString(c.getColumnIndex(DBHelper.KEY_RsNom));
ClientItem.setLeRsNom(LeRsNom);
String LeVille = c.getString(c.getColumnIndex(DBHelper.KEY_Ville));
ClientItem.setLaVille(LeVille);
String LeTel = c.getString(c.getColumnIndex(DBHelper.KEY_Tel));
ClientItem.setLeTel(LeTel);
results.add(ClientItem);
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
return results;
}
public long createItmes(String LeCodeClient, String LeRsNom, String LeVille, String LeTel) {
ContentValues initialValues = new ContentValues();
initialValues.put(DBHelper.KEY_CodeClient, LeCodeClient);
initialValues.put(DBHelper.KEY_RsNom, LeRsNom);
initialValues.put(DBHelper.KEY_Ville, LeVille);
initialValues.put(DBHelper.KEY_Tel, LeTel);
return database.insert(DBHelper.tableName, null, initialValues);
}
public void insertSomeItmes() {
createItmes("850007","A B S","MONTAIGU","02 51 94 18 46");
createItmes("850012","SARL A D I P A"," SAINT HERBLAIN CEDEX","02 40 92 21 2");
createItmes("850024","AIZENAY AUTO","AIZENAY","02 51 94 60 40");
createItmes("850034","AMS","OLONNE SUR MER","02 51 95 05 23");
createItmes("850042","ETS ARMOUET","LES HERBIERS","02 51 92 98 59");
}
}
我的CustomListAdapter:
public class CustomListAdapter extends BaseAdapter {
private List<Client_Pers> Client_Pers_list = null;
private ArrayList<Client_Pers> Client_Pers_arraylist;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context context,List<Client_Pers> Client_Pers_list) {
this.Client_Pers_arraylist = new ArrayList<Client_Pers>();
this.Client_Pers_arraylist.addAll(Client_Pers_list);
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Client_Pers_list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return Client_Pers_list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.CodeClView = (TextView) convertView.findViewById(R.id.CodeCL);
holder.RsNomView = (TextView) convertView.findViewById(R.id.RsNom);
holder.VilleView = (TextView) convertView.findViewById(R.id.LaVille);
holder.TelView = (TextView) convertView.findViewById(R.id.TheTel);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Client_Pers ClientItem = (Client_Pers)Client_Pers_arraylist.get(position);
holder.CodeClView.setText(ClientItem.getLeCodeCl());
holder.RsNomView.setText( ClientItem.getLeRsNom());
holder.VilleView.setText(ClientItem.getLaVille());
holder.TelView.setText(ClientItem.getLeTel());
return convertView;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
Client_Pers_list.clear();
if (charText.length() == 0) {
Client_Pers_list.addAll(Client_Pers_arraylist);
} else {
for (Client_Pers wp : Client_Pers_arraylist) {
if (wp.getLeRsNom().toLowerCase(Locale.getDefault())
.contains(charText)) {
Client_Pers_list.add(wp);
}
}
}
notifyDataSetChanged();
}
static class ViewHolder {
TextView CodeClView;
TextView RsNomView;
TextView VilleView;
TextView TelView;
}
}
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.soft8.sql_test.MainActivity" >
<EditText
android:id="@+id/editText_Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Chercher" />
<ListView
android:id="@+id/theclient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText_Search" >
</ListView>
</RelativeLayout>
错误讯息:
Unable to start activity ComponentInfo{com.soft8.sql_test/com.soft8.sql_test.MainActivity}: java.lang.NullPointerException
答案 0 :(得分:2)
你有
return Client_Pers_list.size();
getCount()
中的
此
private List<Client_Pers> Client_Pers_list = null;
永远不会被初始化。
初始化为
public CustomListAdapter(Context context,List<Client_Pers> Client_Pers_list) {
this.Client_Pers_list = Client_Pers_list;
this.Client_Pers_arraylist = new ArrayList<Client_Pers>();
this.Client_Pers_arraylist.addAll(Client_Pers_list);
layoutInflater = LayoutInflater.from(context);
}