当我切换到带有此片段的选项卡时,它总是崩溃。 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>
答案 0 :(得分:2)
您需要重新考虑找到自己观点的位置。这是因为onCreate()被称为before onCreateView()
所以你应该移动
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()));
到onCreateView()
这意味着您不需要返回通货膨胀,而是使用它,
所以你的onCreateView()应该是这样的:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
infl = inflater;
View mView = inflater.inflate(R.layout.fragment_score_red, container, false);
mGrid = (GridView) (mView.findViewById(R.id.gridViewRed));
//rest of code above
return mView;
}