简体   繁体   English

BackupManager.dataChanged中ContextWrapper.getPackageName上的NullPointerException

[英]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.

相关问题 NullPointerException用于Android ContextWrapper - NullPointerException for android ContextWrapper NullPointerException getPackageName()在bindService上 - NullPointerException getPackageName() on bindService 使用SQLiteOpenHelper时ContextWrapper.openOrCreateDatabase中的NullPointerException - NullPointerException in ContextWrapper.openOrCreateDatabase when using SQLiteOpenHelper 将数据写入内部文件 - ContextWrapper NullPointerException - Writing Data to Internal File - ContextWrapper NullPointerException ContextWrapper.getFilesDir()中出现NullPointerException的原因 - Reasons for NullPointerException from ContextWrapper.getFilesDir() 使用Intent启动新的Activity。 NPE位于android.content.ContextWrapper.getPackageName - Starting new Activity using Intent. NPE at android.content.ContextWrapper.getPackageName android.content.contextwrapper.getsystemservice上的java.lang.nullpointerexception(Contextwrapper.java:386) - java.lang.nullpointerexception at android.content.contextwrapper.getsystemservice(Contextwrapper.java:386) 理解上下文和contextwrapper的问题 - Problems understanding context and contextwrapper Android - 问题 getResources() 和 getPackageName() - Android - Problem getResources() and getPackageName() 未模拟ContextWrapper中的getDir方法[Mockito] - Method getDir in ContextWrapper not mocked [Mockito]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM