0

当我使用此片段切换到选项卡时,它总是崩溃。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>
4

1 回答 1

2

你需要重新考虑你在哪里找到你的观点。这是因为之前调用了 onCreate() 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;
  }
于 2012-12-23T02:13:50.623 回答