我有使用android studio从sqlite数据库获取单行的问题。当我在我的设备上运行应用程序时,它运行顺畅,但是当我键入关键字并单击搜索按钮时,应用程序强制关闭。
这是我的dbhelper文件:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
import android.util.Log;
public class UserDbHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "hunterdb.db";
private static final int DATABASE_VERSION = 1;
public UserDbHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
Log.e("DATABASE OPERATION", "Database created / opened...");
}
public Cursor getContact(String nopol, SQLiteDatabase sqLiteDatabase)
{
String[] projections= {UserContract.NewUserInfo.DATA_TYPE, UserContract.NewUserInfo.DATA_RANGKA, UserContract.NewUserInfo.DATA_MESIN, UserContract.NewUserInfo.DATA_BIAYA};
String selection = UserContract.NewUserInfo.DATA_NOPOL+" LIKE ?";
String[] selection_args={nopol};
Cursor cursor=sqLiteDatabase.query(UserContract.NewUserInfo.TABLE_NAME,projections,selection,selection_args,null,null,null);
return cursor;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
}
}
这是我的searchcontactactivity类
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.media.MediaPlayer;
import android.widget.Toast;
import org.w3c.dom.Text;
public class SearchContantActivity extends ActionBarActivity{
TextView Display_nopol,Display_type,Display_rangka,Display_mesin,Display_pembiayaan;
EditText Search_name;
UserDbHelper userDbHelper;
SQLiteDatabase sqLiteDatabase;
String search_name;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.caridata_layout);
Search_name=(EditText)findViewById(R.id.search_name);
Display_nopol=(TextView)findViewById(R.id.txtnopol);
Display_type=(TextView)findViewById(R.id.txttype);
Display_rangka=(TextView)findViewById(R.id.txtrangka);
Display_mesin=(TextView)findViewById(R.id.txtmesin);
Display_pembiayaan=(TextView)findViewById(R.id.txtbiaya);
}
public void SearchContant(View view){
search_name=Search_name.getText().toString();
userDbHelper=new UserDbHelper(getApplicationContext());
sqLiteDatabase=userDbHelper.getReadableDatabase();
Cursor cursor=userDbHelper.getContact(search_name, sqLiteDatabase);
if(cursor.moveToFirst())
{
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.yelp);
mp.start();
String TYPE=cursor.getString(0);
String RANGKA=cursor.getString(1);
String MESIN=cursor.getString(2);
String BIAYA=cursor.getString(3);
Display_type.setText(TYPE);
Display_rangka.setText(RANGKA);
Display_mesin.setText(MESIN);
Display_pembiayaan.setText(BIAYA);
}else
{
Toast.makeText(getBaseContext(),"Data Tidak Ditemukan!!",Toast.LENGTH_LONG).show();
}
}
}
我用3列表制作了相同的程序,运行正常。但是,当我尝试使用超过3列的表格制作另一个时,应用程序强制关闭。谁知道问题?先谢谢,抱歉4我的英语不好!!