Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to Destroy activity Error

Tags:

android

Im new in android... when i press back button it gives me the below Error...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 12-01 11:10:07.952: E/AndroidRuntime(3171): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3655) 12-01 11:10:07.952: E/AndroidRuntime(3171): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673) 12-01 11:10:07.952: E/AndroidRuntime(3171): at android.app.ActivityThread.access$2900(ActivityThread.java:125) 12-01 11:10:07.952: E/AndroidRuntime(3171): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 12-01 11:10:07.952: E/AndroidRuntime(3171): at android.os.Handler.dispatchMessage(Handler.java:99) 12-01 11:10:07.952: E/AndroidRuntime(3171): at android.os.Looper.loop(Looper.java:123) 12-01 11:10:07.952: E/AndroidRuntime(3171): at android.app.ActivityThread.main(ActivityThread.java:4627) 12-01 11:10:07.952: E/AndroidRuntime(3171): at java.lang.reflect.Method.invokeNative(Native Method) 12-01 11:10:07.952: E/AndroidRuntime(3171): at java.lang.reflect.Method.invoke(Method.java:521) 12-01 11:10:07.952: E/AndroidRuntime(3171): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 12-01 11:10:07.952: E/AndroidRuntime(3171): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 12-01 11:10:07.952: E/AndroidRuntime(3171): at dalvik.system.NativeStart.main(Native Method) 12-01 11:10:07.952: E/AndroidRuntime(3171): Caused by: java.lang.NullPointerException 12-01 11:10:07.952: E/AndroidRuntime(3171): at com.example.IranNara.FirstPage.onDestroy(FirstPage.java:133) 12-01 11:10:07.952: E/AndroidRuntime(3171): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642)

and my activity code is:

    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();
                          }
                      }
                   });
                }
            });     


}

//--------------------------------------------------------------------METHODS---------------------------------------------------------------------------------------------------------------

    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;

}`
like image 581
Mina Ramezani Avatar asked Dec 01 '13 08:12

Mina Ramezani


2 Answers

When password is not equal to storedPassword then mydb instance is null and you are trying to call close() method so put NULL check before calling close() in onDestroy :

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

}
like image 107
ρяσѕρєя K Avatar answered Nov 15 '22 20:11

ρяσѕρєя K


seems like the mydb is already null when you try to close it in the onDestroy method. Try this:

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


    }

Bw you should open and close the db in the getSinlgeEntry method. You dont need to close it in the onDestroy

like image 44
Sayed Jalil Hassan Avatar answered Nov 15 '22 19:11

Sayed Jalil Hassan