当我尝试遍历地图时,我的Android应用程序停止了

时间:2017-03-31 00:29:14

标签: android firebase firebase-realtime-database

我正在我的应用中创建一个活动,用于输出数据库中的数据。 我有一个产品地图,当我尝试遍历输出数据时,应用停止。这是我的代码:

package com.axiobase.axiobaseapp;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

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.ValueEventListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ManagersActivity extends AppCompatActivity {

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference().child("PossibleSale");
DatabaseReference reference = database.getReference().child("Equipment");
List<PossibleSale> possibleSaleList = new ArrayList<PossibleSale>();
List<String> possibleSalesStringList = new ArrayList<String>();
PossibleSale possibleSale;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_managers);

    final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
    final Context context = this;

    myRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            for (DataSnapshot child : snapshot.getChildren()) {
                Log.d("INPUT_INDICATORS", "Value is: " + child);

                possibleSale = child.getValue(PossibleSale.class);
                TextView textView1 = new TextView(context);
                textView1.setText(child.getValue(PossibleSale.class).getCompany_name());
                textView1.setPadding(50, 1, 1, 1);
                textView1.setTextSize(20);

                TextView textView2 = new TextView(context);
                String state = new String();
                state = "Estado de venta: ";
                if (child.getValue(PossibleSale.class).getStatus() == 0){
                    state += "En Progreso";
                }
                else if (child.getValue(PossibleSale.class).getStatus() == 1){
                    state += "Vendida";
                }
                else if (child.getValue(PossibleSale.class).getStatus() == 2){
                    state += "Suspendida";
                }

                textView2.setText(state);
                textView2.setPadding(100,1,1,1);
                textView2.setTextSize(18);

                double probab = child.getValue(PossibleSale.class).getProbability();
                String probability = "Probabilidad  " + String.valueOf(probab);
                TextView textView3 = new TextView(context);
                textView3.setText(probability);
                textView3.setPadding(100, 1, 1, 1);
                textView3.setTextSize(18);


                linearLayout.addView(textView1);
                linearLayout.addView(textView2);
                linearLayout.addView(textView3);


                Map<String, Integer> products = child.getValue(PossibleSale.class).getProducts();


                for (Map.Entry<String, Integer> entry : products.entrySet()){
                    TextView product = new TextView(context);
                    String key = entry.getKey();
                    Integer value = entry.getValue();
                    product.setText(key);
                    product.setTextSize(18);
                    product.setPadding(100, 1, 1, 1);
                    linearLayout.addView(product);
                }




            }

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w("INPUT_INDICATORS", "Failed to read value.", databaseError.toException());
        }
    });

}


}

此for循环出现问题:

    Map<String, Integer> products =  child.getValue(PossibleSale.class).getProducts();

            for (Map.Entry<String, Integer> entry : products.entrySet()){
                TextView product = new TextView(context);
                String key = entry.getKey();
                Integer value = entry.getValue();
                product.setText(key);
                product.setTextSize(18);
                product.setPadding(100, 1, 1, 1);
                linearLayout.addView(product);
            }

当我发表评论时,该应用程序可以正常运行。

我得到的错误是:

java.lang.NullPointerException: Attempt to invoke interface method 
'java.util.Set java.util.Map.entrySet()' on a null object reference

我该如何解决这个问题?

0 个答案:

没有答案