求大神帮忙解决一下 一直出现空指针(萌新求解)
package com.example.zz.myapplication;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SousuoActivity extends Activity implements View.OnClickListener {
public ImageButton sousuo,tianjia;
public ListView lv_users;
private EditText et_shuru;
public List<Map<String, Object>> listdata;
MyDB myDB;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sousuo);
getview();
tianjia.setOnClickListener(this);
sousuo.setOnClickListener(this);
myDB=new MyDB(SousuoActivity.this,"mydatabase.db",null,1);
db=myDB.getWritableDatabase();
setlistviewAdapt();
this.registerForContextMenu(lv_users);
// tianjia.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Intent intent = new Intent(SousuoActivity.this, AddUserActivity.class);
// startActivity(intent);
// }
// });
}
public void setlistviewAdapt()
{
getdata();
SimpleAdapter adapter=new SimpleAdapter(SousuoActivity.this,listdata,R.layout.listviewitem,new String[]{"id","song","artist"},new int[]{R.id.tv_name,R.id.tv_singer});
lv_users.setAdapter(adapter);
}
public void getdata()
{
listdata=new ArrayList<Map<String, Object>>();
String selectall="select * from users";
Cursor cursor=db.rawQuery(selectall,null);
while (cursor.moveToNext())
{
Map<String,Object>map=new HashMap<String,Object>();
map.put("id",cursor.getInt(0));
map.put("song",cursor.getString(cursor.getColumnIndex("song")));
map.put("artist",cursor.getString(cursor.getColumnIndex("artist")));
listdata.add(map);
// cursor.getString(cursor.getColumnIndex("song"));
// cursor.getString(2);
}
}
//模糊查询的方法
public void selectby(String info)
{
listdata=new ArrayList<Map<String, Object>>();
String selectby="select * from users where song like % '"+info+"'% or artist like %'"+info+"' ";
Cursor cursor=db.rawQuery(selectby,null);
while (cursor.moveToNext())
{
Map<String,Object>map=new HashMap<String,Object>();
map.put("id",cursor.getInt(0));
map.put("song",cursor.getString(cursor.getColumnIndex("song")));
map.put("artist",cursor.getString(cursor.getColumnIndex("artist")));
listdata.add(map);
// cursor.getString(cursor.getColumnIndex("song"));
// cursor.getString(2);
}
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.tianjia:
Intent intent=new Intent(SousuoActivity.this,AddUserActivity.class);
startActivity(intent);
break;
case R.id.sousuo://搜索按钮 模糊查询
selectby(et_shuru.getText().toString());
setlistviewAdapt();//重新加载listview数据
break;
}
}
public void ShowMassage(String msg) {
Toast.makeText(SousuoActivity.this, msg, Toast.LENGTH_SHORT).show();
}
public void getview()
{
tianjia=(ImageButton)findViewById(R.id.tianjia);
sousuo=(ImageButton)findViewById(R.id.sousuo);
et_shuru=(EditText)findViewById(R.id.et_shuru);
}
}