无法在编辑sharedpreferences时未调用Looper.prepare()的线程内创建处理程序
我正在尝试学习和开发使用FCM的NoSQL数据库的示例应用程序。我已经在Firebase控制台上成功注册了我的应用程序,并且还通过此应用程序向数据库写入了一些虚拟值,表明从我的应用程序到FCM服务器的连接已清除。现在,我想出于某种原因使用sharedpreferences。当我尝试在onCreate方法编辑键值对,它给了我这个错误:无法在编辑sharedpreferences时未调用Looper.prepare()的线程内创建处理程序
Can't create handler inside thread that has not called Looper.prepare()
,它是在: 在com.example.ankitshubham.ribbon.MyFirebaseInstanceIDService.onTokenRefresh(MyFirebaseInstanceIDService.java :18)`
这是在MainActivity:
public class MainActivity extends AppCompatActivity {
private DatabaseReference mDatabase;
private final int MY_PERMISSIONS_REQUEST_READ_GET_ACCOUNTS =10;
SharedPreferences sharedPreferences;
private final static String INIT_DB = "init_db";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
mDatabase = FirebaseDatabase.getInstance().getReference();
sharedPreferences = getSharedPreferences(INIT_DB, MODE_PRIVATE);
if (!sharedPreferences.contains("firstrun")) {
Toast.makeText(getApplicationContext(),"first run",Toast.LENGTH_SHORT).show();
sharedPreferences.edit().putBoolean("firstrun", false).commit();
}
else {
Toast.makeText(getApplicationContext(),"not the first run",Toast.LENGTH_SHORT).show();
}
}
}
这是MyFirebaseInstanceIDService:
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
Log.i("Updated token", FirebaseInstanceId.getInstance().getToken());
Toast.makeText(getApplicationContext(), FirebaseInstanceId.getInstance().getToken(),Toast.LENGTH_SHORT).show();
}
}
如果我删除从MainActivity.java以下行:
sharedPreferences.edit().putBoolean("firstrun", false).commit();
那么它不显示任何错误。这里有什么可能是错的?
完整的堆栈跟踪:
07-19 18:20:07.468 9082-9082/? D/dalvikvm: Late-enabling CheckJNI
07-19 18:20:07.598 9082-9082/com.example.ankitshubham.ribbon D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
07-19 18:20:07.598 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.Context.getNoBackupFilesDir, referenced from method com.google.android.gms.common.util.zzx.getNoBackupFilesDir
07-19 18:20:07.598 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 382: Landroid/content/Context;.getNoBackupFilesDir()Ljava/io/File;
07-19 18:20:07.598 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
07-19 18:20:07.618 9082-9082/com.example.ankitshubham.ribbon D/FirebaseApp: Initialized class com.google.firebase.iid.FirebaseInstanceId.
07-19 18:20:07.618 9082-9082/com.example.ankitshubham.ribbon D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
07-19 18:20:07.628 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.zze.zzk
07-19 18:20:07.628 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 523: Landroid/content/pm/PackageManager;.getPackageInstaller()Landroid/content/pm/PackageInstaller;
07-19 18:20:07.628 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x000b
07-19 18:20:07.638 9082-9082/com.example.ankitshubham.ribbon I/FA: App measurement is starting up, version: 9080
07-19 18:20:07.638 9082-9082/com.example.ankitshubham.ribbon I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
07-19 18:20:07.638 9082-9082/com.example.ankitshubham.ribbon D/FA: Debug logging enabled
07-19 18:20:07.638 9082-9082/com.example.ankitshubham.ribbon D/FA: AppMeasurement singleton hash: 1099921264
07-19 18:20:07.688 9082-9082/com.example.ankitshubham.ribbon D/FirebaseApp: Initialized class com.google.android.gms.measurement.AppMeasurement.
07-19 18:20:07.688 9082-9082/com.example.ankitshubham.ribbon I/FirebaseInitProvider: FirebaseApp initialization successful
07-19 18:20:07.718 9082-9110/com.example.ankitshubham.ribbon D/dalvikvm: GC_FOR_ALLOC freed 334K, 13% free 2971K/3388K, paused 18ms, total 18ms
07-19 18:20:07.728 9082-9110/com.example.ankitshubham.ribbon W/GooglePlayServicesUtil: Google Play services out of date. Requires 9080000 but found 4324034
07-19 18:20:07.728 9082-9110/com.example.ankitshubham.ribbon D/FA: Service container out of date
07-19 18:20:07.728 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
07-19 18:20:07.728 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve interface method 16355: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V
07-19 18:20:07.728 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
07-19 18:20:07.728 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
07-19 18:20:07.728 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
07-19 18:20:07.728 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve interface method 16357: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
07-19 18:20:07.728 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
07-19 18:20:07.728 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
07-19 18:20:07.728 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve interface method 16361: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
07-19 18:20:07.728 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
07-19 18:20:07.738 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
07-19 18:20:07.738 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 617: Landroid/content/res/TypedArray;.getChangingConfigurations()I
07-19 18:20:07.738 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
07-19 18:20:07.738 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
07-19 18:20:07.738 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 639: Landroid/content/res/TypedArray;.getType (I)I
07-19 18:20:07.738 9082-9110/com.example.ankitshubham.ribbon W/GooglePlayServicesUtil: Google Play services out of date. Requires 9080000 but found 4324034
07-19 18:20:07.738 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.Context.createDeviceProtectedStorageContext, referenced from method android.support.v4.content.ContextCompat.createDeviceProtectedStorageContext
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 363: Landroid/content/Context;.createDeviceProtectedStorageContext()Landroid/content/Context;
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.Context.getCodeCacheDir, referenced from method android.support.v4.content.ContextCompat.getCodeCacheDir
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 369: Landroid/content/Context;.getCodeCacheDir()Ljava/io/File;
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.Context.getColor, referenced from method android.support.v4.content.ContextCompat.getColor
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 370: Landroid/content/Context;.getColor (I)I
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v4.content.ContextCompat.getColorStateList
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 371: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.Context.getDataDir, referenced from method android.support.v4.content.ContextCompat.getDataDir
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 373: Landroid/content/Context;.getDataDir()Ljava/io/File;
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.Context.getDrawable, referenced from method android.support.v4.content.ContextCompat.getDrawable
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 375: Landroid/content/Context;.getDrawable (I)Landroid/graphics/drawable/Drawable;
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.Context.getNoBackupFilesDir, referenced from method android.support.v4.content.ContextCompat.getNoBackupFilesDir
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 382: Landroid/content/Context;.getNoBackupFilesDir()Ljava/io/File;
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.Context.isDeviceProtectedStorage, referenced from method android.support.v4.content.ContextCompat.isDeviceProtectedStorage
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 395: Landroid/content/Context;.isDeviceProtectedStorage()Z
07-19 18:20:07.748 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
07-19 18:20:07.798 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
07-19 18:20:07.798 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 580: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-19 18:20:07.798 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
07-19 18:20:07.798 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
07-19 18:20:07.798 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 582: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-19 18:20:07.798 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
07-19 18:20:07.808 9082-9082/com.example.ankitshubham.ribbon I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList
07-19 18:20:07.808 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve virtual method 371: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
07-19 18:20:07.808 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
07-19 18:20:07.828 9082-9082/com.example.ankitshubham.ribbon W/GooglePlayServicesUtil: Google Play services out of date. Requires 9080000 but found 4324034
07-19 18:20:07.828 9082-9082/com.example.ankitshubham.ribbon I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:2 and remote module com.google.android.gms.firebase_database:0
07-19 18:20:07.828 9082-9082/com.example.ankitshubham.ribbon I/DynamiteModule: Selected local version of com.google.android.gms.firebase_database
07-19 18:20:08.309 9082-9109/com.example.ankitshubham.ribbon D/dalvikvm: GC_FOR_ALLOC freed 308K, 12% free 3158K/3556K, paused 15ms, total 15ms
07-19 18:20:08.399 9082-9082/com.example.ankitshubham.ribbon W/FA: Service connection failed: ConnectionResult{statusCode=SERVICE_VERSION_UPDATE_REQUIRED, resolution=null, message=null}
07-19 18:20:08.439 9082-9082/com.example.ankitshubham.ribbon D/ION: config: version(0x10001) secure(0xf000) 256M(0x221) fast(0x608) hwwr(0x4)
07-19 18:20:08.449 9082-9082/com.example.ankitshubham.ribbon I/MM_DEVICE: Waiting for mm thread to come up
07-19 18:20:08.449 9082-9127/com.example.ankitshubham.ribbon I/MM_DEVICE: mm_device_thread starting
07-19 18:20:08.449 9082-9082/com.example.ankitshubham.ribbon D/HAWAII_EGL: eglCreateContext() config: 19 context: 0x548a2498, VC context 0, Thread 9082
07-19 18:20:08.449 9082-9082/com.example.ankitshubham.ribbon D/HAWAII_EGL: eglMakeCurrent(0x548a2498, 0x5489dc50, 0x5489dc50) Thread: 9082
07-19 18:20:08.459 9082-9082/com.example.ankitshubham.ribbon D/OpenGLRenderer: Enabling debug mode 0
07-19 18:20:08.489 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: DexOpt: couldn't find field Landroid/os/Message;.sendingUid
07-19 18:20:08.489 9082-9082/com.example.ankitshubham.ribbon W/dalvikvm: VFY: unable to resolve instance field 132
07-19 18:20:08.489 9082-9082/com.example.ankitshubham.ribbon D/dalvikvm: VFY: replacing opcode 0x52 at 0x0000
07-19 18:20:09.940 9082-9128/com.example.ankitshubham.ribbon W/InstanceID/Rpc: Found 10009
07-19 18:20:11.352 9082-9117/com.example.ankitshubham.ribbon D/dalvikvm: GC_FOR_ALLOC freed 453K, 15% free 3215K/3752K, paused 15ms, total 17ms
07-19 18:20:11.382 9082-9128/com.example.ankitshubham.ribbon I/Updated token: c_p6z2MheZQ:APA91bFMfIpAyl_1SuCg3F2cG1cRLACtCgBSAtBnWY5sly-eypN-xGdh0jHBVa_EukERqYGUSFyuTYSSvypMXElWoFJ_6qNpjAWHrW7paNqiXMLp_VN2EwKdxTsFWttMMSImaETWFrLN
07-19 18:20:11.382 9082-9128/com.example.ankitshubham.ribbon W/dalvikvm: threadid=15: thread exiting with uncaught exception (group=0x41610ba8)
07-19 18:20:11.382 9082-9128/com.example.ankitshubham.ribbon E/AndroidRuntime: FATAL EXCEPTION: pool-4-thread-1
Process: com.example.ankitshubham.ribbon, PID: 9082
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.widget.Toast$TN.<init>(Toast.java:327)
at android.widget.Toast.<init>(Toast.java:92)
at android.widget.Toast.makeText(Toast.java:241)
at com.example.ankitshubham.ribbon.MyFirebaseInstanceIDService.onTokenRefresh(MyFirebaseInstanceIDService.java:18)
at com.google.firebase.iid.FirebaseInstanceIdService.zza(Unknown Source)
at com.google.firebase.iid.FirebaseInstanceIdService.zzm(Unknown Source)
at com.google.firebase.iid.zzb$2.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
07-19 18:20:11.542 9082-9082/com.example.ankitshubham.ribbon D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 9082
07-19 18:20:11.542 9082-9082/com.example.ankitshubham.ribbon D/HAWAII_EGL: eglDestroySurface() surface: 0x5489dc50, android window 0x548a0378, Thread: 9082
07-19 18:20:11.992 9082-9082/com.example.ankitshubham.ribbon D/HAWAII_EGL: eglMakeCurrent(0x548a2498, 0x55611c90, 0x55611c90) Thread: 9082
07-19 18:20:12.002 9082-9082/com.example.ankitshubham.ribbon D/FirebaseApp: Notifying background state change listeners.
07-19 18:20:12.002 9082-9082/com.example.ankitshubham.ribbon D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 9082
07-19 18:20:12.002 9082-9082/com.example.ankitshubham.ribbon D/HAWAII_EGL: eglMakeCurrent(0x548a2498, 0x55611c90, 0x55611c90) Thread: 9082
07-19 18:20:12.002 9082-9082/com.example.ankitshubham.ribbon D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 9082
07-19 18:21:07.747 9082-9886/com.example.ankitshubham.ribbon W/FA: Tasks have been queued for a long time
07-19 18:25:11.434 9082-9128/? I/Process: Sending signal. PID: 9082 SIG: 9
崩溃是由您的请求在不是主(UI)线程的线程上创建Toast而引起的。更改onTokenRefresh()
删除敬酒:
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
Log.i("Updated token", FirebaseInstanceId.getInstance().getToken());
// TOAST REMOVED
}
}
您的设备,或者有一个非常旧版本的谷歌游戏服务安装仿真器上同时运行。此警告表明:
W/GooglePlayServicesUtil: Google Play services out of date. Requires 9080000 but found 4324034
您应该更新设备/仿真器以使用支持您正在使用的Firebase版本的更新版本。
该警告消息还表示您正在构建Firebase版本9.0.8。大约一岁。当前版本是11.0.2。考虑升级。
由于您的错误显示com.google.firebase.auth.FirebaseAuth is not linked.
,意味着你没有intialized的FirebaseAuth
对象。所以使用下面的代码来解决你的问题。
FirebaseAuth auth = FirebaseAuth.getInstance();
使用也AuthStateListener
让你连接的状态是这样的:
FirebaseAuth.AuthStateListener authListener;
authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
Log.d("TAG", "You are signed in Firebase!");
} else {
Log.d("TAG", "You are not signed in Firebase!");
}
}
};
希望它能帮助。
请完整的stacktrace –