2016-12-27 15 views
0

新しい問題が発生しました.FirebaseデータベースからArrayAdapterを使用してデータを取得する必要があります。最後に表示されるコード内でエラーが発生し続けます。本当の質問はここで私はどのようにエラーなしで私のlistviewに値をつけることができるのですか?ここでAndroidでリストビューのFirebaseデータを表示するエラー

は私activity_main.javaです:

package com.example.sean.firebasemessengerapp; 
 

 
import android.provider.Settings; 
 
import android.support.v7.app.AppCompatActivity; 
 
import android.os.Bundle; 
 
import java.util.ArrayList; 
 
import android.support.v7.widget.RecyclerView; 
 
import android.text.TextUtils; 
 
import android.util.Log; 
 
import android.view.View; 
 
import android.widget.ArrayAdapter; 
 
import android.widget.EditText; 
 
import android.widget.ListView; 
 
import android.widget.Toast; 
 

 
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.Query; 
 
import com.google.firebase.database.ValueEventListener; 
 

 

 
import java.util.HashMap; 
 
import java.util.Map; 
 

 
public class MainActivity extends AppCompatActivity { 
 
    // [START declare_database_ref] 
 
    private DatabaseReference mDatabase; 
 

 
    //ADD ARRAY FOR MESSAGES TO CAPTURE 
 
    protected ArrayList<String> MessagesToPost = new ArrayList<>(); 
 
    //final ListView listView = (ListView)findViewById(R.id.my_list_view); 
 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 

 
     mDatabase = FirebaseDatabase.getInstance().getReference(); 
 
     mDatabase = mDatabase.child("Messages").child("Message"); 
 

 

 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 
     final ListView listView = (ListView)findViewById(R.id.my_list_view); 
 

 
     final Query myTopMessagesPostQuery = mDatabase.child("Messages").child("Message"); 
 
     myTopMessagesPostQuery.addValueEventListener(new ValueEventListener() { 
 
      @Override 
 
      public void onDataChange(DataSnapshot dataSnapshot) { 
 
       for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) { 
 
        String MessagesPost = (String) postSnapshot.getValue(); //Updated line 
 
        MessagesToPost.add(MessagesPost); 
 
       } 
 
       ArrayAdapter<String> adapter = new ArrayAdapter<>(
 
         MainActivity.this, 
 
         android.R.layout.simple_list_item_multiple_choice, 
 
         MessagesToPost); 
 
       listView.setAdapter(adapter); 
 
       System.out.println("Array size is: " + MessagesToPost); 
 
      } 
 

 
      @Override 
 
      public void onCancelled(DatabaseError databaseError) { 
 

 
      } 
 
     }); 
 
     /*final Query myTopMessagesPostQuery = mDatabase.child("Messages").child("Message"); 
 

 
     myTopMessagesPostQuery.addValueEventListener(new ValueEventListener() { 
 
      @Override 
 
      public void onDataChange(DataSnapshot dataSnapshot) { 
 
       for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) { 
 
        String MessagesPost = (String) postSnapshot.child("Messages").child("Message").getValue(); 
 
        MessagesToPost.add(MessagesPost); 
 
       } 
 
       ArrayAdapter<String> adapter = new ArrayAdapter<>(
 
         MainActivity.this, 
 
         android.R.layout.simple_list_item_multiple_choice, 
 
         MessagesToPost); 
 
         listView.setAdapter(adapter); 
 
       System.out.println("Array size is: " + MessagesToPost.size()); 
 

 
      } 
 

 
      @Override 
 
      public void onCancelled(DatabaseError databaseError) { 
 

 
      } 
 

 

 
     });*/ 
 

 

 
    } 
 

 

 

 
} 
 

 

ここに私のXMLファイルには、次のとおりです。ここで

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:id="@+id/activity_main" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:paddingBottom="@dimen/activity_vertical_margin" 
 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
 
    android:paddingRight="@dimen/activity_horizontal_margin" 
 
    android:paddingTop="@dimen/activity_vertical_margin" 
 
    tools:context="com.example.sean.firebasemessengerapp.MainActivity"> 
 

 
    <ListView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:id="@+id/my_list_view"></ListView> 
 
</RelativeLayout>

は私のGradle Fiのですレ:ここ

// Top-level build file where you can add configuration options common to all sub-projects/modules. 
 

 
buildscript { 
 
    repositories { 
 
     jcenter() 
 
    } 
 
    dependencies { 
 
     classpath 'com.android.tools.build:gradle:2.2.3' 
 

 
     // NOTE: Do not place your application dependencies here; they belong 
 
     // in the individual module build.gradle files 
 
     classpath 'com.google.gms:google-services:3.0.0' 
 
    } 
 
} 
 

 
allprojects { 
 
    repositories { 
 
     jcenter() 
 
    } 
 
} 
 

 
task clean(type: Delete) { 
 
    delete rootProject.buildDir 
 
}

apply plugin: 'com.android.application' 
 

 
android { 
 
    compileSdkVersion 24 
 
    buildToolsVersion "25.0.0" 
 
    defaultConfig { 
 
     applicationId "com.example.sean.firebasemessengerapp" 
 
     minSdkVersion 15 
 
     targetSdkVersion 24 
 
     versionCode 1 
 
     versionName "1.0" 
 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
 
    } 
 
    buildTypes { 
 
     release { 
 
      minifyEnabled false 
 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
 
     } 
 
    } 
 
} 
 

 
dependencies { 
 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
 
     exclude group: 'com.android.support', module: 'support-annotations' 
 
    }) 
 
    compile 'com.android.support:appcompat-v7:24.2.1' 
 
    compile 'com.google.firebase:firebase-database:10.0.1' 
 
    testCompile 'junit:junit:4.12' 
 
    compile 'com.android.support:recyclerview-v7:24.2.1' 
 

 
} 
 

 

 
apply plugin: 'com.google.gms.google-services'
は、私は現在受け付けておりエラーは以下のとおりです。

12-27 16:41:39.542 18031-18031/? I/art: Not late-enabling -Xcheck:jni (already on) 
 
12-27 16:41:39.542 18031-18031/? W/art: Unexpected CPU variant for X86 using defaults: x86 
 
12-27 16:41:39.580 18031-18031/com.example.sean.firebasemessengerapp W/System: ClassLoader referenced unknown path: /data/app/com.example.sean.firebasemessengerapp-2/lib/x86 
 
                       
 
                       [ 12-27 16:41:39.610 1512: 1535 D/   ] 
 
                       HostConnection::get() New Host Connection established 0x93c2ed80, tid 1535 
 
12-27 16:41:39.620 18031-18031/com.example.sean.firebasemessengerapp I/InstantRun: Instant Run Runtime started. Android package is com.example.sean.firebasemessengerapp, real application class is null. 
 
12-27 16:41:39.973 18031-18031/com.example.sean.firebasemessengerapp W/System: ClassLoader referenced unknown path: /data/app/com.example.sean.firebasemessengerapp-2/lib/x86 
 
12-27 16:41:40.017 18031-18031/com.example.sean.firebasemessengerapp D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization. 
 
12-27 16:41:40.032 18031-18031/com.example.sean.firebasemessengerapp D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization. 
 
12-27 16:41:40.052 18031-18031/com.example.sean.firebasemessengerapp I/FA: App measurement is starting up, version: 10084 
 
12-27 16:41:40.052 18031-18031/com.example.sean.firebasemessengerapp I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE 
 
12-27 16:41:40.052 18031-18031/com.example.sean.firebasemessengerapp D/FA: Debug-level message logging enabled 
 
12-27 16:41:40.052 18031-18031/com.example.sean.firebasemessengerapp D/FA: AppMeasurement singleton hash: 23098228 
 
12-27 16:41:40.064 18031-18031/com.example.sean.firebasemessengerapp V/FA: Collection enabled 
 
12-27 16:41:40.064 18031-18031/com.example.sean.firebasemessengerapp V/FA: App package, google app id: com.example.sean.firebasemessengerapp, 1:79285920863:android:2946d5f3d63425bf 
 
12-27 16:41:40.064 18031-18031/com.example.sean.firebasemessengerapp I/FA: To enable faster debug mode event logging run: 
 
                      adb shell setprop debug.firebase.analytics.app com.example.sean.firebasemessengerapp 
 
12-27 16:41:40.095 18031-18031/com.example.sean.firebasemessengerapp V/FA: Registered activity lifecycle callback 
 
12-27 16:41:40.096 18031-18031/com.example.sean.firebasemessengerapp I/FirebaseInitProvider: FirebaseApp initialization successful 
 
12-27 16:41:40.117 18031-18064/com.example.sean.firebasemessengerapp V/FA: Using measurement service 
 
12-27 16:41:40.133 18031-18064/com.example.sean.firebasemessengerapp V/FA: Connecting to remote service 
 
12-27 16:41:40.257 18031-18031/com.example.sean.firebasemessengerapp D/AndroidRuntime: Shutting down VM 
 
                         
 
                         
 
                         --------- beginning of crash 
 
12-27 16:41:40.257 18031-18031/com.example.sean.firebasemessengerapp E/AndroidRuntime: FATAL EXCEPTION: main 
 
                         Process: com.example.sean.firebasemessengerapp, PID: 18031 
 
                         java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.sean.firebasemessengerapp/com.example.sean.firebasemessengerapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference 
 
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548) 
 
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
 
                          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
 
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
 
                          at android.os.Handler.dispatchMessage(Handler.java:102) 
 
                          at android.os.Looper.loop(Looper.java:154) 
 
                          at android.app.ActivityThread.main(ActivityThread.java:6077) 
 
                          at java.lang.reflect.Method.invoke(Native Method) 
 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
 
                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference 
 
                          at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116) 
 
                          at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147) 
 
                          at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27) 
 
                          at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:50) 
 
                          at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29) 
 
                          at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:29) 
 
                          at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:197) 
 
                          at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181) 
 
                          at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521) 
 
                          at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190) 
 
                          at com.example.sean.firebasemessengerapp.MainActivity.<init>(MainActivity.java:32) 
 
                          at java.lang.Class.newInstance(Native Method) 
 
                          at android.app.Instrumentation.newActivity(Instrumentation.java:1078) 
 
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538) 
 
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  
 
                          at android.app.ActivityThread.-wrap12(ActivityThread.java)  
 
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  
 
                          at android.os.Handler.dispatchMessage(Handler.java:102)  
 
                          at android.os.Looper.loop(Looper.java:154)  
 
                          at android.app.ActivityThread.main(ActivityThread.java:6077)  
 
                          at java.lang.reflect.Method.invoke(Native Method)  
 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  
 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

は、ここに私のデータベース構造である:

enter image description here

答えて

1

このあなたはレイアウトが作成される前であっても、リストを初期化しているため。 フェッチ問題について

setContentView(R.layout.activity_main);

更新 後、この行

listView = (ListView)findViewById(R.id.my_list_view); 

を追加し、あなたは間違った方法でデータをフェッチしています。これは次のようになります。

final Query myTopMessagesPostQuery = mDatabase.child("Messages").child("Message"); 
myTopMessagesPostQuery.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) { 
        String MessagesPost = (String) postSnapshot.getValue(); //Updated line 
        MessagesToPost.add(MessagesPost); 
       } 
       ArrayAdapter<String> adapter = new ArrayAdapter<>(
         MainActivity.this, 
         android.R.layout.simple_list_item_multiple_choice, 
         MessagesToPost); 
         listView.setAdapter(adapter); 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
+0

ありがとうございます。私はそのコードを明日試してみます。私はAndroidプログラミングを始めているので、私はまだ愚かな間違いを犯していると感じています。 – seansanders

+0

動作しているかどうか教えてください –

+0

私は適切な変更を加えましたが、最初はデータが表示されていません。私はそれについて何をするのか、どこが間違っているのか分かりません。 – seansanders

関連する問題