即使我使用返回对象的功能填充它们,我的对象仍然是空的

时间:2019-05-08 15:11:21

标签: java android xml

我使用一种在我的案例中称为dajMiPod()的函数,该函数使用齐射从api中填充来自对象的信息,然后返回一个对象,其中填充了所有这些信息。当我尝试运行该应用程序时,它崩溃了,logcat指出我试图在空对象引用上调用虚拟方法。

public class FiveWeatherDetailsActivity extends AppCompatActivity {

    RequestQueue Queue1;

    DailyForecast today;
    DailyForecast Day1;
    DailyForecast  theDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_five_weather_details);


        today = new DailyForecast();
        Day1 = new DailyForecast();


        Queue1 = Volley.newRequestQueue(this);

        today = dajMiPod(0);

//This is the line that logcat gives me an error to.... -> txtGrad.setText(city.getName() + ", " + city.getCountry());
        txtDns.setText(String.valueOf(today.getTemperature()) + "°" + " / "+String.valueOf(today.getTemperature1())+"°" );


        int iconId = WeatherIconsHelper.getIconId(today.getIcon());
        imgDns.setImageDrawable(getResources().getDrawable(iconId));


        Day1 = dajMiPod(1);
        txtDayTemp1.setText(String.valueOf(Day1.getTemperature()) + "°" + " / "+String.valueOf(Day1.getTemperature1())+"°" );
        txtDayName1.setText(Day1.getDtjedna());
        int iconId1 = WeatherIconsHelper.getIconId(Day1.getIcon());
        imgDay1.setImageDrawable(getResources().getDrawable(iconId1));
// this
//goes on fo
//each day 
// until day5

    }



//THE FUNCTION THAT RETURNS THE OBJECT //

public DailyForecast dajMiPod( final int i){
        String url = "http://openweathermap.org/data/2.5/forecast/daily?id={gradId}&appid=b6907d289e10d714a6e88b30761fae22";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url.replace("{gradId}", String.valueOf(city.getId())), null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {

                            theDay = new Daenter code hereilyForecast();

                            JSONArray jsonArray = response.getJSONArray("list");
                            JSONObject day = jsonArray.getJSONObject(i);

                            ///TEMPERATURA DAN I NOC
                            JSONObject temp = day.getJSONObject("temp");
                            int tempDan = temp.getInt("day");
                            int tempNoc = temp.getInt("night");
                            theDay.setTemperature(tempDan);
                            theDay.setTemperature1(tempNoc);

                            ///IKONA
                            JSONArray jsonArray1 = day.getJSONArray("weather");
                            JSONObject weather = jsonArray1.getJSONObject(0);
                            String ikona = weather.getString("icon");
                            theDay.setIcon(ikona);

                            ///DAN U TJEDNU
                            long dt1 = day.getLong("dt");
                            Calendar day1 = Calendar.getInstance();
                            day1.setTimeInMillis(dt1 * 1000);
                            int dayOfWeek = day1.get(Calendar.DAY_OF_WEEK);
                            String DanUTjednu = new DateFormatSymbols().getShortWeekdays()[dayOfWeek];
                            theDay.setDtjedna(DanUTjednu);



                        }catch(JSONException e){
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        Queue1.add(request);

     return theDay;
    }
}

LOG(仅红色字母部分): 2019-05-08 16:56:10.561 14167-14167 / com.example.weatherforecast E / AndroidRuntime:FATAL EXCEPTION:main     流程:com.example.weatherforecast,PID:14167     java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.weatherforecast / com.example.weatherforecast.activities.FiveWeatherDetailsActivity}:java.lang.NullPointerException:尝试调用虚拟方法'double com.example.weatherforecast.objects。在一个空对象引用上的DailyForecast.getTemperature()'         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)         在android.app.ActivityThread.-wrap12(ActivityThread.java)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)         在android.os.Handler.dispatchMessage(Handler.java:102)         在android.os.Looper.loop(Looper.java:154)         在android.app.ActivityThread.main(ActivityThread.java:6077)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)      原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'double com.example.weatherforecast.objects.DailyForecast.getTemperature()'         在com.example.weatherforecast.activities.FiveWeatherDetailsActivity.onCreate(FiveWeatherDetailsActivity.java:105)         在android.app.Activity.performCreate(Activity.java:6662)         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)

我希望功能可以使用该函数将今天和day1的信息填满。

0 个答案:

没有答案