我刚开始使用android,我正在尝试创建一个注册页面并将输入数据保存在sqlite数据库中,但是在我的手机上编译时我不断收到这个错误。“不幸的是 sqlite 已停止”;
注册类包 com.example.asus.sqlite;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Signup extends AppCompatActivity {
private EditText userinput ;
private EditText passin;
private EditText reppassin;
private EditText emailin;
private EditText phonenum;
private Button signup;
public static String user = "";
public static String pass = "";
public static String reppass = "";
public static String phonen = "";
public static String mail = "";
UserInfo submit = new UserInfo();
final DatabaseHandler handler = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
userinput = (EditText) findViewById(R.id.userin);
passin = (EditText) findViewById(R.id.pass1in);
reppassin = (EditText) findViewById(R.id.pass2in);
emailin = (EditText) findViewById(R.id.emailin);
phonenum = (EditText) findViewById(R.id.phonein);
signup = (Button) findViewById(R.id.signupbutt);
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
user = userinput.getText().toString().trim();
pass = passin.getText().toString().trim();
reppass = reppassin.getText().toString().trim();
phonen = phonenum.getText().toString().trim();
mail = emailin.getText().toString().trim();
if(pass.equals("") || user.equals("") || reppass.equals("") || phonen.equals("") || mail.equals("") ) {
Toast.makeText(getApplicationContext(), "make sure to fill all the informations", Toast.LENGTH_LONG).show();
} else if(!pass.equals(reppass)) {
Toast.makeText(getApplicationContext(), "password does not match", Toast.LENGTH_LONG).show();
} else {
submit.setUsername(user);
submit.setPassword(pass);
submit.setEmail(mail);
submit.setPhone(phonen);
handler.addUser(submit);
Intent redirect = new Intent(getApplicationContext(),Signin.class);
startActivity(redirect);
}
}
});
}
}
数据库类
包 com.example.asus.sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
/**
* Created by ASUS on 6/25/2017.
*/
public class DatabaseHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1 ;
public static final String DATABASE_NAME = "sqliteApp.db" ;
public static final String TABLE_NAME = "user_infos";
public static final String ID = "ID";
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String EMAIL = "Email";
public static final String PHONE = "phonenum";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + DATABASE_NAME + "("
+ ID + " INTEGER PRIMARY KEY," + USERNAME + " TEXT, "
+ PASSWORD + " TEXT, "
+ EMAIL + " TEXT, "
+ PHONE + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void addUser(UserInfo submit) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(USERNAME, submit.getUsername()); // Contact Name
values.put(PASSWORD, submit.getPassword()); // Contact password
values.put(EMAIL, submit.getEmail()); // Contact password
values.put(PHONE, submit.getPhone()); // Contact password
// Inserting Row
db.insert(TABLE_NAME, null, values);
db.close(); // Closing database connection
}
// Getting single contact
public UserInfo getUser(String userName , String password) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(DATABASE_NAME, new String[] {
USERNAME, PASSWORD }, USERNAME + "=?" + " AND " + PASSWORD + "=?",
new String[] { String.valueOf(userName) , String.valueOf(password) }, null, null, null);
if (cursor != null)
cursor.moveToFirst();
UserInfo user = new UserInfo (cursor.getString(0),
cursor.getString(1),"","");
// return contact
return user;
}
}
这是 logcat 错误
06-27 15:34:01.060 10546-10546/com.example.asus.sqlite E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.asus.sqlite, PID: 10546
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.sqlite/com.example.asus.sqlite.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.asus.sqlite/com.example.asus.sqlite.Signin}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2697)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2771)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.asus.sqlite/com.example.asus.sqlite.Signin}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1791)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1513)
at android.app.Activity.startActivityForResult(Activity.java:3940)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:3888)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4211)
at android.app.Activity.startActivity(Activity.java:4179)
at com.example.asus.sqlite.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:6185)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2650)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2771)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200