2014-04-24 66 views
-1

我想在活動中使用的片段,我已經使用這個代碼爲FragmentNullPointerException異常,當我嘗試使用定製的片斷

public class MyFragment extends Fragment { 
    private TextView txtT1, txtT2; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.my_fragment, container, false); 

     txtT1 = (TextView) getView().findViewById(R.id.txtT1); 
     txtT2 = (TextView) getView().findViewById(R.id.txtT2); 

     return view; 
    } 
} 

NullPointerException日誌正好顯示錯誤在這些線路上

 txtT1 = (TextView) getView().findViewById(R.id.txtT1); 
     txtT2 = (TextView) getView().findViewById(R.id.txtT2); 

命名爲my_fragment的XML片段是這樣的

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 
android:layout_width="match_parent" android:layout_height="match_parent"> 


    <LinearLayout 
     android:layout_width="380dp" 
     android:layout_height="100dp" 
     android:layout_alignParentBottom="true" 
     android:background="@drawable/background" 
     android:layout_centerHorizontal="true" 
     android:orientation="vertical" 
     android:id="@+id/BoxFragment" 
     android:layout_margin="5dp" 
     android:layout_gravity="center_horizontal"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/title" 
      android:gravity="center_horizontal" 
      android:id="@+id/txtTitle" 
      android:textSize="13sp"/> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/scrollView" 
      android:layout_gravity="center_horizontal" 
      android:fillViewport="true" 
      android:paddingLeft="6dp" 
      android:paddingRight="6dp" 
      > 

      <HorizontalScrollView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/horizontalScrollView" 
       android:fillViewport="true"> 

       <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:gravity="center"> 

        <LinearLayout 
         android:orientation="horizontal" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_horizontal" 
         android:id="@+id/summaryRow1"> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceSmall" 
          android:text="@string/sample1" 
          android:id="@+id/titlT1" 
          android:textSize="13sp"/> 

         <TextView 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceSmall" 
          android:text="..." 
          android:id="@+id/txtT1" 
          android:gravity="center_horizontal" 
          android:textSize="12sp" 
          android:layout_marginLeft="3dp"/> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceSmall" 
          android:text="@string/sample2" 
          android:id="@+id/titlT2" 
          android:textSize="13sp" 
          android:layout_marginLeft="5dp"/> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textAppearance="?android:attr/textAppearanceSmall" 
          android:text="..." 
          android:id="@+id/txtT2" 
          android:textSize="12sp" 
          android:layout_marginLeft="3dp"/> 

        </LinearLayout> 

       </LinearLayout> 
      </HorizontalScrollView> 
     </ScrollView> 

    </LinearLayout> 


</LinearLayout> 
+0

哪一行代碼的將導致NPE? –

+0

它已經寫入:) – AndreaF

+0

你需要使用view.findView,你沒有getView()直到返回這個方法 – sherpya

回答

2

getView()返回View,返回onCreateView(),因此當您撥打時,onCreateView()應該爲空。

嘗試改變這一行

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

    txtT1 = (TextView) getView().findViewById(R.id.txtT1); 
    txtT2 = (TextView) getView().findViewById(R.id.txtT2); 

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

    txtT1 = (TextView) view.findViewById(R.id.txtT1); 
    txtT2 = (TextView) view.findViewById(R.id.txtT2); 
+0

謝謝這是錯誤的原因。倒數後我會接受你的回答 – AndreaF

相關問題