【发布时间】:2017-12-16 00:20:36
【问题描述】:
我正在尝试制作一个适配器,它将显示到 listview 实时数据库内容。
我正在使用我自己的课程,我希望该行显示课程中的有限内容。
我尝试运行时收到 FATAL EXCEPTION: main。
---编辑---
我注意到正在发生的事情是 auth uid 返回 null 而 user 不为 null。
使用适配器返回 null 的身份验证:
private void prepareRequests() {
auth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
user = firebaseAuth.getCurrentUser();
if (user == null) {
startActivity(new Intent(MainActivity.this, OnStart.class));
finish();
} else {
databaseRef = database.getReference("requests").child(user.getUid());
}
}
});
FirebaseListAdapter<Request> requestAdapter;
try {
requestAdapter = new FirebaseListAdapter<Request>(
this,
Request.class,
android.R.layout.two_line_list_item,
databaseRef) {
@Override
protected void populateView(View v, Request model, int position) {
((TextView) v.findViewById(R.id.requestName)).setText(model.getRequestName());
((TextView) v.findViewById(R.id.handled)).setText(model.getHandled());
}
};
} catch (NullPointerException e){
Request request = new Request("Make new request here!", "false", "no one", "None", 0, 0, "No Message");
databaseRef.child("Request").setValue(request);
requestAdapter = new FirebaseListAdapter<Request>(
this,
Request.class,
android.R.layout.two_line_list_item,
databaseRef
) {
@Override
protected void populateView(View v, Request model, int position) {
((TextView) v.findViewById(R.id.requestName)).setText(model.getRequestName());
((TextView) v.findViewById(R.id.handled)).setText(model.getHandled());
}
};
}
ListView listView = (ListView) findViewById(R.id.list_of_req);
listView.setAdapter(requestAdapter);
}
类:
public class Request {
private String requestname;
private String handled;
private String type;
private String handler;
private String message;
private double lat;
private double longt;
public Request() {
}
public Request(String requestname, String handled, String handler, String type, double lat, double longt, String message) {
this.requestname = requestname;
this.handled = handled;
this.lat = lat;
this.longt = longt;
this.type = type;
this.handler = handler;
this.message = message;
}
public String getRequestName(){
return requestname;
}
public void setRequestName(String requestname){
this.requestname = requestname;
}
public double getLat () {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLongt() {
return longt;
}
public void setLongt(double longt) {
this.longt = longt;
}
public String getHandled() {
return handled;
}
public void setHandled(String handled){
this.handled = handled;
}
public String getType(){
return type;
}
public void setType (String type) {
this.type = type;
}
public String getHandler() {
return handler;
}
public void setHandler(String handler){
this.handler = handler;
}
public String getMessage(){
return message;
}
public void setMessage(String message){
this.message = message;
}
}
请求布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/requestName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:textSize="18sp" />
<TextView
android:id="@+id/handled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimaryDark"
android:textSize="16sp" />
</LinearLayout>
日志猫:
--------- 崩溃开始 07-12 18:30:15.773 2689-2689/com.newworldgrip.lostandfound.lostandfound E/AndroidRuntime: 致命例外: main 进程:com.newworldgrip.lostandfound.lostandfound,PID:2689 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.newworldgrip.lostandfound.lostandfound/com.newworldgrip.lostandfound.lostandfound.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法 'com.google.firebase.database .ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' 在空对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 在 android.app.ActivityThread.-wrap11(未知来源:0) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 在 android.os.Handler.dispatchMessage(Handler.java:105) 在 android.os.Looper.loop(Looper.java:164) 在 android.app.ActivityThread.main(ActivityThread.java:6540) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 原因:java.lang.NullPointerException:尝试在 null 上调用虚拟方法 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)'对象引用 在 com.firebase.ui.database.FirebaseArray.(FirebaseArray.java:43) 在 com.firebase.ui.database.FirebaseListAdapter.(FirebaseListAdapter.java:93) 在 com.newworldgrip.lostandfound.lostandfound.MainActivity$1.(MainActivity.java:0) 在 com.newworldgrip.lostandfound.lostandfound.MainActivity.onCreate(MainActivity.java:56) 在 android.app.Activity.performCreate(Activity.java:6980) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 在 android.app.ActivityThread.-wrap11(未知来源:0) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 在 android.os.Handler.dispatchMessage(Handler.java:105) 在 android.os.Looper.loop(Looper.java:164) 在 android.app.ActivityThread.main(ActivityThread.java:6540) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 07-12 18:30:15.773 2689-2689/com.newworldgrip.lostandfound.lostandfound E/UncaughtException: java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.newworldgrip.lostandfound.lostandfound/com.newworldgrip.lostandfound.lostandfound .MainActivity}:java.lang.NullPointerException:尝试在空对象引用 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 在 android.app.ActivityThread.-wrap11(未知来源:0) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 在 android.os.Handler.dispatchMessage(Handler.java:105) 在 android.os.Looper.loop(Looper.java:164) 在 android.app.ActivityThread.main(ActivityThread.java:6540) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 原因:java.lang.NullPointerException:尝试在 null 上调用虚拟方法 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)'对象引用 在 com.firebase.ui.database.FirebaseArray.(FirebaseArray.java:43) 在 com.firebase.ui.database.FirebaseListAdapter.(FirebaseListAdapter.java:93) 在 com.newworldgrip.lostandfound.lostandfound.MainActivity$1.(MainActivity.java:0) 在 com.newworldgrip.lostandfound.lostandfound.MainActivity.onCreate(MainActivity.java:56) 在 android.app.Activity.performCreate(Activity.java:6980) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 在 android.app.ActivityThread.-wrap11(未知来源:0) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 在 android.os.Handler.dispatchMessage(Handler.java:105) 在 android.os.Looper.loop(Looper.java:164) 在 android.app.ActivityThread.main(ActivityThread.java:6540) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 任何帮助将不胜感激。
【问题讨论】:
-
你得到什么异常?请编辑您的问题以包含完整的堆栈跟踪(您可以从 logcat 获得)。
-
Frank van Puffelen,完成!
标签: android firebase firebase-realtime-database firebaseui