2017-04-14 207 views
0

我在我的android應用程序中使用了指紋登錄。如何從onAuthenticationSucceeded方法調用另一個類的方法?

  • 如果認證成功,它會調用onAuthenticationSucceeded方法。

  • 成功驗證指紋後,我想驗證onAuthenticationSucceeded內的用戶名。

  • 但我無法在onAuthenticationSucceeded方法內調用另一個類的方法(用於驗證用戶名)。

該應用似乎總是被停止。

我怎麼能達到它? 請幫幫我。 謝謝

  • 這是我FingerprintHandler.java

    public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result{
    login log=new login(); log.unameCheck(); }

  • 這是login.java

public void unameCheck(){
String uname=edit_username.getText().toString(); String storedPassword=myDb.getSingleEntry(uname);
if(storedPassword!=0){ Toast.makeText(login.this,"Login Successfull",Toast.LENGTH_LONG).show();
Intent intent =new Intent("michel.maan.login1"); startActivity(intent);. } else { Toast.makeText(login.this,"Login failed",Toast.LENGTH_LONG).show(); } }

    的方法
  • 這是我Databasehelper.java

    public String getSingleEntry(String userName){
    SQLiteDatabase db=this.getWritableDatabase();
    Cursor cursor=db.query("USER_TABLE".null,"NAME=?",new String[]{username},null,null,null); if(cursor.getCount()<1){
    cursor.close();
    return"NOT EXIST" ; } cursor.moveToFirst(); String password=cursor.getString(cursor.getColumnIndex("PASSWORD")); cursor.close();
    return password; }

  • android系統監視器顯示的例外是

    Java.lang.NullPointerExceptio:Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

This is the code I referred for my project from android authority

回答

0

你必須創建的實例上課並打電話給方法:在你的驗證類的其他類

void onAuthenticationSucceeded (...) { //in your onAuthenticationSucceeded method 
    ... 
    yourAnotherClass newInstance = new yourAnotherClass(...); //Make Instance of your verfying class 
    newInstance.verfyUserName (...); //call the Verifying Method 

    } 
+0

我曾經嘗試過,但verfyUsername()方法不能被調用。 – Michel

+0

登錄日誌=新登錄(); – Michel

+0

log.unameCheck(); – Michel

0

製作對象,並調用該方法使用該對象像下面

假設你的方法名稱,VerifyUserName(),它是在SecondActivity然後在您的firstActivity在驗證成功,做像下面

SecondActivity sa = new SecondActivity(); 
sa.VerifyUSerName(); 
+0

我試過了,但unameCheck()沒有調用。 – Michel

+0

那個方法有什麼? 它返回布爾值嗎? –

+0

public void unameCheck()方法用於驗證數據庫中的用戶名並在成功時爲Toast添加一條消息。 – Michel

1

簡單的方法是給startActivity上onAuthenticatinSucceed方法,並通過一個額外的與意圖

Intent intent=new Intent(this,SecondActivity.class); 
intent.putExtra("authentication","done"); 
startActivity(intent); 

現在在secondActiviy的的onCreate得到這個額外的檢查這樣

if(getIntent.getExtra("authentication").equals("done")) 
callVerifyUsername(); 

,如果你願意,你也可以從FingerprintActivity通過用戶名作爲額外的意圖。 如果你想回到前一個畫面,如果用戶名是未經過驗證,你可以簡單地調用

finish(); 
+0

我擔心它無法完成。 FingerprintHandler.java是從登錄活動中調用的。 – Michel

+0

請看看。我編輯了我的問題。 – Michel

+0

當你想驗證用戶來自同一個班級時,爲什麼你要在其他班級寫作。通過將它寫入同一個類來簡化它。 –

相關問題