我有一个代码:
主要代码:
package com.example.m.i.f.projectsandroidtestapp;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;
import android.widget.GridView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import com.directionalviewpager.DirectionalViewPager;
import com.example.m.i.f.projectsandroidtestapp.WeatherHttpClient;
import com.example.m.i.f.projectsandroidtestapp.adapter.DailyForecastPageAdapter;
import com.example.m.i.f.projectsandroidtestapp.fragment.DayForecastFragment;
import com.example.m.i.f.projectsandroidtestapp.model.Weather;
import com.example.m.i.f.projectsandroidtestapp.model.WeatherForecast;
public class ForecastWeatherActivity extends SherlockFragment {
Parcelable state;
TextView cityText;
TextView condDescr;
TextView temp;
TextView press;
TextView windSpeed;
TextView minmaxTempNow;
TextView hum;
TextView textTemp;
TextView textHum;
TextView textPress;
TextView textWind;
TextView date;
Button Update;
String dateSt;
GridView GridV;
String humidT;
String cityT;
String tempT;
String descrT;
String mmT;
String windT;
String pressT;
public JSONWeatherTask mt;
public JSONForecastWeatherTask ft;
JSONWeatherTask task;
JSONForecastWeatherTask task1;
WeatherForecast forecast;
Weather weather;
Bundle bundle;
final String LOG_TAG = "myLogs";
DayForecastFragment fragment1;
//количесво дней для прогноза
static String forecastDaysNum = "7";
ViewPager pager;
final String city = "Kirovohrad";
final String lang = "en";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.weathertab, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//setContentView(R.layout.weathertab);
//дата
SimpleDateFormat sdf = new SimpleDateFormat("d MMMMMM, EEEEE");
dateSt = sdf.format(new Date());;
//подключение элементов
date = (TextView) getView().findViewById(R.id.datenow);
Update = (Button) getView().findViewById(R.id.update);
textWind = (TextView) getView().findViewById(R.id.textWind);
textPress = (TextView) getView().findViewById(R.id.textPress);
textHum = (TextView) getView().findViewById(R.id.textHum);
textTemp = (TextView) getView().findViewById(R.id.textTemp);
cityText = (TextView) getView().findViewById(R.id.cityText);
temp = (TextView) getView().findViewById(R.id.temp);
condDescr = (TextView) getView().findViewById(R.id.skydesc);
pager = (ViewPager) getView().findViewById(R.id.pager);
minmaxTempNow = (TextView) getView().findViewById(R.id.minmaxTempNow);
hum = (TextView) getView().findViewById(R.id.hum);
press = (TextView) getView().findViewById(R.id.press);
windSpeed = (TextView) getView().findViewById(R.id.windSpeed);
//конпка обновить
Update.setOnClickListener (new OnClickListener() {
public void onClick(View v) {
mt = new JSONWeatherTask();
mt.execute(new String[]{city,lang});
ft = new JSONForecastWeatherTask();
ft.execute(new String[]{city,lang, forecastDaysNum});
}
});
bundle = new Bundle();
//запуск
task = new JSONWeatherTask();
task.execute(new String[]{city,lang});
task1 = new JSONForecastWeatherTask();
task1.execute(new String[]{city,lang, forecastDaysNum});
}
//класс погоды
private class JSONWeatherTask extends AsyncTask<String, Void, Weather> {
@Override
protected Weather doInBackground(String... params) {
Weather weather = new Weather();
String data = ( (new WeatherHttpClient()).getWeatherData(params[0], params[1]));
try {
weather = JSONWeatherParser.getWeather(data);
System.out.println("Weather ["+weather+"]");
} catch (JSONException e) {
e.printStackTrace();
}
return weather;
}
protected void onPostExecute (Weather weather) {
super.onPostExecute(weather);
//присвоение значений
humidT =(" " + weather.currentCondition.getHumidity() + "%");
cityT = (weather.location.getCity());
tempT = ("" + Math.round((weather.temperature.getTemp() - 275.15 )) + " C");
descrT = (weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescr() + ")");
mmT = (" " + Math.round(weather.temperature.getMinTemp() -275.15) + "/" + Math.round(weather.temperature.getMaxTemp() - 275.15));
pressT = (" " + weather.currentCondition.getPressure() + " hPa");
windT = (" " + weather.wind.getSpeed() + " mps");
date.setText(dateSt);
textWind.setText("wind: ");
textPress.setText("pressure: ");
textHum.setText("humidity: ");
textTemp.setText("temperature: ");
cityText.setText(cityT);
temp.setText(tempT);
condDescr.setText(descrT);
minmaxTempNow.setText(mmT);
hum.setText(humidT);
press.setText(pressT);
windSpeed.setText(windT);
}
}
//класс прогноза погоды
private class JSONForecastWeatherTask extends AsyncTask<String, Void, WeatherForecast> {
@Override
protected WeatherForecast doInBackground(String... params) {
String data = ( (new WeatherHttpClient()).getForecastWeatherData(params[0], params[1], params[2]));
WeatherForecast forecast = new WeatherForecast();
try {
forecast = JSONWeatherParser.getForecastWeather(data);
System.out.println("Weather ["+forecast+"]");
} catch (JSONException e) {
e.printStackTrace();
}
return forecast;
}
@Override
protected void onPostExecute(WeatherForecast forecastWeather) {
super.onPostExecute(forecastWeather);
DailyForecastPageAdapter adapter = new DailyForecastPageAdapter(Integer.parseInt(forecastDaysNum), getActivity().getSupportFragmentManager(), forecastWeather);
state = adapter.saveState();
adapter.restoreState(state, null);
pager.setAdapter(adapter);
state = adapter.saveState();
}
}
}
分段:
package com.example.m.i.f.projectsandroidtestapp.fragment;
import com.example.m.i.f.projectsandroidtestapp.R;
import com.example.m.i.f.projectsandroidtestapp.model.DayForecast;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DayForecastFragment extends Fragment {
String temp;
String desc;
DayForecast dayForecast;
public DayForecastFragment() {}
public void setForecast(DayForecast dayForecast) {
this.dayForecast = dayForecast;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dayforecast_fragment, container, false);
//задание значений
if( savedInstanceState != null ) {
savedInstanceState.getString("TEMP");
savedInstanceState.getString("DESC");
savedInstanceState.get(getTag());
}
TextView tempView = (TextView) v.findViewById(R.id.tempForecast);
TextView descView = (TextView) v.findViewById(R.id.skydescForecast);
//температура мин/ макс
temp = (int) (dayForecast.forecastTemp.min - 275.15) + "/" + (int) (dayForecast.forecastTemp.max - 275.15) + " C" ;//облачность
desc = dayForecast.weather.currentCondition.getDescr();
tempView.setText(temp);
descView.setText(desc);
return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("TEMP", temp);
outState.putString("DESC", desc);
outState.putAll(outState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( savedInstanceState != null ) {
savedInstanceState.getString("TEMP");
savedInstanceState.getString("DESC");
savedInstanceState.get(getTag());
}
}
}
适配器:
package com.example.m.i.f.projectsandroidtestapp.adapter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import com.example.m.i.f.projectsandroidtestapp.fragment.DayForecastFragment;
import com.example.m.i.f.projectsandroidtestapp.model.DayForecast;
import com.example.m.i.f.projectsandroidtestapp.model.WeatherForecast;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
public class DailyForecastPageAdapter extends FragmentStatePagerAdapter {
int numDays;
FragmentManager fm;
WeatherForecast forecast;
//формат даты
static SimpleDateFormat sdf = new SimpleDateFormat("E, dd-MM");
public DailyForecastPageAdapter(int numDays, FragmentManager fm, WeatherForecast forecast) {
super(fm);
this.numDays = numDays;
this.fm = fm;
this.forecast = forecast;
}
// заголовок страницы
public CharSequence getPageTitle(int position) {
Date d = new Date();
Calendar gc = new GregorianCalendar();
gc.setTime(d);
gc.add(GregorianCalendar.DAY_OF_MONTH, position);
return sdf.format(gc.getTime());
}
@Override
public Fragment getItem(int num) {
DayForecast dayForecast = (DayForecast) forecast.getForecast(num);
DayForecastFragment f = new DayForecastFragment();
f.setForecast(dayForecast);
return f;
}
@Override
public int getCount() {
return numDays;
}
public void restoreState(Parcelable arg0, ClassLoader arg1){
}
public Parcelable saveState(){
return null;
}
}
现在是我的屏幕旋转问题。我阅读了许多关于使用片段保存状态的示例,但无法理解。当我旋转屏幕时出现错误“应用已停止”
有LogCat:
10-29 13:52:50.262: W/dalvikvm(1601): threadid=1: 线程退出未捕获异常 (group=0xa629c288) 10-29 13:52:50.286: E/AndroidRuntime(1601): 致命异常: main 10-29 13:52:50.286: E/AndroidRuntime(1601): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.mifprojectsandroidtestapp/com.example.mifprojectsandroidtestapp.MainActivity}: java.lang.NullPointerException 10- 29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app。 ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3512) 10-29 13:52:50.286: E/Android运行时(1601):在 android.app.ActivityThread.access$700(ActivityThread.java:130) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201) 10 -29 13:52:50.286: E/AndroidRuntime(1601): 在 android.os.Handler.dispatchMessage(Handler.java:99) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.os .Looper.loop(Looper.java:137) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.ActivityThread.main(ActivityThread.java:4745) 10-29 13:52:50.286 : E/AndroidRuntime(1601): 在 java.lang.reflect.Method.invokeNative(Native Method) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 java.lang.reflect.Method.invoke(方法.java:511) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 10-29 13:52:50 .286: E/AndroidRuntime(1601): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 dalvik.system.NativeStart .main(Native Method) 10-29 13:52:50.286: E/AndroidRuntime(1601): 由: java.lang.NullPointerException 10-29 13:52:50.286: E/AndroidRuntime(1601): at com.example .mifprojectsandroidtestapp.fragment.DayForecastFragment.onCreateView(DayForecastFragment.java:43) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:1478) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 10-29 13:52:50.286: E/AndroidRuntime(1601) :在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 10-29 13:52:50。286: E/AndroidRuntime(1601): 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.support.v4 .app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552) 10- 29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app。 Activity.performStart(Activity.java:5018) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)java:1086) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877) 10-29 13:52:50.286: E/ AndroidRuntime(1601): 在 android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.Instrumentation.callActivityOnStart(Instrumentation .java:1163) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.Activity.performStart(Activity.java:5018) 10-29 13:52:50.286: E/AndroidRuntime(1601) ): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)java:1086) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877) 10-29 13:52:50.286: E/ AndroidRuntime(1601): 在 android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.Instrumentation.callActivityOnStart(Instrumentation .java:1163) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.Activity.performStart(Activity.java:5018) 10-29 13:52:50.286: E/AndroidRuntime(1601) ): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)在 android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.Activity.performStart(Activity.java:5018) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android. app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)在 android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android.app.Activity.performStart(Activity.java:5018) 10-29 13:52:50.286: E/AndroidRuntime(1601): 在 android. app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)performLaunchActivity(ActivityThread.java:2032)performLaunchActivity(ActivityThread.java:2032)
我希望你能帮助我,这是我最后的机会(