我有一個代碼:運行時錯誤
主要代碼:
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):線程ID = 1:螺紋與未捕獲離開異常(group = 0xa629c288) 10-29 13:52:50.286:E/AndroidRuntime(1601):致命異常:主 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):at android.app。 ActivityThread.performLaunchActivity(ActivityThread.java:2059) E:/ AndroidRuntime(1601):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 10-29 13:52:50.286:E/AndroidRuntime(1601):at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3512) 10-29 13:52:50.286:E/AndroidRuntime(1601):at android.app.ActivityThread.access $ 700(ActivityThread.java:130) 10- 29 13:52:50.286:E/AndroidRuntime(1601):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1201) 10-29 13:52:50.286:E/AndroidRuntime(1601):at android .os.Handler.dispatchMessage(Handler.java:99) 10-29 13:52:50.286:E/AndroidRuntime(1601):at 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):at java.lang.reflect.Method.invokeNative(Native Method) 10-29 13:52:50.286:E/AndroidRuntime(1601):at java .lang.reflect.Method.invoke(Method.java:511) 10-29 13:52:50.286:E/AndroidRuntime(1601):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java :786) 10-29 13:52:50.286:E/AndroidRuntime(1601):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-29 13:52:50.286:E/AndroidRuntime(1601):在dalvik.system.NativeStart.main(本地方法) 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)E/AndroidRuntime(1601):at 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):at android.support.v4.app.FragmentManagerImpl。 moveToState(FragmentManager.java:1104) 10-29 13:52:50.286:E/AndroidRuntime(1601):at 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):at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163) 10-29 13:52:50.286:E/AndroidRuntime 1601):at android.app.Activity.performStart(Activity.java:5018) 10-29 13:52:50.286:E/AndroidRuntime(1601):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
我希望大家幫幫我,它的我最後的機會(
哪些代碼在線寫入:片段中的43 – Tejas