在Bean类中调用String返回Null

时间:2017-07-04 11:55:56

标签: java android

我有一个成功从另一个类中检索字符串值的bean类。=(它在bean类中打印得很好)

当我尝试调用该类/字符串时,它返回null。

以下是相关代码:

 public class cityModel implements Serializable {
    private String fajr;

    public void setFajr(String fajr) {
        this.fajr= fajr;

    }
    public String getFajr() {

        return fajr;
    }
}
 public void mutePrayerTime(View view) {
    cityModel cityObj= new cityModel();
    String fajr=cityObj.getFajr();
    Log.d("LOGCAT", "" + cityObj.getFajr());

    //StringBuilder newFajr = new StringBuilder(fajr);
    //newFajr.delete(2,5);
    //Log.d("newFajr", String.valueOf(newFajr));
  //  Intent alarm = new Intent(AlarmClock.ACTION_SET_ALARM);

   //alarm.putExtra(AlarmClock.EXTRA_HOUR, fajr );

}

Log.d标记LOGCAT返回为空

编辑: bean类从以下代码检索字符串的代码:

  protected void outputTimings(JSONArray jsonArray) {
        String[] prayers = {"fajr", "shurooq", "dhuhr", "asr", "maghrib", "isha"};
        cityModel cityObj;
        try {
            cityObj= new cityModel();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject cityObject =
                        jsonArray.getJSONObject(i);
                result = result + "fajr" + " : "
                        + cityObject.getString("fajr") + "\n" + result + "shurooq" + " : "
                        + cityObject.getString("shurooq") + "\n" + result + "dhuhr" + " : "
                        + cityObject.getString("dhuhr") + "\n" + result + "asr" + " : "
                        + cityObject.getString("asr") + "\n" + result + "maghrib" + " : "
                        + cityObject.getString("maghrib") + "\n" + result + "isha" + " : "
                        + cityObject.getString("isha") + "\n";

                cityObj.setFajr(""+cityObject.getString("fajr"));


            }

1 个答案:

答案 0 :(得分:1)

看看这个方法:

def get_week_number(year=2017, day=0):
   #day 0 means sunday
   ...
   ...
   return [1, 5, 9, 13, 18, 22, ...]  

你只是像这个public void mutePrayerTime(View view) { cityModel cityObj= new cityModel(); String fajr=cityObj.getFajr(); Log.d("LOGCAT", "" + cityObj.getFajr()); //StringBuilder newFajr = new StringBuilder(fajr); //newFajr.delete(2,5); //Log.d("newFajr", String.valueOf(newFajr)); // Intent alarm = new Intent(AlarmClock.ACTION_SET_ALARM); //alarm.putExtra(AlarmClock.EXTRA_HOUR, fajr ); } 那样创建cityObj,它的所有属性都是cityModel cityObj= new cityModel();,这就是你在日志中看到null的原因。您应该将null方法中的cityObj传递给outputTimings方法:

mutePrayerTime

然后在public void mutePrayerTime(View view,cityModel cityObj) { Log.d("LOGCAT", "" + cityObj.getFajr()); //StringBuilder newFajr = new StringBuilder(fajr); //newFajr.delete(2,5); //Log.d("newFajr", String.valueOf(newFajr)); // Intent alarm = new Intent(AlarmClock.ACTION_SET_ALARM); //alarm.putExtra(AlarmClock.EXTRA_HOUR, fajr ); }

outputTimings