-1
我想在活動中使用的片段,我已經使用這個代碼爲Fragment
類NullPointerException異常,當我嘗試使用定製的片斷
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>
哪一行代碼的將導致NPE? –
它已經寫入:) – AndreaF
你需要使用view.findView,你沒有getView()直到返回這個方法 – sherpya