当我使用此片段切换到选项卡时,它总是崩溃。logcat 似乎说它在 `mGrid = (GridView) (getView().findViewById(R.id.gridViewRed)); 上崩溃了 知道为什么吗?
RedScorerFragment.java
public class RedScorerFragment extends SherlockFragment {
LayoutInflater infl;
GridView mGrid;
@Override
public void onCreate(Bundle savedInstanceState){
mGrid = (GridView) (getView().findViewById(R.id.gridViewRed));
mGrid.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_MOVE)
return true;
return false;
}
});
mGrid.setAdapter(new ImageAdapter(getActivity()));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
infl = inflater;
return inflater.inflate(R.layout.fragment_score_red, container, false);
}
fragment_score_red.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RedScorerFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/gridLabel__red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="@string/grid_label__red"
android:background="@color/white"
/>
<GridView
android:id="@+id/gridViewRed"
android:layout_width="wrap_content"
android:layout_height="350dp"
android:layout_alignParentLeft="true"
android:layout_below="@id/gridLabel__red"
android:columnWidth="40dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="7dp"
android:gravity="center"
android:background="@color/white"
>
</GridView>
<LinearLayout
android:id="@+id/linLayoutSide__red"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/gridView__red"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/weighted_label"
/>
<EditText
android:id="@+id/waitRed__red"
android:inputType="number"
android:maxLength="1"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:textColor="@color/red"
/>
<EditText
android:id="@+id/waitBlue__red"
android:inputType="number"
android:maxLength="1"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:textColor="@color/blue" />
</LinearLayout>
</RelativeLayout>
</ScrollView>