java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() …. APP IS CRASHING

huangapple go评论78阅读模式
英文:

java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() .... APP IS CRASHING

问题

我的应用程序崩溃了...

我无法理解为什么会出现这个错误,以及如何去除这个错误以使应用程序正常工作。

在logcat中显示:
java.lang.NullPointerException: 无法在child()的参数'pathString'中传递null

2020-08-19 02:44:21.603 25163-25282/com. E/Parcel: 不支持读取空字符串。

我的活动:

      public class MainActivity extends AppCompatActivity {

private NavigationView navigationView;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private RecyclerView postList;
private Toolbar mToolbar;

private CircleImageView NavProfileImage;
private TextView NavProfileUserName;

private FirebaseAuth mAuth;
private DatabaseReference UsersRef;

String CurrentUserID;

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

    mAuth = FirebaseAuth.getInstance();
    CurrentUserID = mAuth.getCurrentUser().getUid();
    UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");

    mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle("Home");

    drawerLayout = (DrawerLayout) findViewById(R.id.drawable_layout);
    actionBarDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.drawer_open, R.string.drawer_closed);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    navigationView = (NavigationView) findViewById(R.id.navigation_view);

    View navView = navigationView.inflateHeaderView(R.layout.navigation_header);
    NavProfileImage = (CircleImageView) navView.findViewById(R.id.nav_profile_image);
    NavProfileUserName = (TextView) navView.findViewById(R.id.nav_user_full_name);

    UsersRef.child(CurrentUserID).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if (snapshot.exists()) {

                if (snapshot.hasChild("fullname")) {
                    String fullname = snapshot.child("fullname").getValue().toString();
                    NavProfileUserName.setText(fullname);
                }

                if (snapshot.hasChild("profileimage")) {
                    String image = snapshot.child("profileimage").getValue().toString();
                    Picasso.get().load(image).placeholder(R.drawable.ic_profile_pic_grey).into(NavProfileImage);
                }

                else {
                    Toast.makeText(MainActivity.this, "Profile name does not exist...", Toast.LENGTH_SHORT).show();
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            UserMenuSelector(item);

            return false;
        }
    });
}

我的logcat:
2020-08-19 02:44:21.603 25163-25282/com.app E/Parcel: 不支持读取空字符串。
2020-08-19 02:44:21.666 25163-25280/com.app V/FA: onActivityCreated
2020-08-19 02:44:21.828 25163-25336/com.appi V/FA: 正在连接到远程服务
2020-08-19 02:44:21.887 25163-25336/com.app V/FA: 连接尝试已在进行中
2020-08-19 02:44:21.949 25163-25336/? V/FA: 连接尝试已在进行中
2020-08-19 02:44:21.953 25163-25163/? W/ActionBarDrawerToggle: DrawerToggle可能不会显示,因为NavigationIcon不可见。您可能需要调用actionbar.setDisplayHomeAsUpEnabled(true);
2020-08-19 02:44:21.969 25163-25163/? D/OpenGLRenderer: Skia GL Pipeline
2020-08-19 02:44:21.969 25163-25163/? D/MMListParser: onlineVersion -1 defaultVersion 20191101
2020-08-19 02:44:21.970 25163-25163/? D/mmscene: 打开服务器配置失败/data/oppo/multimedia/oppo_display_perf_list.xml
2020-08-19 02:44:21.971 25163-25163/? D/mmscene: getHint applicationScene=com.appinion,idx=0 none
2020-08-19 02:44:21.981 25163-25163/? D/AndroidRuntime: 正在关闭VM
2020-08-19 02:44:21.985 25163-25163/? E/AndroidRuntime: 致命异常: 主线程
进程: com.app, PID: 25163
java.lang.RuntimeException: 无法启动组件信息{com.app/com.app.MainActivity}: java.lang.NullPointerException: 无法在child()的参数'pathString'中传递null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3146)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3296)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:114)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:74)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:500)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:913)
Caused by: java.lang.NullPointerException: 无法在child()的参数'pathString'中传递null
at com.google.firebase.database.DatabaseReference.child(DatabaseReference.java:96)
at com.appinion.MainActivity.onCreate(MainActivity.java:72)
at android.app.Activity.performCreate(Activity.java:7337)
at android.app.Activity.performCreate(Activity.java:7328)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3126)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3296)
... 13 more
2020-08-19 02:44:21.999 25163-25163/? I/Process: 发送信号。 PID: 25163 SIG: 9
告诉我我做错了什么...
请帮助我...
谢谢...
<details>
<summary>英文:</summary>
My app is crashing... 
I&#39;m not able to understand why iS  it is giving this error and how can i remove this error so the app starts working.
in the logcat it states: 
java.lang.NullPointerException: Can&#39;t pass null for argument &#39;pathString&#39; in child()
2020-08-19 02:44:21.603 25163-25282/com. E/Parcel: Reading a NULL string not supported here.
My activity:
public class MainActivity extends AppCompatActivity {
private NavigationView navigationView;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private RecyclerView postList;
private Toolbar mToolbar;
private CircleImageView NavProfileImage;
private TextView NavProfileUserName;
private FirebaseAuth mAuth;
private DatabaseReference UsersRef;
String CurrentUserID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
CurrentUserID = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child(&quot;Users&quot;);
mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle(&quot;Home&quot;);
drawerLayout = (DrawerLayout) findViewById(R.id.drawable_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.drawer_open, R.string.drawer_closed);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
View navView = navigationView.inflateHeaderView(R.layout.navigation_header);
NavProfileImage = (CircleImageView) navView.findViewById(R.id.nav_profile_image);
NavProfileUserName = (TextView) navView.findViewById(R.id.nav_user_full_name);
UsersRef.child(CurrentUserID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
if (snapshot.hasChild(&quot;fullname&quot;)) {
String fullname = snapshot.child(&quot;fullname&quot;).getValue().toString();
NavProfileUserName.setText(fullname);
}
if (snapshot.hasChild(&quot;profileimage&quot;)) {
String image = snapshot.child(&quot;profileimage&quot;).getValue().toString();
Picasso.get().load(image).placeholder(R.drawable.ic_profile_pic_grey).into(NavProfileImage);
}
else {
Toast.makeText(MainActivity.this, &quot;Profile name does not exist...&quot;, Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
UserMenuSelector(item);
return false;
}
});
}
my logcat:
2020-08-19 02:44:21.603 25163-25282/com.app E/Parcel: Reading a NULL string not supported here.
2020-08-19 02:44:21.666 25163-25280/com.app V/FA: onActivityCreated
2020-08-19 02:44:21.828 25163-25336/com.appi V/FA: Connecting to remote service
2020-08-19 02:44:21.887 25163-25336/com.app V/FA: Connection attempt already in progress
2020-08-19 02:44:21.949 25163-25336/? V/FA: Connection attempt already in progress
2020-08-19 02:44:21.953 25163-25163/? W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
2020-08-19 02:44:21.969 25163-25163/? D/OpenGLRenderer: Skia GL Pipeline
2020-08-19 02:44:21.969 25163-25163/? D/MMListParser: onlineVersion -1 defaultVersion 20191101
2020-08-19 02:44:21.970 25163-25163/? D/mmscene: open server config failed /data/oppo/multimedia/oppo_display_perf_list.xml
2020-08-19 02:44:21.971 25163-25163/? D/mmscene: getHint applicationScene=com.appinion,idx=0 none
2020-08-19 02:44:21.981 25163-25163/? D/AndroidRuntime: Shutting down VM
2020-08-19 02:44:21.985 25163-25163/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app, PID: 25163
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.MainActivity}: java.lang.NullPointerException: Can&#39;t pass null for argument &#39;pathString&#39; in child()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3146)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3296)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:114)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:74)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:500)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:913)
Caused by: java.lang.NullPointerException: Can&#39;t pass null for argument &#39;pathString&#39; in child()
at com.google.firebase.database.DatabaseReference.child(DatabaseReference.java:96)
at com.appinion.MainActivity.onCreate(MainActivity.java:72)
at android.app.Activity.performCreate(Activity.java:7337)
at android.app.Activity.performCreate(Activity.java:7328)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3126)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3296)&#160;
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)&#160;
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:114)&#160;
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:74)&#160;
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)&#160;
at android.os.Handler.dispatchMessage(Handler.java:106)&#160;
at android.os.Looper.loop(Looper.java:226)&#160;
at android.app.ActivityThread.main(ActivityThread.java:7224)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:500)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:913)&#160;
2020-08-19 02:44:21.999 25163-25163/? I/Process: Sending signal. PID: 25163 SIG: 9
TELL ME WHERE I&#39;M GOING WRONG...
please help me.....
thanks...
</details>
# 答案1
**得分**: 4
错误消息告诉您,在调用`child()`时传递了`null`。在您的代码中,只有一个地方可能出现`null`值:
```java
UsersRef.child(CurrentUserID).addValueEventListener(new ValueEventListener() {

这意味着在此处使用CurrentUserID时,它为null。这也意味着在调用它时,mAuth.getCurrentUser().getUid()null。这表示在那个时候用户未登录。在使用它之前,您应该检查是否为null

英文:

The error message is telling you that you passed null in a call to child(). You have only one place in your code where a null value is possible:

UsersRef.child(CurrentUserID).addValueEventListener(new ValueEventListener() {

This means CurrentUserID is null at the time you used it here. This means that mAuth.getCurrentUser().getUid() is null at the time you called it. Which means a user was not signed in at that time. You should check for null before using it like this.

huangapple
  • 本文由 发表于 2020年8月19日 05:36:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63476960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定