Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我尝试遍历地图时,我的android应用程序停止_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

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

当我尝试遍历地图时,我的android应用程序停止,android,firebase,firebase-realtime-database,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.vie

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

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

}


}

如何修复此问题?

您是否遇到任何错误异常?java.lang.NullPointerException:尝试在空对象引用上调用接口方法“java.util.Set java.util.Map.entrySet()”,或者我忘记将其放在帖子中发生错误是因为它无法读取
可能的\{id}\products
,可能在您的数据库中没有值?您的
地图产品
可能是
空的
,您可以
在foreach循环之前先记录它
    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