我的片段有问题。当屏幕方向改变时,我得到一个空指针异常。这是我的片段代码:
公共类 PageFragment 扩展 Fragment{
private WeatherCondition data;
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
return inflateLayout(inflater, container);
}
private View inflateLayout(LayoutInflater inflater,ViewGroup container) {
View view = (LinearLayout)inflater.inflate(R.layout.layout_pager,container,false);
TextView tv = (TextView) view.findViewById(R.id.ora_data);
tv.setText(data.getSunrise()+"-"+data.getSunset());
tv=(TextView) view.findViewById(R.id.condizioni_data);
tv.setText(data.getWeather());
tv=(TextView) view.findViewById(R.id.windDirection_data);
tv.setText(data.getWindDirection());
tv=(TextView) view.findViewById(R.id.windDegree_data);
tv.setText((Double.toString(data.getWindDegree())));
tv=(TextView) view.findViewById(R.id.windSpeed_data);
tv.setText((Double.toString(data.getWindSpeed())));
tv=(TextView) view.findViewById(R.id.temperatura_data);
tv.setText(Double.toString(data.getTemperatureC())+" C");
tv=(TextView) view.findViewById(R.id.umidita_data);
tv.setText(data.getHumidity()+"%");
tv=(TextView) view.findViewById(R.id.pressione_data);
tv.setText(data.getPressure()+" hPa");
return view;
}
public void setData(WeatherCondition cond) {
data=cond;
}
}
以及调用片段的活动:
public class DetailedMeteo extends SherlockFragmentActivity{
// list contains fragments to instantiate in the viewpager
List<PageFragment> fragments = new Vector<PageFragment>();
// page adapter between fragment list and view pager
private PagerAdapter mPagerAdapter;
// view pager
private ViewPager mPager;
String index;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailedmeteo);
index=getIntent().getStringExtra("indice");
}
@Override
protected void onResume(){
super.onResume();
retrieveData(index);
}
private void retrieveData(String i) {
ArrayList<WeatherCondition> datiOrari=GetData.getGeoDetailedMeteo(i,getApplicationContext());
for(WeatherCondition cond:datiOrari){
// creating fragments and adding to list
PageFragment pg=(PageFragment) Fragment.instantiate(this,PageFragment.class.getName());
pg.setData(cond);
fragments.add(pg);
}
// creating adapter and linking to view pager
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(),fragments);
mPager = (ViewPager) super.findViewById(R.id.pager);
mPager.setAdapter(this.mPagerAdapter);
}
}
这是我的 logcat 输出:
05-12 11:21:48.695: E/AndroidRuntime(30302): FATAL EXCEPTION: main
05-12 11:21:48.695: E/AndroidRuntime(30302): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.freemeteo/com.meteo.freemeteo.DetailedMeteo}: java.lang.NullPointerException
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.app.ActivityThread.access$700(ActivityThread.java:141)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.os.Looper.loop(Looper.java:137)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-12 11:21:48.695: E/AndroidRuntime(30302): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 11:21:48.695: E/AndroidRuntime(30302): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 11:21:48.695: E/AndroidRuntime(30302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-12 11:21:48.695: E/AndroidRuntime(30302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-12 11:21:48.695: E/AndroidRuntime(30302): at dalvik.system.NativeStart.main(Native Method)
05-12 11:21:48.695: E/AndroidRuntime(30302): Caused by: java.lang.NullPointerException
05-12 11:21:48.695: E/AndroidRuntime(30302): at com.meteo.fragments.PageFragment.inflateLayout(PageFragment.java:33)
05-12 11:21:48.695: E/AndroidRuntime(30302): at com.meteo.fragments.PageFragment.onCreateView(PageFragment.java:25)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1810)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:501)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.app.Activity.performStart(Activity.java:5114)
05-12 11:21:48.695: E/AndroidRuntime(30302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
05-12 11:21:48.695: E/AndroidRuntime(30302): ... 12 more
这是 layout_pager.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/gradient"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/ora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ora"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/ora_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/condizioni_data"
android:layout_alignParentRight="true"
android:text="15-17"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:layout_below="@id/ora"
android:id="@+id/condizioni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/meteo"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:layout_below="@id/condizioni"
android:id="@+id/temperatura"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/temperatura"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:layout_below="@id/temperatura"
android:id="@+id/umidita"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/umidita"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:layout_below="@id/umidita"
android:id="@+id/pressione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pressione"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:layout_below="@id/pressione"
android:id="@+id/windSpeed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/vento"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:layout_below="@id/windSpeed"
android:id="@+id/windDegree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ventoGradi"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:layout_below="@id/windDegree"
android:id="@+id/windDirection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/windDirection"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/condizioni_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/temperatura_data"
android:layout_alignParentRight="true"
android:text="beloo bello"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/temperatura_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/umidita"
android:layout_alignParentRight="true"
android:text="15-17"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/umidita_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/temperatura_data"
android:text="70%"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/pressione_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/pressione"
android:layout_alignBottom="@+id/pressione"
android:layout_alignParentRight="true"
android:text="1024"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/windSpeed_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/pressione_data"
android:text="100kmh"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/windDegree_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/windSpeed"
android:text="270"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/windDirection_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="SW-E"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>
</ScrollView>
</LinearLayout>
有人知道我错在哪里吗?