我试图制作一个用于记录的Android应用程序但是当我运行它时它告诉我应用已经停止,这是我的登录活动:
public class Connexion extends Activity {
Button b;
EditText PWDEDIT;
EditText LOGINEDIT;
String loginedit;
String pwdedit;
Context CTX = this;
Toast toast = Toast.makeText(this, "login ou mot de passe incorrect", Toast.LENGTH_LONG);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connexion);
b = (Button) findViewById(connexion);
PWDEDIT = (EditText) findViewById(passwordeditText);
LOGINEDIT = (EditText) findViewById(logineditText);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast t = Toast.makeText(getBaseContext(), "Please wait", Toast.LENGTH_LONG);
loginedit = LOGINEDIT.getText().toString();
pwdedit = PWDEDIT.getText().toString();
DataBaseOperationstwo DOP = new DataBaseOperationstwo(CTX);
Cursor CR = DOP.getloginuser(DOP);
CR.moveToFirst();
boolean loginstatus = false;
String NAME = "";
do {
if (loginedit.equals(CR.getString(0)) && (pwdedit.equals(CR.getString(1)))) {
loginstatus = true;
NAME = CR.getString(0);
}
} while (CR.moveToNext());
if (loginstatus) {
Toast.makeText(getBaseContext(), "authentification réussie \n Bienvenue" + NAME, Toast.LENGTH_LONG).show();
finish();
} else {
Toast.makeText(getBaseContext(), "login failed", Toast.LENGTH_LONG).show();
finish();
}
}
});
}
}
这是我的sqliteopenhelper类:
public class DataBaseOperationstwo extends SQLiteOpenHelper {
public static final int database_version = 10;
public String CREATE_QUERY = "CREATE TABLE " + TableData.TableInfo.TABLE_NAME +" ("+ TableData.TableInfo.USER_NAME+" TEXT,"+ TableData.TableInfo.USER_PASS+" TEXT );";
//clé etranger avec utilisateur
// + TASK_CAT + " integer," + " FOREIGN KEY ("+TASK_CAT+") REFERENCES "+CAT_TABLE+" ("+CAT_ID+"));";
//foreign key doone !!
private static final String CREATE_NOTE = "CREATE TABLE " + TableData.TableInfo.TABLE_NOTE_NAME +" (" +
TableData.TableInfo.UID_NOTE+" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.TITRE+" VARCHAR (255),"+ TableData.TableInfo.OBJET+" VARCHAR (255),"+ TableData.TableInfo.PRAM_N+" VARCHAR (255)," + TableData.TableInfo.EIDUSER+ " integer," + " FOREIGN KEY ("+TableData.TableInfo.EIDUSER+") REFERENCES "+TableData.TableInfo.TABLE_USER+" ("+TableData.TableInfo.UID_USER+"));";
//foreign key done!!
private static final String CREATE_ARCHIVE = "CREATE TABLE " + TableData.TableInfo.TABLE_ARCHIVE_NAME+" (" + TableData.TableInfo.UID_ARCHIVE+" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.ARCHIVE_NOTEID+ " integer," + " FOREIGN KEY ("+TableData.TableInfo.ARCHIVE_NOTEID+") REFERENCES "+TableData.TableInfo.TABLE_NOTE_NAME+" ("+TableData.TableInfo.UID_NOTE+"));";
//cv no FK
private static final String CREATE_USER = "CREATE TABLE " + TableData.TableInfo.TABLE_USER +" (" +
TableData.TableInfo.UID_USER+" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.NOM+" VARCHAR (255),"+ TableData.TableInfo.PRENOM+" VARCHAR (255),"+ TableData.TableInfo.DATE_NAISS+" DATE,"+ TableData.TableInfo.E_MAIL+
" VARCHAR (255),"+ TableData.TableInfo.LOGIN+" VARCHAR (255),"+ TableData.TableInfo.MOT_PASSE+" VARCHAR (255),"+ TableData.TableInfo.GENRE+" VARCHAR (255) );";
//cv FK dooooone!!
private static final String CREATE_INSCRIPTION = "CREATE TABLE " + TableData.TableInfo.TABLE_INSCRIPTION_NAME+" (" + TableData.TableInfo.UID_USERI+
" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.NOMI+" VARCHAR (255),"+ TableData.TableInfo.PRENOMI+"VARCHAR (255),"+ TableData.TableInfo.DATE_NAISSI+
"DATE,"+ TableData.TableInfo.E_MAILI+"VARCHAR (255),"+ TableData.TableInfo.LOGINI+" VARCHAR (255),"+ TableData.TableInfo.MOT_PASSEI+" VARCHAR (255),"+ TableData.TableInfo.INC_IDUSER+ " integer," + " FOREIGN KEY ("+TableData.TableInfo.INC_IDUSER+") REFERENCES "+TableData.TableInfo.TABLE_USER+" ("+TableData.TableInfo.UID_USER+"));";
//FK DOONE
private static final String CREATE_AUTHENTIFICATION = "CREATE TABLE " + TableData.TableInfo.TABLE_AUTHENTIFICATION_NAME+" (" + TableData.TableInfo.LOGINA+" VARCHAR (255),"+ TableData.TableInfo.MOT_PASSEA+" VARCHAR (255),"+ TableData.TableInfo.AIDUSER+ " integer," + " FOREIGN KEY ("+TableData.TableInfo.AIDUSER+") REFERENCES "+TableData.TableInfo.TABLE_USER+" ("+TableData.TableInfo.UID_USER+"));";
//cv no FK
private static final String CREATE_MESSAGE= "CREATE TABLE " + TableData.TableInfo.TABLE_MESSAGE_NAME+" (" + TableData.TableInfo.UID_MESSAGE+
" INTEGER PRIMARY KEY AUTOINCREMENT," + TableData.TableInfo.TEXTE_MESSAGE +" VARCHAR(255) );";
//cv no FK
private static final String CREATE_MESSAGE_FORUM = "CREATE TABLE " + TableData.TableInfo.TABLE_MESSAGE_NAME+" (" + TableData.TableInfo.UID_MESSAGE+
" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.SUJET_MESSAGE +" VARCHAR(255)," + TableData.TableInfo.TEXTE_MESSAGE+" VARCHAR(255) );";
//FK CV
private static final String CREATE_FORUM = "CREATE TABLE " + TableData.TableInfo.TABLE_FORUM_NAME+" (" + TableData.TableInfo.UID_FORUM+
" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.UID_NOTEF+ " integer," + " FOREIGN KEY ("+TableData.TableInfo.UID_NOTEF+") REFERENCES "+TableData.TableInfo.TABLE_NOTE_NAME+" ("+TableData.TableInfo.UID_NOTE+"));";
// clé etran\\CV
private static final String CREATE_NOTIFICATION = "CREATE TABLE " + TableData.TableInfo.TABLE_NOTIFICATION_NAME+" (" + TableData.TableInfo.UID_USERN+
" INTEGER, " + TableData.TableInfo.UID_NOTEN + TableData.TableInfo.UID_MESSAGE+
" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.TEXTE_MESSAGE +" VARCHAR(255)," + TableData.TableInfo.OBJET_NOTIFICATION+ " VARCHAR(255) )"+TableData.TableInfo.NIDUSER+ " integer," + " FOREIGN KEY ("+TableData.TableInfo.NIDUSER+") REFERENCES "+TableData.TableInfo.TABLE_USER+" ("+TableData.TableInfo.UID_USER+"));";
//insertion mrigel!!
private static final String CREATE_INSERTION = "CREATE TABLE " + TableData.TableInfo.TABLE_INSERTION_NAME+" (" + TableData.TableInfo.UID_INSERTION+" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.PATH_INSERTION +" VARCHAR(255)," + TableData.TableInfo.TAILLE_INSERTION+ " VARCHAR(255)"+ TableData.TableInfo.UID_NOTEINSERT+ " integer," + " FOREIGN KEY ("+TableData.TableInfo.UID_NOTEINSERT+") REFERENCES "+TableData.TableInfo.TABLE_NOTE_NAME+" ("+TableData.TableInfo.UID_NOTE+"));";
private static final String CREATE_ATEMPORAIRE = "CREATE TABLE " + TableData.TableInfo.TABLE_ATEMPORAIRE_NAME+" (" + TableData.TableInfo.UID_INSERTION+
" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.PATH_INSERTION +" VARCHAR(255)," + TableData.TableInfo.TAILLE_INSERTION+ " VARCHAR(255)," + TableData.TableInfo.DATE_ATEMPORAIRE+
" VARCHAR(255) );";
private static final String CREATE_AGEOGRAPHIQUE = "CREATE TABLE " + TableData.TableInfo.TABLE_AGEOGRAPHIQUE+" (" + TableData.TableInfo.UID_INSERTION+
" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.PATH_INSERTION +" VARCHAR(255)," + TableData.TableInfo.TAILLE_INSERTION+ " VARCHAR(255)," + TableData.TableInfo.LOCALISATION_AGEOGRAPHIQUE+
" VARCHAR(255) );";
private static final String CREATE_IMAGE = "CREATE TABLE " + TableData.TableInfo.TABLE_IMAGE_NAME+" (" + TableData.TableInfo.UID_INSERTION+
" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.PATH_INSERTION +" VARCHAR(255)," + TableData.TableInfo.TAILLE_INSERTION+ " VARCHAR(255) );" ;
private static final String CREATE_AUDIO = "CREATE TABLE " + TableData.TableInfo.TABLE_AUDIO_NAME+" (" + TableData.TableInfo.UID_INSERTION+
" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.PATH_INSERTION +" VARCHAR(255)," + TableData.TableInfo.TAILLE_INSERTION+ " VARCHAR(255) );" ;
private static final String CREATE_VIDEO = "CREATE TABLE " + TableData.TableInfo.TABLE_VIDEO_NAME+" (" + TableData.TableInfo.UID_INSERTION+
" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.PATH_INSERTION +" VARCHAR(255)," + TableData.TableInfo.TAILLE_INSERTION+ " VARCHAR(255) );" ;
private static final String CREATE_DOCUMENT = "CREATE TABLE " + TableData.TableInfo.TABLE_DOCUMENT_NAME+" (" + TableData.TableInfo.UID_INSERTION+
" INTEGER PRIMARY KEY AUTOINCREMENT,"+ TableData.TableInfo.PATH_INSERTION +" VARCHAR(255)," + TableData.TableInfo.TAILLE_INSERTION+ " VARCHAR(255) );" ;
public DataBaseOperationstwo(Context context){
super(context, TableData.TableInfo.DATABASE_NAME,null,database_version);
Log.d("SANADATABASE OPERATIONS", "DATABASE CREATED");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_QUERY);
Log.d("SANADATABASE OPERATIONS","note CREATED");
} catch (android.database.SQLException e) {
e.printStackTrace();
}
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
db.execSQL(CREATE_USER);
db.execSQL(CREATE_NOTE);
db.execSQL(CREATE_ARCHIVE);
db.execSQL(CREATE_INSCRIPTION);
db.execSQL(CREATE_AUTHENTIFICATION);
db.execSQL(CREATE_MESSAGE);
db.execSQL(CREATE_MESSAGE_FORUM);
db.execSQL(CREATE_FORUM);
db.execSQL(CREATE_NOTIFICATION);
db.execSQL(CREATE_INSERTION);
db.execSQL(CREATE_ATEMPORAIRE);
db.execSQL(CREATE_AGEOGRAPHIQUE);
db.execSQL(CREATE_IMAGE);
db.execSQL(CREATE_AUDIO);
db.execSQL(CREATE_VIDEO);
db.execSQL(CREATE_DOCUMENT);
onCreate(db);
Log.d("SANADATABASE OPERATIONS","new tables CREATED");
} catch (android.database.SQLException e) {
Log.d("SANADATABASE OPERATIONS","error"+e);
}
}
public void putuser(DataBaseOperationstwo dop,String nom,String prenom,String datenaiss,String mail,String login,String pwd,String genre)
{
SQLiteDatabase sqLiteDatabase= dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.NOM, nom);
cv.put(TableData.TableInfo.PRENOM, prenom);
cv.put(TableData.TableInfo.DATE_NAISS, datenaiss);
cv.put(TableData.TableInfo.E_MAIL, mail);
cv.put(TableData.TableInfo.LOGIN, login);
cv.put(TableData.TableInfo.MOT_PASSE, pwd);
cv.put(TableData.TableInfo.GENRE, genre);
long k = sqLiteDatabase.insert(TableData.TableInfo.TABLE_USER,null,cv);
Log.d("SANADATABASE OPERATIONS","One row inserted ");
}
public Cursor getloginuser(DataBaseOperationstwo dop)
{
SQLiteDatabase SQ = dop.getReadableDatabase();
String[] coloums = {TableData.TableInfo.LOGIN, TableData.TableInfo.MOT_PASSE};
Cursor CR = SQ.query(TableData.TableInfo.TABLE_USER,coloums,null,null,null,null,null);
return CR;
}
public void putnote(DataBaseOperationstwo dop,String titre,String objet)
{
SQLiteDatabase sqLiteDatabase= dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.TITRE, titre);
cv.put(TableData.TableInfo.OBJET, objet);
long k = sqLiteDatabase.insert(TableData.TableInfo.TABLE_USER,null,cv);
Log.d("SANADATABASE OPERATIONS","One row inserted ");
}
}
当我尝试打开我的应用时,最后但并非最不重要的是我的logcat:
04-30 13:41:43.796 1388-1388/com.example.hp.memorybackup D/AndroidRuntime﹕ Shutting down VM
04-30 13:41:43.816 1388-1388/com.example.hp.memorybackup W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-30 13:41:44.156 1388-1388/com.example.hp.memorybackup E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hp.memorybackup/com.example.hp.memorybackup.Connexion}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
at android.widget.Toast.<init>(Toast.java:92)
at android.widget.Toast.makeText(Toast.java:238)
at com.example.hp.memorybackup.Connexion.<init>(Connexion.java:34)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
04-30 13:42:07.967 1388-1388/com.example.hp.memorybackup I/Process﹕ Sending signal. PID: 1388 SIG: 9
然而它在我的Android清单中声明了。
答案 0 :(得分:1)
在你的代码中你得到资源ID错误添加R.id使用以下内容这将对你有帮助。
b = (Button) findViewById(R.id.connexion);
PWDEDIT = (EditText) findViewById(R.id.passwordeditText);
LOGINEDIT = (EditText) findViewById(R.id.logineditText);
当您从textview获取文本时,由于错误地查找ViewById,它为null。