[英]NullPointerException on ContextWrapper.getPackageName in BackupManager.dataChanged
Hello i try to use android cloud backup tools. 您好,我尝试使用android云备份工具。
Here is my code: 这是我的代码:
public class TheBackupAgent extends BackupAgentHelper {
// The name of the SharedPreferences file
static final String DATABASE = "/data/data/com.tigo/databases/exercise";
// A key to uniquely identify the set of backup data
public Context mContext;
String FILES_BACKUP_KEY;
// Allocate a helper and add it to the backup agent
@Override
public void onCreate() {
FILES_BACKUP_KEY = Secure.getString(TheBackupAgent.this.getContentResolver(),Secure.ANDROID_ID);
Log.i("created", "created");
FileBackupHelper helper = new FileBackupHelper(TheBackupAgent.this, DATABASE);
addHelper(FILES_BACKUP_KEY, helper);
}
public void requestBackup() {
BackupManager bm = new BackupManager(TheBackupAgent.this);
bm.dataChanged();
}
public void setContext(Context context)
{
mContext = context;
}
public void requestRestore()
{
BackupManager bm = new BackupManager(TheBackupAgent.this);
bm.requestRestore(
new RestoreObserver()
{
public void restoreStarting(int error)
{
System.out.println(error);
}
public void restoreFinished(int error)
{
System.out.println(error);
}
}
);
}
In my manifest i have this: 在我的清单中,我有:
<application
android:name="com.example.workoutlog.ErrorReport"
android:allowBackup="true"
android:backupAgent="com.example.workoutlog.TheBackupAgent"
android:icon="@drawable/logo_for_stroe"
android:label="@string/app_name"
android:theme="@style/Holo.Theme.Light.DarkActionBar"
>
and this 和这个
<meta-data android:name="com.google.android.backup.api_key" android:value="********************************" />
When i try to use bm.dataChanged()
当我尝试使用
bm.dataChanged()
I get this error: 我收到此错误:
java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
at android.app.backup.BackupManager.dataChanged(BackupManager.java:93)
at com.example.workoutlog.TheBackupAgent.requestBackup(TheBackupAgent.java:42)
at com.example.workoutlog.Settings$9.onClick(Settings.java:1231)
at org.holoeverywhere.internal.AlertController$ButtonHandler.handleMessage(AlertController.java:280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Thanks for helping 感谢您的帮助
The BackupManager
constructor requires a Context
as an argument. BackupManager
构造函数需要使用Context
作为参数。 You are instead passing the instance of TheBackupAgent
. 相反,您正在传递
TheBackupAgent
的实例。 Use mContext
and make sure that you call setContext
first. 使用
mContext
并确保首先调用setContext
。
Even better would be to pass the context to the constructor of TheBackupAgent
, or even better, use the application context. 更好的方法是将上下文传递给
TheBackupAgent
的构造TheBackupAgent
,甚至更好的方法是使用应用程序上下文。
public class TheBackupAgent extends BackupAgentHelper {
private Context mContext;
public TheBackupAgent(Context context)
{
mContext = context;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.