简体   繁体   English

每次我开始上一个活动时Android应用程序崩溃

[英]Android app crashes every time i start the previous activity

I have 2 activities let's say activity Alpha and activity Beta. 我有2个活动,例如活动Alpha和活动Beta。

in my AlphaActivity.class i have the code below: 在我的AlphaActivity.class我有以下代码:

...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_profile);

    cDatabase.addValueEventListener(new ValueEventListener() {
        public static final String TAG = "XD";

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
                String u = snapshot.child("username").getValue().toString(),
                        p = snapshot.child("phone").getValue().toString(),
                        ad = snapshot.child("address").getValue().toString(),
                        f = snapshot.child("floor").getValue().toString(),
                        ns = snapshot.child("notes").getValue().toString();
                Address a = new Address(u, p, ad, f, ns);
                infoArray.add(a.address);
                nameArray.add(a.name);
                phoneArray.add(a.phone);
                writeListView();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(ProfileActivity.this, "Action canceled!", Toast.LENGTH_SHORT).show();
        }
    });
}
...

Whenever i run that activity works like a charm. 每当我运行该活动时,它就会像魅力一样。 I have a button in this one where i start the Beta activity onClick like so: 我在此按钮中有一个按钮,在该按钮中,我将启动Beta活动onClick如下所示:

public void betaMethod(View view) {
    finish();
    Intent intent = new Intent(this, BetaActivity.class);
    startActivity(intent);
}

In BetaActivity.class i do something and then again i have a button where onClick acts like below: BetaActivity.class我做了一些事情,然后又有一个按钮,其中onClick行为如下:

public void addToDatabase() {
    ...

    finish();

    Intent intent = new Intent(this, AlphaActivity.class);
    startActivity(intent);
}

When that codes executes, my app crashes and the error i get is on the AlphaActivity.class down there where i have p = snapshot.child("phone").getValue().toString() . 当该代码执行时,我的应用程序崩溃,并且我得到的错误是在那AlphaActivity.class上,其中我有p = snapshot.child("phone").getValue().toString() I bet it has to do with the firebase method onDataChange but i can't figure it out. 我敢打赌它与onDataChange方法onDataChange但我不知道。 Any suggestion please ? 有什么建议吗?

The error i get is: 我得到的错误是:

06-04 16:59:16.885 13978-13978/com.example.johng.assosfood E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.johng.assosfood, PID: 13978
java.lang.NullPointerException
    at com.example.johng.assosfood.ProfileActivity$1.onDataChange(ProfileActivity.java:48)
    at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
    at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
    at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5333)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
    at dalvik.system.NativeStart.main(Native Method)

where ProfileActivity.java = AlphaActivity.java 其中ProfileActivity.java = AlphaActivity.java

When you starting a new activtiy your code is 当您开始新的活动时,您的代码是

public void betaMethod(View view) {
    finish(); //here
    Intent intent = new Intent(this, BetaActivity.class);
    startActivity(intent);
}

Why you finish your activity when launching intent it means when you came back it can not find your last activity and in that case it can not load your last activity because it finished before launching intent . 为什么要在启动intent时完成活动,这意味着您回来后找不到您的上一个activity ,在这种情况下,它无法加载您的上一个活动,因为它在启动intent之前已完成。 and if you want to remove backstack from activity then use 如果您想从活动中删除后backstack ,请使用

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 

so remove finish from here and check your code works fine. 因此,请从此处删除完成并检查代码是否正常。

Let me know if it works for you. 请让我知道这对你有没有用。

I don't see any line looking like cDatabase = FireabseDatabase.getInstance().getReference(); 我没有看到任何类似cDatabase = FireabseDatabase.getInstance().getReference();

phone child must contain value in numbers and you're retrieving it as a String 子电话必须包含数字值,并且您将其作为字符串检索

Instead of p = snapshot.child("phone").getValue().toString() . 而不是p = snapshot.child("phone").getValue().toString()

Always use p = snapshot.child("phone").getValue(String.class); 始终使用p = snapshot.child("phone").getValue(String.class); or 要么

p = String.valueOf(snapshot.child("phone").getValue(String.class)); do this for all snapshots. 对所有快照执行此操作。

Also in public void betaMethod(View view) use finish(); 同样在public void betaMethod(View view)使用finish(); after startActivity() it's just good practice startActivity()这只是一个好习惯

Actually there is nothing wrong with calling finish() ! 实际上,调用finish()并没有错! in fact if you go back and forth too many times without calling finish() this can cause an OOM exception as the stack fills up with instances of each activity. 实际上,如果您在不调用finish()OOM exception下来回移动了太多次,则可能会导致OOM exception因为堆栈中充满了每个活动的实例。 I think you just need to call the finish() method after startActivity() not before. 我认为您只需要在startActivity()而不是之前调用finish()方法即可。

When you store long numbers in Firebase Database, it probably stores it in Float data type. 当您在Firebase数据库中存储长数字时,它可能以Float数据类型存储它。 So maybe to get value of "phone" you should try 因此,也许为了获得“电话”的价值,您应该尝试

String.valueOf((Float)snapshot.child("phone").getValue());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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