在从Android Vitals
收集失败和ANR的新部分之后,我在启用IllegalStateException
时有firebase database
个persistence
。
public class Configuration extends Application {
@Override
public void onCreate() {
super.onCreate();
if(!FirebaseApp.getApps(this).isEmpty()) {
FirebaseDatabase.getInstance().setPersistenceEnable(true);
}
}
}
例外:
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5530)
at java.lang.reflect.Method.invoke(Method.java:0)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:733)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
Caused by: java.lang.IllegalStateException:
at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:0)
at com.google.firebase.database.FirebaseDatabase.getInstance(FirebaseDatabase.java:0)
at <OR>.getInstance(FirebaseDatabase.java:0)
at <OR>.setPersistenceEnabled(FirebaseDatabase.java:0)
at <OR>.zziE(FirebaseDatabase.java:0)
at myapp.MyActivity.<init>(MyActivity.java:0)
at java.lang.Class.newInstance(Class.java:0)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
更新02/03/2018
根据@Frank van Puffelen的建议,我创建了这个answer,但问题仍然存在。
今天上午9:14,在应用版本27上 LGE LG K8(mm1v),1536MB RAM,Android 6.0 报告1
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2494)
at android.app.ActivityThread.access$900 (ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1356)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:148)
at android.app.ActivityThread.main (ActivityThread.java:5551)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:731)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:621)
Caused by: java.lang.IllegalStateException:
at com.google.firebase.FirebaseApp.getInstance (FirebaseApp.java)
at com.google.firebase.database.FirebaseDatabase.getInstance (FirebaseDatabase.java)
or .setPersistenceEnabled (FirebaseDatabase.java)
or .zzph (FirebaseDatabase.java)
at myapp.MyActivity.<init> (MyActivity.java)
at java.lang.Class.newInstance (Class.java)
at android.app.Instrumentation.newActivity (Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2329)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2494)
at android.app.ActivityThread.access$900 (ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1356)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:148)
at android.app.ActivityThread.main (ActivityThread.java:5551)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:731)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:621)
答案 0 :(得分:1)
if (pictureBox1.Image != null)
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] a = ms.GetBuffer();
ms.Close();
MemoryStream ms1 = new MemoryStream();
pictureBox2.Image.Save(ms1, pictureBox2.Image.RawFormat);
byte[] a2 = ms1.GetBuffer();
ms1.Close();
MemoryStream ms2 = new MemoryStream();
pictureBox3.Image.Save(ms2, pictureBox3.Image.RawFormat);
byte[] a3 = ms2.GetBuffer();
ms2.Close();
MemoryStream ms3 = new MemoryStream();
pictureBox4.Image.Save(ms3, pictureBox4.Image.RawFormat);
byte[] a4 = ms3.GetBuffer();
ms3.Close();
cmd.Parameters.Clear();
// cmd1.Parameters.Clear();
cmd.Connection = con;
cmd1.Connection = con;
cmd.Parameters.AddWithValue("@img1", a);
cmd.Parameters.AddWithValue("@img2", a2);
cmd.Parameters.AddWithValue("@img3", a3);
cmd.Parameters.AddWithValue("@img4", a4);
cmd1.CommandText = "Insert into proiecte(numeproiect,judet,oras,strada,numaretajeimobil,clasaenergetica,parcare,mezanin,demisol,mansarda,descriereproiect)values('"
+ nameofproject.Text + "','" + district_text.Text + "','" + city_text.Text + "','" + street_text.Text + "','" + bunifuDropdown2.selectedValue + "','" + bunifuMaterialTextbox1.Text + "','" + bunifuDropdown1.selectedValue + "','" + mezanine + "','" + semibasement + "','" + mansard + richTextBox1.Text + "')";
cmd.CommandText = "insert into proiecte(img1,img2,img3,img4)values(@img1,@img2,@img3,@img4)";
con.Open();
cmd1.ExecuteNonQuery();
cmd.ExecuteNonQuery();
con.Close();
}
的AndroidManifest.xml
public class FirebaseContentProvider extends ContentProvider {
private static FirebaseDatabase mDatabase;
@Override
public boolean onCreate() {
if (mDatabase == null) {
mDatabase = FirebaseDatabase.getInstance();
mDatabase.setPersistenceEnabled(true);
}
return true;
}
}
答案 1 :(得分:0)
要解决此问题,请使用以下代码:
private static boolean calledAlready = false;
if (!calledAlready) {
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
calledAlready = true;
}
您需要使用此代码作为onCreate
方法的第一个代码。在这种情况下,setPersistenceEnabled()
方法只有在之前未被调用时才会被调用,因为calledAlready
布尔值存在。
希望它有所帮助。