intent:我多次尝试从数据库中获取值。但它返回错误信息,请任何人都可以帮助我。当我从数据库中获取将显示nullpointer异常的用户名。
submit_btn.setOnClickListener(new OnClickListener() { String user;
Cursor cursor;
@Override
public void onClick(View v) {
cursor=database.rawQuery("SELECT * FROM " + "login" + "WHERE" + "username" + "=" +t, null);
do
{
if(cursor!=null&&cursor.getCount()>0)
{
user=cursor.getString(0);
}
}while(cursor.moveToNext());
Log.i(LOGTAG, ""+user);
String s=t.getText().toString();
float rate_bar_value=rb.getNumStars();
System.out.println(":::::::::::::::"+rate_bar_value);
if(rate_bar_value==5.0)
{
Intent i=new Intent(getApplicationContext(),ToggleActivity.class);
startActivity(i);
Toast.makeText(getApplicationContext(), "Welcome",Toast.LENGTH_LONG).show();
System.out.println(+pass);
}
else
{
System.out.println(+pass);
Toast.makeText(getApplicationContext(), "Try Again",Toast.LENGTH_LONG).show();
Intent i=new Intent(getApplicationContext(),CarActivity.class);
startActivity(i);
}
}
});
}
public void onRatingChanged(RatingBar rb, float rating,
boolean fromTouch)
{
final int numStars=rb.getNumStars();
pass=numStars;
System.out.println(+pass);
}
@Override
protected void onResume() {
super.onResume();
datasource.open();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
susper.onPause();
}
private void createData() {
Tables tables=new Tables();
tables.setuser("kumar");
tables.setpass("kumar");
Log.i(LOGTAG, "congrates");
}
}
我一直在研究一些sqlite数据库,我创建了一个表,并且...插入数据,如何获取数据并在textview中显示? ....如何从android中的sqlite数据库中检索数据并在TextView中显示它。
错误日志:
02-21 00:55:38.164: E/AndroidRuntime(1079): FATAL EXCEPTION:
main 02-21 00:55:38.164: E/AndroidRuntime(1079):
Process: com.example.splash, PID: 1079 02-21 00:55:38.164: E/AndroidRuntime(1079): java.lang.RuntimeException:
Unable to start activity
ComponentInfo{com.example.splash/com.example.splash.LoginActivity}:
java.lang.NullPointerException 02-21 00:55:38.164: E/AndroidRuntime(1079): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
答案 0 :(得分:2)
您的查询似乎未正确整理:
database.rawQuery("SELECT * FROM " + "login" + "WHERE" + "username" + "=" +t, null);
这将创建一个SQL语句:
SELECT * FROM loginWHEREusername=null
显然不会返回您期望的结果,这可能会导致您的NullPointerException。
插入something I made,这样可以更轻松地在没有SQL的情况下进行基本查询。如果您是初学者,我不会推荐它。