我是android的新手并实现了一个数据来自数据库的类,我使用了光标,但它给了我NumberformatException
。
我的代码是:
package com.example.login;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b,b1;
EditText e1,e2,e3;
SQLiteDatabase a;
database db;
Cursor r;
String s="";
String d="";
String w,t;
int q;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button1);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
e3=(EditText)findViewById(R.id.editText3);
q=Integer.parseInt(e1.getText()+"");
w=e2.getText()+"";
t=e3.getText()+"";
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
a=db.getWritableDatabase();
a.execSQL("insert into data values("+q+",'"+w+"','"+t+"');");
db.close();
}
});
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
a=db.getReadableDatabase();
r=a.rawQuery("select name from data", null);
System.out.println("========cursor======"+r.toString());
if(r.getCount()>0){
r.moveToNext();
s+=r.getString(0);
}
s+=r.getString(0);
db.close();
/*a=db.getReadableDatabase();
r=a.rawQuery("select pass from data", null);
d+=r.getString(0);
db.close();*/
Toast.makeText(getBaseContext(), s, Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
class database extends SQLiteOpenHelper{
public database(Context context) {
super(context, "data", null, 2);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
arg0.execSQL("create table data(cd int,name string ,pass string);");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
arg0.execSQL("drop table data;");
onCreate(arg0);
}
}
logcat的
07-28 17:06:47.988: E/AndroidRuntime(28379): Process: com.example.login, PID: 28379
07-28 17:06:47.988: E/AndroidRuntime(28379): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.login/com.example.login.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
07-28 17:06:47.988: E/AndroidRuntime(28379): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
07-28 17:06:47.988: E/AndroidRuntime(28379): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
07-28 17:06:47.988: E/AndroidRuntime(28379): at android.app.ActivityThread.access$900(ActivityThread.java:172)
07-28 17:06:47.988: E/AndroidRuntime(28379): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
07-28 17:06:47.988: E/AndroidRuntime(28379): at android.os.Handler.dispatchMessage(Handler.java:102)
07-28 17:06:47.988: E/AndroidRuntime(28379): at android.os.Looper.loop(Looper.java:146)
07-28 17:06:47.988: E/AndroidRuntime(28379): at android.app.ActivityThread.main(ActivityThread.java:5653)
07-28 17:06:47.988: E/AndroidRuntime(28379): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 17:06:47.988: E/AndroidRuntime(28379): at java.lang.reflect.Method.invoke(Method.java:515)
07-28 17:06:47.988: E/AndroidRuntime(28379): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
07-28 17:06:47.988: E/AndroidRuntime(28379): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
07-28 17:06:47.988: E/AndroidRuntime(28379): at dalvik.system.NativeStart.main(Native Method)
07-28 17:06:47.988: E/AndroidRuntime(28379): Caused by: java.lang.NumberFormatException: Invalid int: ""
07-28 17:06:47.988: E/AndroidRuntime(28379): at java.lang.Integer.invalidInt(Integer.java:137)
07-28 17:06:47.988: E/AndroidRuntime(28379): at java.lang.Integer.parseInt(Integer.java:358)
07-28 17:06:47.988: E/AndroidRuntime(28379): at java.lang.Integer.parseInt(Integer.java:331)
07-28 17:06:47.988: E/AndroidRuntime(28379): at com.example.login.MainActivity.onCreate(MainActivity.java:36)
07-28 17:06:47.988: E/AndroidRuntime(28379): at android.app.Activity.performCreate(Activity.java:5541)
07-28 17:06:47.988: E/AndroidRuntime(28379): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
07-28 17:06:47.988: E/AndroidRuntime(28379): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
07-28 17:06:47.988: E/AndroidRuntime(28379): ... 11 more
答案 0 :(得分:0)
错误应与此相关:
q=Integer.parseInt(e1.getText()+"");
我希望e1.getText()
为空字符串。
将其删除并使用数字初始化q
。
答案 1 :(得分:0)
你的例外是:
引起:java.lang.NumberFormatException:无效的int:""
这意味着您必须确保要解析的值。 ""
(无)不是数字。将文本设置为某个初始值,例如:e1.setText("42");
以确保正确解析,或者如果String为null / empty则不解析。
请注意,onCreate不允许用户更改其数据。你基本上总是得到一个空值。
if(e1.getText()!=null && !e1.getText().isEmpty()){
q=Integer.parseInt(e1.getText()+"");
} else {
Log.v(YourTag,"no number to parse, setting -1 instead");
q = -1;
}
答案 2 :(得分:0)
e1.getText()
返回空,因此Integer.parseInt(e1.getText()+"")
尝试将空字符串解析为int。首先,您可以查看e1.getText().toString()
is numeric.
private boolean checkStringIsNumeric(String string) {
return string.matches("\\d+(?:\\.\\d+)?");
}
if (checkStringIsNumeric(e1.getText().toString()))
q=Integer.parseInt(e1.getText().toString());