我正在创建一个Android应用,我想进行注册。我已将C.R.U.D文件写入数据库。当我启动我的应用程序并尝试添加某人时,我可以看到弹出消息:
"Unfortunately , the application has been stopped".
这是我的数据库控制器代码:
package com.example.lingwista.lingwista;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DB_Controller extends SQLiteOpenHelper {
public DB_Controller(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, "LINGWISTA.db", factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE USERS( ID INTEGER PRIMARY KEY AUTOINCREMENT, USERNAME TEXT UNIQUE, PASSWORD TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS USERS;");
}
public void insert_user(String userName, String password){
ContentValues contentValues = new ContentValues();
contentValues.put("USERNAME",userName);
contentValues.put("PASSWORD",password);
this.getWritableDatabase().insertOrThrow("USERS", "", contentValues);
}
public boolean search_user(String userName){
Cursor cursor = this.getReadableDatabase().rawQuery("SELECT USERNAME FROM USERS",null);
while (cursor.moveToNext()){
if(cursor.getString(1).equals(userName)){
return true;
}
}
return false;
}
public boolean search_password(String password){
Cursor cursor = this.getReadableDatabase().rawQuery("SELECT PASSWORD FROM USERS",null);
while (cursor.moveToNext()){
if(cursor.getString(1).equals(password)){
return true;
}
}
return false;
}
}
这是我使用控制器的java文件:
package com.example.lingwista.lingwista;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Rejestracja extends AppCompatActivity {
DB_Controller controller;
EditText username;
EditText password1;
EditText password2;
String login;
String haslo1;
String haslo2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rejestracja);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
username = (EditText)findViewById(R.id.editText3);
password1 = (EditText)findViewById(R.id.editText4);
password2 = (EditText)findViewById(R.id.editText5);
controller = new DB_Controller(this, "", null, 1);
}
public void goToMenu(View view) {
login = username.getText().toString();
haslo1 = password1.getText().toString();
haslo2 = password2.getText().toString();
boolean u;
String message;
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
if(haslo1.equals(haslo2)){
u = controller.search_user(login);
if(u==true){
message = "Istnieje już użytkownik który korzysta z tego adresu email";
Toast toast = Toast.makeText(context,message, duration);
toast.show();
}
else{
controller.insert_user(login,haslo1);
Intent intent = new Intent(".Menu");
startActivity(intent);
}
}else{
message = "Błąd hasła. Proszę upewnić się ze oba pola zawierają to samo hasło";
Toast toast = Toast.makeText(context,message, duration);
toast.show();
}
if(login.equals("") || haslo1.equals("") || haslo2.equals("")){
message = "Wszystkie pola muszą być wypełnione !";
Toast toast = Toast.makeText(context,message, duration);
toast.show();
}
}
@Override
public void onPause(){
super.onPause();
finish();
}
public void back(View view) {
Intent intent =new Intent(".MainMenuActivity");
startActivity(intent);
}
public void exitApp(View view) {
System.exit(0);
}
}
这是我的content.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.lingwista.lingwista.Rejestracja"
tools:showIn="@layout/activity_rejestracja">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:layout_alignParentTop="true"
android:src="@drawable/lingwista"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Rejestracja"
android:id="@+id/textView3"
android:layout_below="@+id/imageView2"
android:layout_centerHorizontal="true"
android:textColor="#00FF00"
android:layout_marginTop="30dp"
android:textSize="38dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText3"
android:layout_below="@+id/textView3"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:autoText="false"
android:hint="Adres email"
android:backgroundTint="#0000ff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText4"
android:layout_below="@+id/editText3"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:hint="Hasło"
android:backgroundTint="#0000ff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText5"
android:layout_below="@+id/editText4"
android:layout_alignRight="@+id/editText4"
android:layout_marginTop="5dp"
android:layout_alignEnd="@+id/editText4"
android:hint="Powtorz hasło"
android:backgroundTint="#0000ff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zarejestruj"
android:id="@+id/button12"
android:layout_marginTop="52dp"
android:layout_below="@+id/editText5"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:textSize="20dp"
android:background="#0000ff"
android:onClick="goToMenu"/>
</RelativeLayout>
这是错误日志:
01-16 02:55:10.539 18767-18791/com.example.lingwista.lingwista E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab855e50
01-16 02:55:13.104 18767-18767/com.example.lingwista.lingwista E/CursorWindow: Failed to read row 0, column 1 from a CursorWindow which has 1 rows, 1 columns.
01-16 02:55:13.104 18767-18767/com.example.lingwista.lingwista D/AndroidRuntime: Shutting down VM
01-16 02:55:13.104 18767-18767/com.example.lingwista.lingwista E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lingwista.lingwista, PID: 18767
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.example.lingwista.lingwista.DB_Controller.search_user(DB_Controller.java:35)
at com.example.lingwista.lingwista.Rejestracja.goToMenu(Rejestracja.java:47)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-16 02:55:19.507 18767-18767/? I/Process: Sending signal. PID: 18767 SIG: 9
答案 0 :(得分:2)
错误很明显:
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.example.lingwista.lingwista.DB_Controller.search_user(DB_Controller.java:35)
以下是崩溃的违规代码:
public boolean search_user(String userName){
Cursor cursor = this.getReadableDatabase().rawQuery("SELECT USERNAME FROM USERS",null);
while (cursor.moveToNext()){
if(cursor.getString(1).equals(userName)){
return true;
}
}
return false;
}
列具有从零开始的索引,您只需要查询中的一列。因此它应该是cursor.getString(0)
Cursor.getString(int columnIndex) documentation:
以字符串形式返回所请求列的值。
结果以及当列值为null或列类型不是字符串类型时此方法是否抛出异常是实现定义的。
<强>参数强>
columnIndex - 目标列的从零开始的索引。
<强>返回强>
该列的值为String。
另请注意,即使您使用了正确的列,写入时也可以获得空引用。你可能想要这样的东西:
if (!cursor.isNull(0) && cursor.getString(0).equals(userName)) {