3

How to delete user data in Android programmed?

 3 years ago
source link: https://www.codesd.com/item/how-to-delete-user-data-in-android-programmed.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to delete user data in Android programmed?

advertisements

Hello i am new in android , i followed a tutorial which explain how to clear app data in android, but when ever i am trying to clear my data i am getting an error of null object reference.

Main.java

public class MainActivity extends ActionBarActivity {
        Button b;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            b = (Button)findViewById(R.id.start);
            addDataInAppDir();

            b.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    ClearDataApplication.getInstance().clearApplicationData(getBaseContext());
                }

            });
        }

        private void addDataInAppDir() {
            SharedPreferences settings = getSharedPreferences("sample", 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("key1", true);
            editor.putString("key2", "Some strings in prefs");
            editor.commit();

            try {

                final String FILECONTENT = "This is string in file samplefile.txt";
                FileOutputStream fout = openFileOutput("samplefile.txt", MODE_PRIVATE);
                OutputStreamWriter osw = new OutputStreamWriter(fout);
                osw.write(FILECONTENT);
                osw.flush();
                osw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

ClearDataApplication.java

  public class ClearDataApplication extends Application
    {
        private static ClearDataApplication instance;

           @Override
           public void onCreate() {
               super.onCreate();
               instance = this;
           }

           public static ClearDataApplication getInstance() {
               return instance;
           }

           public void clearApplicationData(Context mContext) {

               File cache = mContext.getCacheDir();
               File appDir = new File(cache.getParent());

               if(appDir.exists()) {
                   String[] children = appDir.list();

                   for(String s : children) {
                       if(!s.equals("lib")) {
                           deleteDir(new File(appDir, s));
                       }
                   }
               }
           }

           public static boolean deleteDir(File dir) {
               if (dir != null && dir.isDirectory()) {
                   String[] children = dir.list();
                   for (int i = 0; i < children.length; i++) {
                       boolean success = deleteDir(new File(dir, children[i]));
                       if (!success) {
                           return false;
                       }
                   }
               }

               return dir.delete();
           }
    }

Logcat

10-10 16:25:55.589: E/AndroidRuntime(6121): FATAL EXCEPTION: main
10-10 16:25:55.589: E/AndroidRuntime(6121): Process: com.example.fetchdata, PID: 6121
10-10 16:25:55.589: E/AndroidRuntime(6121): java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.fetchdata.ClearDataApplication.clearApplicationData(android.content.Context)' on a null object reference
10-10 16:25:55.589: E/AndroidRuntime(6121):     at com.example.fetchdata.MainActivity$1.onClick(MainActivity.java:29)
10-10 16:25:55.589: E/AndroidRuntime(6121):     at android.view.View.performClick(View.java:4756)
10-10 16:25:55.589: E/AndroidRuntime(6121):     at android.view.View$PerformClick.run(View.java:19749)
10-10 16:25:55.589: E/AndroidRuntime(6121):     at android.os.Handler.handleCallback(Handler.java:739)
10-10 16:25:55.589: E/AndroidRuntime(6121):     at android.os.Handler.dispatchMessage(Handler.java:95)
10-10 16:25:55.589: E/AndroidRuntime(6121):     at android.os.Looper.loop(Looper.java:135)
10-10 16:25:55.589: E/AndroidRuntime(6121):     at android.app.ActivityThread.main(ActivityThread.java:5221)
10-10 16:25:55.589: E/AndroidRuntime(6121):     at java.lang.reflect.Method.invoke(Native Method)
10-10 16:25:55.589: E/AndroidRuntime(6121):     at java.lang.reflect.Method.invoke(Method.java:372)
10-10 16:25:55.589: E/AndroidRuntime(6121):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
10-10 16:25:55.589: E/AndroidRuntime(6121):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


The NPE is saying that your ClearDataApplication.getInstance() is returning null.

ClearDataApplication extends Application so that means it's intended to be your representation of the Application for the lifetime of the running process. You don't need that getInstance() method as the runtime already guarantees you that there will only be one instance. Instead, from your activity, call getApplication() and cast it to ClearDataApplicatiom.

In order for the above to work, you also need to make a change in your manifest file to specify that ClearDataApplication is what should be used as the representation of the Application.

<application android:name=".ClearDataApplication" ...>

Reference: http://developer.android.com/guide/topics/manifest/application-element.html#nm


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK