问题描述
- android一个萌新的问题。。
- 想做一个登陆界面,首先上布局
<?xml version=""1.0"" encoding=""utf-8""?>
android:orientation=""vertical""
android:layout_width=""fill_parent""
android:layout_height=""fill_parent""
>
android:id=""@+id/button3""
android:layout_width=""wrap_content""
android:layout_height=""wrap_content""
android:text=""create""/>
android:id=""@+id/button1""
android:layout_width=""wrap_content""
android:layout_height=""wrap_content""
android:text=""Query""/>
android:id=""@+id/button2""
android:layout_width=""wrap_content""
android:layout_height=""wrap_content""
android:text=""Add""/><EditText android:id=""@+id/text1"" android:layout_width=""match_parent"" android:layout_height=""wrap_content""/><EditText android:id=""@+id/text2"" android:layout_width=""match_parent"" android:layout_height=""wrap_content""/>
就三个button和两个editview,没什么好说的
然后做了一张表
package com.example.SqlTest;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;/**
- Created by Administrator on 2016/5/15.
*/
public class Userdata extends SQLiteOpenHelper {public static final String CREATE_BOOK = ""create table Book(""+""id integer primary key autoincrement
+""account text+""password text)"";public Userdata(Context context String name SQLiteDatabase.CursorFactory factory int version) {
super(context name factory version);
}@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
}@Override
public void onUpgrade(SQLiteDatabase db int oldVersion int newVersion) {}
}
应该也没什么问题
下面是mianactivity
package com.example.SqlTest;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.EditText;public class MyActivity extends Activity {
/**
* Called when the activity is first created.
/
private EditText editText1;
private EditText editText2;
private Userdata userdata;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
userdata = new Userdata(thisData.db""null1);
Button create = (Button) findViewById(R.id.button3);
create.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
userdata.getWritableDatabase();
}
});
Button add = (Button) findViewById(R.id.button2);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String input1 = editText1.getText().toString();
String input2 = editText2.getText().toString();
SQLiteDatabase db = userdata.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(""account""input1);
values.put(""password""input2);
db.insert(""Book""nullvalues);
/*values.clear();
values.put(""account""yzw"");
values.put(""password""15260880965"");
db.insert(""Book""nullvalues);/
}
});
Button query = (Button) findViewById(R.id.button1);
query.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = userdata.getWritableDatabase();
Cursor cursor = db.query(""Book""nullnullnullnullnullnull);
if (cursor.moveToFirst()) {
do
{
String aaa = cursor.getString(cursor.getColumnIndex(""account""));
String ppp = cursor.getString(cursor.getColumnIndex(""password""));
Log.d(""MyActivity""account is ""+aaa);
Log.d(""MyActivity""password is""+ppp);
}while (cursor.moveToNext());
}
cursor.close();
}
});
}
}
代码做了两版,第一版我直接在代码里加数据到book里,这一版没问题(就是代码中注释掉的部分)
第二版我改成,获取两行editview中的内容,然后insert到book里,发现点击add按钮时程序就崩溃了。。。实在不知道为什么会这样。。第一次在CSDN发帖,谢谢各位的解答。。
- Created by Administrator on 2016/5/15.
解决方案
解决方案二:
自己好好看看错误日志,里面必定有说明,要么就是getText为null,转换失败,要么值不符合字段类型,无法转换