2013-05-12 66 views
3

我的碎片存在問題。當屏幕方向改變時,我得到一個空指針異常。這裏是我的片段代碼:Android碎片:屏幕方向變化崩潰

公共類PageFragment擴展片段{

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> 

是否有人知道我錯了?此

View view = (LinearLayout)inflater.inflate(R.layout.layout_pager,container,false);

+0

你可以指向'PageFragment.java:33'嗎? – 2013-05-12 09:45:35

+0

你可以添加你的onCreate函數嗎? – 2013-05-12 09:52:55

+0

PageFragment 33: tv.setText(data.getSunrise()+「 - 」+ data.getSunset()); – user1071138 2013-05-12 10:16:56

回答

0

嘗試刪除這些

if (container == null) { 
    return null; 
    } 
+0

我試過了,但是我沒有解決。 – user1071138 2013-05-12 09:41:26

+0

你可以發佈你的'layout_pager.xml'。 – Neoh 2013-05-12 09:43:23

+0

好的我編輯了帖子! – user1071138 2013-05-12 10:01:53

3

儘量不要使用容器,也就是改變這種

View view = (LinearLayout)inflater.inflate(R.layout.layout_pager,null,false);

它應該工作。

+0

是的,刪除'if(container == null){...}' – Daniel 2013-05-15 21:08:35

+0

沒有爲我工作 – 2014-07-29 11:20:13