显示第二个活动

时间:2017-12-06 11:05:49

标签: java android nullpointerexception sharedpreferences

问题是我有一个仪表板,它将显示6条生产线的数据,并且只需textviews即可查看进度。

但是,textview的数据来自一系列IF语句和计算,这些语句和计算是在另一个活动上执行的。

当我运行应用程序时,因为仪表板在第二个activity设置之前尝试读取数据,所以有空指针是可以理解的,因为我没有机会甚至到达第二个activity

要妥协,我必须将仪表板上的IF语句设置为按钮,但这会妨碍我的应用程序的可用性,我希望数据能够在仪表板上自动刷新,而无需按下按钮。

在第二个活动中,我将值保存到共享首选项,然后在仪表板上获取共享首选项并将其设置为textviews但仍然获取空值,因为仪表板是第一个屏幕。

行详细信息Java :(返回信息中心的主页按钮):

            SharedPreferences.Editor editor = getActivity().getSharedPreferences(MyPREFERENCES, MODE_PRIVATE).edit();

            if (spinnerSelection == null || spinnerSelection2 == null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Data Input Error");
                builder.setMessage("Line S2 Data has not been entered yet!");
                builder.setIcon(R.drawable.warning_icon);
                builder.setCancelable(true);

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
            } else if (spinnerSelection.equals("S1") && spinnerSelection2.equals("20")) {
                if (actualshippersnum > optimum20) {
                    plus20 = (int) (actualshippersnum - optimum20);
                    editor.putInt("Key1", plus20);
                    Toast.makeText(getActivity(), "Line S1 ahead by: " + (plus20) + " shippers", Toast.LENGTH_LONG).show();
                } else if (actualshippersnum < optimum20) {
                    minus20 = (int) (optimum20 - actualshippersnum);
                    editor.putInt("Key2", minus20);
                    Toast.makeText(getActivity(), "Line S1 behind by: " + (minus20) + " shippers", Toast.LENGTH_LONG).show();
                } else if (actualshippersnum == optimum20) {
                    editor.putInt("Key3", percentageActual20);
                    Toast.makeText(getActivity(), "Line S1 is on target at!", Toast.LENGTH_LONG).show();
                }
            } else if (spinnerSelection.equals("S1") && spinnerSelection2.equals("30")) {
                if (actualshippersnum == null) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle("Refresh Error");
                    builder.setMessage("Cannot Refresh! Please Fill in Line Details");
                    builder.setIcon(R.drawable.warning_icon);
                    builder.setCancelable(true);

                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    AlertDialog alert = builder.create();
                    alert.show();
                } else if (actualshippersnum > optimum30) {
                    plus30 = (int) (actualshippersnum - optimum30);
                    editor.putInt("Key 4", plus30);
                    Toast.makeText(getActivity(), "Line S1 ahead by: " + (plus30) + " shippers", Toast.LENGTH_LONG).show();
                } else if (actualshippersnum < optimum30) {
                    minus30 = (int) (optimum30 - actualshippersnum);
                    editor.putInt("Key 5", minus30);
                    Toast.makeText(getActivity(), "Line S1 behind by: " + (minus30) + " shippers", Toast.LENGTH_LONG).show();
                } else if (actualshippersnum == optimum30) {
                    editor.putInt("Key 6", percentageActual30);
                    Toast.makeText(getActivity(), "Line S1 is on target!", +Toast.LENGTH_LONG).show();
                }
            }
            if (spinnerSelection == null || spinnerSelection2 == null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Data Input Error");
                builder.setMessage("Line S2 Data has not been entered yet!");
                builder.setIcon(R.drawable.warning_icon);
                builder.setCancelable(true);

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
            } else if (spinnerSelection.equals("S2") && spinnerSelection2.equals("20")) {
                if (actualshippersnum == null) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle("Refresh Error");
                    builder.setMessage("Cannot Refresh! Please Fill in Line Details");
                    builder.setIcon(R.drawable.warning_icon);
                    builder.setCancelable(true);

                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    AlertDialog alert = builder.create();
                    alert.show();
                } else if (actualshippersnum > optimum20) {
                    plus20 = (int) (actualshippersnum - optimum20);
                    editor.putInt("Key 7", plus20);
                    Toast.makeText(getActivity(), "Line S2 ahead by: " + (plus20) + " shippers", Toast.LENGTH_LONG).show();
                } else if (actualshippersnum < optimum20) {
                    minus20 = (int) (optimum20 - actualshippersnum);
                    editor.putInt("Key 8", minus20);
                    Toast.makeText(getActivity(), "Line S2 behind by: " + (minus20) + " shippers", Toast.LENGTH_LONG).show();
                } else if (actualshippersnum == optimum20) {
                    editor.putInt("Key 9", percentageActual20);
                    Toast.makeText(getActivity(), "Line S2 is on target at!", Toast.LENGTH_LONG).show();
                }
            } else if (spinnerSelection.equals("S2") && spinnerSelection2.equals("30")) {
                if (actualshippersnum == null) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle("Refresh Error");
                    builder.setMessage("Cannot Refresh! Please Fill in Line Details");
                    builder.setIcon(R.drawable.warning_icon);
                    builder.setCancelable(true);

                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    AlertDialog alert = builder.create();
                    alert.show();
                } else if (actualshippersnum > optimum30) {
                    plus30 = (int) (actualshippersnum - optimum30);
                    editor.putInt("Key 10", plus30);
                    Toast.makeText(getActivity(), "Line S2 ahead by: " + (plus30) + " shippers", Toast.LENGTH_LONG).show();
                } else if (actualshippersnum < optimum30) {
                    minus30 = (int) (optimum30 - actualshippersnum);
                    editor.putInt("Key 11", minus30);
                    Toast.makeText(getActivity(), "Line S2 behind by: " + (minus30) + " shippers", Toast.LENGTH_LONG).show();
                } else if (actualshippersnum == optimum30) {
                    editor.putInt("Key 12", percentageActual30);
                    Toast.makeText(getActivity(), "Line S2 is on target!", +Toast.LENGTH_LONG).show();
                }
            }
            if (spinnerSelection == null || spinnerSelection2 == null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Data Input Error");
                builder.setMessage("Line S3 Data has not been entered yet!");
                builder.setIcon(R.drawable.warning_icon);
                builder.setCancelable(true);

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
            } else if (spinnerSelection.equals("S3")) {
                if (spinnerSelection2.equals("20")) {
                    //HAVENT FILLED INTO DECOVIEW - 20
                    if (actualshippersnum == null) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                        builder.setTitle("Refresh Error");
                        builder.setMessage("Cannot Refresh! Please Fill in Line Details");
                        builder.setIcon(R.drawable.warning_icon);
                        builder.setCancelable(true);

                        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });

                        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                        AlertDialog alert = builder.create();
                        alert.show();
                    } else
                    if (actualshippersnum > optimum20) {
                        plus20 = (int) (actualshippersnum - optimum20);
                        editor.putInt("Key 13", plus20);
                        Toast.makeText(getActivity(), "Line S3 ahead by: " + (plus20)+ " shippers" , Toast.LENGTH_LONG).show();
                    } else if (actualshippersnum < optimum20) {
                        minus20 = (int) (optimum20 - actualshippersnum);
                        editor.putInt("Key 14", minus20);
                        Toast.makeText(getActivity(), "Line S3 behind by: " + (minus20)+ " shippers" , Toast.LENGTH_LONG).show();
                    } else if (actualshippersnum == optimum20) {
                        editor.putInt("Key 15", percentageActual20);
                        Toast.makeText(getActivity(), "Line S3 is on target!" , Toast.LENGTH_LONG).show();
                    }
                }
                else if (spinnerSelection2.equals("30")) {

                    //HAVENT FILLED INTO DECOVIEW - 30
                    if (actualshippersnum == null) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                        builder.setTitle("Refresh Error");
                        builder.setMessage("Cannot Refresh! Please Fill in Line Details");
                        builder.setIcon(R.drawable.warning_icon);
                        builder.setCancelable(true);

                        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });

                        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                        AlertDialog alert = builder.create();
                        alert.show();
                    } else
                    if (actualshippersnum > optimum30) {
                        plus30 = (int) (actualshippersnum - optimum30);
                        editor.putInt("Key 16", plus30);
                        Toast.makeText(getActivity(), "Line S3 ahead by: " + (plus30)+ " shippers" ,Toast.LENGTH_LONG).show();
                    } else if (actualshippersnum < optimum30) {
                        minus30 = (int) (optimum30 - actualshippersnum);
                        editor.putInt("Key 17", minus30);
                        Toast.makeText(getActivity(), "Line S3 behind by: " + (minus30)+ " shippers" , Toast.LENGTH_LONG).show();
                    } else if (actualshippersnum == optimum30) {
                        editor.putInt("Key 18", percentageActual30);
                        Toast.makeText(getActivity(), "Line S3 is on target!", Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
    });
}

Dashboard Java :(简历中)

public void onResume() {
    super.onResume();

    int getKey1, getKey2, getKey3, getKey4, getKey5, getKey6, getKey7, getKey8, getKey9, getKey10, getKey11, getKey12, getKey13, getKey14, getKey15, getKey16,getKey17, getKey18;

    if (spinnerSelection == null || spinnerSelection2 == null) {
        AlertDialog.Builder builder = new AlertDialog.Builder(Dashboard.this);
        builder.setTitle("Data Input Error");
        builder.setMessage("No SP found!");
        builder.setIcon(R.drawable.warning_icon);
        builder.setCancelable(true);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    } else if (spinnerSelection.equals("S1") && spinnerSelection2.equals("20")) {
        if (actualshippersnum > optimum20) {
            getKey1 = sharedPreferences.getInt("Key1", plus20);
            bar4.setText(getKey1);
        } else if (actualshippersnum < optimum20) {
            getKey2 = sharedPreferences.getInt("Key2", minus20);
            bar4.setText(getKey2);
        } else if (actualshippersnum == optimum20) {
            getKey3 = sharedPreferences.getInt("Key3", percentageActual20);
            bar4.setText(getKey3);
    }


        else if (spinnerSelection.equals("S1") && spinnerSelection2.equals("30")) {
        if (actualshippersnum == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(Dashboard.this);
            builder.setTitle("Refresh Error");
            builder.setMessage("No SP found");
            builder.setIcon(R.drawable.warning_icon);
            builder.setCancelable(true);

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        } else if (actualshippersnum > optimum30) {
            getKey4 = sharedPreferences.getInt("Key4", plus30);
            bar4.setText(getKey4);
        } else if (actualshippersnum < optimum30) {
            getKey5 = sharedPreferences.getInt("Key5", minus30);
            bar4.setText(getKey5);
        } else if (actualshippersnum == optimum30) {
            getKey6 = sharedPreferences.getInt("Key6", percentageActual30);
            bar4.setText(getKey6);
        }
    }
    if (spinnerSelection == null || spinnerSelection2 == null) {
        AlertDialog.Builder builder = new AlertDialog.Builder(Dashboard.this);
        builder.setTitle("Data Input Error");
        builder.setMessage("No SP found");
        builder.setIcon(R.drawable.warning_icon);
        builder.setCancelable(true);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    } else if (spinnerSelection.equals("S2") && spinnerSelection2.equals("20")) {
        if (actualshippersnum == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(Dashboard.this);
            builder.setTitle("Refresh Error");
            builder.setMessage("No SP found");
            builder.setIcon(R.drawable.warning_icon);
            builder.setCancelable(true);

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        } else if (actualshippersnum > optimum20) {
            getKey7 = sharedPreferences.getInt("Key7", plus20);
            bar5.setText(getKey7);
        } else if (actualshippersnum < optimum20) {
            getKey8 = sharedPreferences.getInt("Key8", minus20);
            bar4.setText(getKey8);
        } else if (actualshippersnum == optimum20) {
            getKey9 = sharedPreferences.getInt("Key9", percentageActual20);
            bar4.setText(getKey9);
        }
    } else if (spinnerSelection.equals("S2") && spinnerSelection2.equals("30")) {
        if (actualshippersnum == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(Dashboard.this);
            builder.setTitle("Refresh Error");
            builder.setMessage("No SP found");
            builder.setIcon(R.drawable.warning_icon);
            builder.setCancelable(true);

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        } else if (actualshippersnum > optimum30) {
            getKey10 = sharedPreferences.getInt("Key10", plus30);
            bar4.setText(getKey10);
        } else if (actualshippersnum < optimum30) {
            getKey11 = sharedPreferences.getInt("Key11", minus30);
            bar4.setText(getKey11);
        } else if (actualshippersnum == optimum30) {
            getKey12 = sharedPreferences.getInt("Key12", percentageActual30);
            bar4.setText(getKey12);
        }
    }
        if (spinnerSelection == null || spinnerSelection2 == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(Dashboard.this);
            builder.setTitle("Data Input Error");
            builder.setMessage("Line S3 Data has not been entered yet!");
            builder.setIcon(R.drawable.warning_icon);
            builder.setCancelable(true);

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        } else if (spinnerSelection.equals("S3")) {
            if (spinnerSelection2.equals("20")) {
                //HAVENT FILLED INTO DECOVIEW - 20
                if (actualshippersnum == null) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Dashboard.this);
                    builder.setTitle("Refresh Error");
                    builder.setMessage("Cannot Refresh! Please Fill in Line Details");
                    builder.setIcon(R.drawable.warning_icon);
                    builder.setCancelable(true);

                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    AlertDialog alert = builder.create();
                    alert.show();
                } else if (actualshippersnum > optimum20) {
                    getKey13 = sharedPreferences.getInt("Key13", plus20);
                    bar6.setText(getKey13);
                } else if (actualshippersnum < optimum20) {
                    getKey14 = sharedPreferences.getInt("Key14", minus20);
                    bar6.setText(getKey14);
                } else if (actualshippersnum == optimum20) {
                    getKey15 = sharedPreferences.getInt("Key15", percentageActual20);
                    bar6.setText(getKey15);
                }
            } else if (spinnerSelection2.equals("30")) {

                //HAVENT FILLED INTO DECOVIEW - 30
                if (actualshippersnum == null) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Dashboard.this);
                    builder.setTitle("Refresh Error");
                    builder.setMessage("Cannot Refresh! Please Fill in Line Details");
                    builder.setIcon(R.drawable.warning_icon);
                    builder.setCancelable(true);

                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    AlertDialog alert = builder.create();
                    alert.show();
                } else if (actualshippersnum > optimum30) {
                    getKey16 = sharedPreferences.getInt("Key16", plus30);
                    bar6.setText(getKey16);
                } else if (actualshippersnum < optimum30) {
                    getKey17 = sharedPreferences.getInt("Key17", minus30);
                    bar6.setText(getKey17);
                } else if (actualshippersnum == optimum30) {
                    getKey18 = sharedPreferences.getInt("Key18", percentageActual30);
                    bar6.setText(getKey18);

1 个答案:

答案 0 :(得分:0)

您可以覆盖“仪表板”中的onActivityResult方法,并从“行详细信息”活动中获取结果。 Follow this link for more info