2017-07-11 1 views
0

J'essaye de faire un adaptateur qui montrera au listview le contenu de base de données en temps réel. J'utilise ma propre classe et je veux que la ligne montre des choses limitées de la classe.base de données FirebaseUI et auth

Je reçois FATAL EXCEPTION: principal quand j'essaie de courir.

--- EDIT ---

Je remarqué que ce qui se passe est que le uid auth retourne null alors que l'utilisateur est non nul.

auth qui retourne null avec adaptateur:

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); 
} 

classe:

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; 
    } 
} 

plan de la demande:

<?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> 

logcat:

--------- début de l'accident 07-12 18: 30: 15.773 2689-2689/com.newworldgrip.lostandfound.lostandfound E/AndroidRuntime: FATAL EXCEPTION: principal Processus: com.newworldgrip.lostandfound. lostandfound, PID: 2689 java.lang.RuntimeException: Impossible de démarrer l'activité ComponentInfo {com.newworldgrip.lostandfound.lostandfound/com.newwg.prip.lostandfound.lostandfound.MainActivity}: java.lang.NullPointerException: Tentative d'invocation de la méthode virtuelle 'com .google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener (com.google.firebase.database.ChildEventListener) » sur un objet de référence nulle à android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2817) at android.app.ActivityThread.handleL aunchActivity (ActivityThread.java:2892) at android.app.ActivityThread.-wrap11 (Unknown Source: 0) à l'adresse android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1593) at 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 (méthode native) à com.android.internal.os.Zygote $ MethodAndArgsCaller.run (Zygote.java:240) à com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767) Causée par: java.lang.NullPointerException: Tentative d'invocation de la méthode virtuelle 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener (com.google.firebase.database.ChildEventListener)' sur un null référence d'objet à 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) at 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 (Information inconnue Source: 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 (méthode native) à 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: Impossible de démarrer l'activité ComponentInfo {com.newworldgrip.lostandfound.lostandfound/com.newworldgrip.lostandfound.lostandfound.MainActivity}: java.lang. NullPointerException: Tentative d'invocation de la méthode virtuelle 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener (com.google.firebase.database.ChildEventListener)' sur une référence d'objet null at android.app .ActivityThread.performLaunchActivity (ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivi ty (ActivityThread.java:2892) at android.app.ActivityThread.-wrap11 (Unknown Source: 0) à l'adresse android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1593) at 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 (méthode native) à com.android.internal.os.Zygote $ MethodAndArgsCaller.run (Zygote.java:240) à com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767) Causée par: java.lang.NullPointerException: Tentative d'invocation de la méthode virtuelle 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener (com.google.firebase.database.ChildEventListener)' sur un null référence d'objet à 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.Instr umentation.callActivityOnCreate (Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2770) à l'adresse android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2892) at android.app.ActivityThread. -wrap11 (Source inconnue: 0) à android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1593) à android.os.Handler.dispatchMessage (Handler.java:105) à android.os.Looper. loop (Looper.java:164) at android.app.ActivityThread.main (ActivityThread.java:6540) à java.lang.reflect.Method.invoke (méthode native) à com.android.internal.os.Zygote $ MethodAndArgsCaller.run (Zygote.java:240) à com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767) toute aide serait apprécié.

+0

Quelle exception obtenez-vous? Veuillez modifier votre question pour inclure la trace complète de la pile (que vous pouvez obtenir depuis logcat). –

+0

Frank van Puffelen, c'est fait! –

Répondre

0

J'ai trouvé le problème, Android est asynchrone ce qui signifie qu'il n'attend pas sur l'auditeur d'état auth et saute directement dans l'analyse de base de données.

Tout ce que je devais faire est de mettre la base de données et l'adaptateur de liste dans l'écouteur.

0

Une première estimation rapide est que vos sous-vues ne sont pas trouvés dans populateView, parce que vous les chercher dans le monde:

((TextView) v.findViewById(R.id.requestName)).setText(model.getRequestName()); 

Le v. en est nouveau et il assure que vous recherchez un TextView à l'intérieur de la vue de cet élément spécifique, au lieu d'un global.

+0

Je l'ai fait, ça n'a pas marché –