无法销毁活动错误

12
当我按下后退按钮时,出现以下错误:12-01 11:10:07.952: E/AndroidRuntime(3171): FATAL EXCEPTION: main 12-01 11:10:07.952: E/AndroidRuntime(3171): java.lang.RuntimeException: Unable to destroy activity {com.example.IranNara/com.example.IranNara.FirstPage}: java.lang.NullPointerException。我的活动代码是:
    Button Nara;
SQLiteDatabase mydb;
String Text="";
//private static String DBNAME = "PDA";    
private static String TABLE = "Users";
public static Integer StartFlag;
//LinearLayout linearL;
//public static String storedPersonelNo = "";

@Override
  public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.first_xml);                  
      Nara = (Button)findViewById(R.id.btnNara);
      StartFlag =0;

      final NaraAdapter mydb = new NaraAdapter(this);         
      mydb.createDatabase();       
      mydb.open();
      //System.out.println("My DB is:::: "+mydb);


      //For Login
      final Dialog dialog = new Dialog(FirstPage.this);
      dialog.setContentView(R.layout.login);
      dialog.setTitle("Login");
      final  EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
      final  EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin); 

     //----------for entering on Exit Button------------------------------------------------------------------------------------------
      if( getIntent().getBooleanExtra("Exit", false)){
            finish();
            return;
        }
     //-----------------------------------------------------------------------------------------------------------------------//********click on IranNara Button


Nara.setOnClickListener(new View.OnClickListener() {

                //Click on Cancel Button
                public void onClick(View v) 
                {      
                   Button btnLogin=(Button)dialog.findViewById(R.id.buttonLogin);
                   Button Cancel = (Button)dialog.findViewById(R.id.btncancel);
                   dialog.show();

                   Cancel.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        dialog.dismiss();
                        editTextPassword.setText("") ;
                    }});


                   //Click on Login Button 
                   btnLogin.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        // get The User name and Password
                        String userName=editTextUserName.getText().toString();
                        String password=editTextPassword.getText().toString();
                        // fetch the Password

                        String storedPassword = getSinlgeEntry(userName,1);
                        System.out.println("storedPassword:"+storedPassword);

                        // check if the Stored password matches with  Password entered by user
                        if(password.equals(storedPassword))
                        {
                            Toast.makeText(FirstPage.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
                            // Entered to Messages
                            String storedPersonelNo = getSinlgeEntry(userName,2);
                            Intent Startmain    = new Intent(FirstPage.this, Main.class);
                            Startmain.putExtra("PersonelNumber", storedPersonelNo);
                           // SharedPreferences settings = getSharedPreferences(storedPersonelNo, 0);
                            dialog.dismiss();
                            StartFlag=1;
                            startActivityForResult(Startmain, 0);                           
                        }
                        else
                        {
                            Toast.makeText(FirstPage.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
                          }
                      }
                   });
                }
            });     


}

//--------------------------------------------------------------------方法---------------------------------------------------------------------------------------------------------------

    public String getSinlgeEntry(String userName,Integer n)
{  
    NaraDatabaseHandler DH = new NaraDatabaseHandler(this);         
    mydb = DH.getReadableDatabase();
    //System.out.println("My DB is:::: "+mydb);
    Cursor cursor=mydb.query(TABLE, null, " UserName=?", new String[]{userName}, null, null, null);
    if(cursor.getCount()<1) 
    {   
        cursor.close();  
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    if (n==1){
    Text= cursor.getString(cursor.getColumnIndex("Password"));}
    if(n==2){
    Text= cursor.getString(cursor.getColumnIndex("PersonelNo"));}
    cursor.close();

    return Text;
}   

@Override
protected void onDestroy() {
    super.onDestroy();
    mydb.close();
    //StartFlag = null;

}`

在我的情况下,这个错误导致应用程序崩溃。 - Selim Raza
我找到了一个解决方案,只需要将应用程序的gradle更新从28到29,并更新必要的build.gradle文件和gradle-wrapper.properties文件。就这样。 - Selim Raza
2个回答

7

passwordstoredPassword不相等时,mydb实例为null,而您试图调用close()方法,请在onDestroy中在调用close()之前添加NULL检查:

@Override
protected void onDestroy() {
    super.onDestroy();
    if(null !=mydb)
       mydb.close();
    //StartFlag = null;

}

3

看起来当您在onDestroy方法中尝试关闭它时,mydb已经为空了。 请尝试这样做:

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mydb != null)
            mydb.close();


    }

在getSingleEntry方法中,您应该打开和关闭数据库。您不需要在onDestroy中关闭它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接