简体   繁体   中英

Restore sms in android device

I am working on a restore application. So I used this code trying to try to put an sms in the sms list

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Sms obj = null;
obj.setAddress("+216423788");
obj.setId("128");
obj.setMsg("Good morning Pall ");
System.out.println(obj.getAddress());  
restoreSms(obj , "inbox");}
public boolean restoreSms(Sms obj, String folderName) {
boolean ret = false;
 try {

        ContentValues values = new ContentValues();
        values.put("address", obj.getAddress());
        values.put("body", obj.getMsg());
        getContentResolver().insert(Uri.parse("content://sms/" + folderName), values);
        ret = true;
    } catch (Exception ex) {
        ret = false;
    }

    return ret;

}
}

It is not working and I am getting this error

E/AndroidRuntime(16957): FATAL EXCEPTION: main
E/AndroidRuntime(16957): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.restoresms/com.example.restoresms.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(16957): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2313)
E/AndroidRuntime(16957): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
E/AndroidRuntime(16957): at android.app.ActivityThread.access$600(ActivityThread.java:156)
E/AndroidRuntime(16957): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
E/AndroidRuntime(16957): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(16957): at android.os.Looper.loop(Looper.java:153)
E/AndroidRuntime(16957): at android.app.ActivityThread.main(ActivityThread.java:5336)
E/AndroidRuntime(16957): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(16957): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(16957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
E/AndroidRuntime(16957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
E/AndroidRuntime(16957): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(16957): Caused by: java.lang.NullPointerException
E/AndroidRuntime(16957): at com.example.restoresms.MainActivity.restoreSms(MainActivity.java:83)
E/AndroidRuntime(16957): at com.example.restoresms.MainActivity.onCreate(MainActivity.java:28)
E/AndroidRuntime(16957): at android.app.Activity.performCreate(Activity.java:5122)
E/AndroidRuntime(16957): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
E/AndroidRuntime(16957): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277)
E/AndroidRuntime(16957):    ... 11 more

You find here the Sms class that I created

public class Sms {

public   String Id;
public String Address;
public String Msg;
public Sms() {
this("","","");
   }       
   public Sms(String Id,String Address, String Msg) {
       this.Id = Id;
       this.Address = Address ; 
       this.Msg=Msg;     

   }
public String getId() {
    return Id;
}
public void setId(String id) {
    Id = id;
}
public String getAddress() {
    return Address;
}
public void setAddress(String address) {
    Address = address;
}
public String getMsg() {
    return Msg;
}
public void setMsg(String msg) {
    Msg = msg;
}   

}

I deleted Id from Sms class and my code works now

public class Sms {
public String Address;
public String Msg;
public Sms() {
this("","");
}       
public Sms(String Address, String Msg) {
   this.Address = Address ; 
   this.Msg=Msg;     

}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getMsg() {
return Msg;
}
public void setMsg(String msg) {
Msg = msg;
}   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM