我的android应用程序在验证阶段(firebase)崩溃了?

wztqucjr  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(404)

我正在尝试从firebase检索数据。操作看起来成功,但在我登录/注册时正在崩溃。我认为这可能是阵列适配器的问题。我已经清理、重建并查看了清单文件(顺便说一下,我正在使用一个模拟器)
我是android studio+firebase+stack overflow的新手,
控制台运行输出

reported: 147798919; UID 10154; state: ENABLED
I/OpenGLRenderer: Davey! duration=701ms; Flags=0, IntendedVsync=28127383579304, Vsync=28127400245970, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=28127409815200, AnimationStart=28127409867900, PerformTraversalsStart=28127410700500, DrawStart=28127411258900, SyncQueued=28127414225700, SyncStart=28127422917600, IssueDrawCommandsStart=28127422979700, SwapBuffers=28127823242800, FrameCompleted=28128093957100, DequeueBufferDuration=478000, QueueBufferDuration=266940200, GpuCompleted=0, 
I/AssistStructure: Flattened final assist data: 1576 bytes, containing 1 windows, 9 views
W/System: Ignoring header X-Firebase-Locale because its value was null.
W/System: Ignoring header X-Firebase-Locale because its value was null.
W/System: Ignoring header X-Firebase-Locale because its value was null.
D/FirebaseAuth: Notifying id token listeners about user ( ZHeNAZbKDUQbUSF5u9ilCXmlycv1 ).
D/AndroidRuntime: Shutting down VM
    Process: com.example.bearnecetties4, PID: 18765
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.bearnecetties4/com.example.bearnecetties4.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:173)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
        at android.content.Context.obtainStyledAttributes(Context.java:744)
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
        at com.example.bearnecetties4.MainActivity.<init>(MainActivity.java:28)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
I/Process: Sending signal. PID: 18765 SIG: 9

主活动.java
我认为问题出在代码的这一部分

package com.example.bearnecetties4;

    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;

    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.google.firebase.auth.FirebaseAuth;
    import com.google.firebase.database.DataSnapshot;
    import com.google.firebase.database.DatabaseError;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.database.ValueEventListener;

    import java.util.ArrayList;

    public class MainActivity extends AppCompatActivity {

        private Button logout = findViewById(R.id.logout);
        private ListView listView = findViewById(R.id.listVie

    w);
    private Button add = findViewById(R.id.add);
    private EditText edit = findViewById(R.id.edit);
    private TextView label =findViewById(R.id.label);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FirebaseAuth.getInstance().signOut();
                Toast.makeText(MainActivity.this, "logged Out", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(new Intent(MainActivity.this, StartActivity.class)));
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String txt_name = edit.getText().toString();
                if (txt_name.isEmpty())
                    Toast.makeText(MainActivity.this,"Logged Out",Toast.LENGTH_SHORT).show();
                else    {
                    FirebaseDatabase.getInstance().getReference().child("Test").push().child("Name").setValue(txt_name);
                }
            }
      **});
        final ArrayList<String> list= new ArrayList<>();
        final  ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.list_item,list);
                listView.setAdapter(adapter);
      DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
      reference.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
              list.clear();
              for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                  list.add(snapshot.getValue().toString());
              }
              adapter.notifyDataSetChanged();
          }
          @Override
          public void onCancelled(@NonNull DatabaseError error) {
          }
      });
    }
}

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bearnecetties4">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="BearNecessities"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.BearNecetties4">
        <activity android:name=".MainActivity"/>
        <activity android:name=".LoginActivity2" />
        <activity android:name=".RegisterActivity2" />
        <activity android:name=".StartActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest/>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题