我试图在我的Android应用中使用Firebase而不是潜入并尝试自己动手,我正在关注Google Cloud网站上的教程。
https://cloud.google.com/solutions/mobile/firebase-app-engine-android-studio
我已经在第5步下添加了一个带有Firebase的ListView到你的Android应用程序'当我运行应用程序时,它会崩溃。我已经完成了它所说的一切,但仍然不了解它是如何崩溃的。
我的Logcat报告:
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: FATAL EXCEPTION: main
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: Process: com.simple.plain.todoapp, PID: 6164
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2387)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.ListView.makeAndAddView(ListView.java:1864)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.ListView.fillDown(ListView.java:698)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.ListView.fillFromTop(ListView.java:759)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.ListView.layoutChildren(ListView.java:1659)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.AbsListView.onLayout(AbsListView.java:2191)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.View.layout(View.java:15654)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4967)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.View.layout(View.java:15654)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4967)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.View.layout(View.java:15654)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4967)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.View.layout(View.java:15654)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4967)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.View.layout(View.java:15654)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4967)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.View.layout(View.java:15654)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4967)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.View.layout(View.java:15654)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:4967)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2102)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1859)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1078)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5875)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:580)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:550)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5299)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
11-19 18:42:02.199 6164-6164/com.simple.plain.todoapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
11-19 18:42:04.089 6164-6164/? I/Process: Sending signal. PID: 6164 SIG: 9
有没有其他人有同样的问题或想知道如何克服这个错误?我完全迷失了这个。
更新 这是我的XML文件代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
</LinearLayout>
这是我的Java文件代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get ListView object from xml
final ListView listView = (ListView) findViewById(R.id.listView);
// Create a new Adapter
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text1);
// Assign adapter to ListView
listView.setAdapter(adapter);
// Use Firebase to populate the list.
Firebase.setAndroidContext(this);
new Firebase("https://luminous-torch-5743.firebaseio.com/todoitems")
.addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
adapter.add((String)dataSnapshot.child("text").getValue());
}
public void onChildRemoved(DataSnapshot dataSnapshot) {
adapter.remove((String)dataSnapshot.child("text").getValue());
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) { }
public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
public void onCancelled(FirebaseError firebaseError) { }
});
}