从Fragment调用时,getActivity导致App崩溃

时间:2014-10-25 10:55:00

标签: android android-fragments android-fragmentactivity android-listfragment android-lifecycle

我正在尝试使用fragment来测试设备的屏幕方向,以了解fragment的lifeCycle是如何工作的。在下面发布的代码中,在

行中
int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();

在这里使用getActivity()最优?如果是的话,为什么我的应用程序在添加时会崩溃?请参阅下面发布的Logcat输出。

Java_Code

public class Fragment02 extends Fragment {

private final String TAG = Fragment02.class.getName();
private StringBuilder strBuilder = new StringBuilder("");

OnClickListener btn_00_Listener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        // TODO Auto-generated method stub
        int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();

        Toast mToast;

        switch(rotation) {

        case Surface.ROTATION_0:
            mToast = Toast.makeText(getActivity(), R.string.portrait, Toast.LENGTH_SHORT);
            mToast.setGravity(Gravity.CENTER, 10, 0);
            mToast.show();
            Log.i(TAG, "PORTRAIT");
            break;

        case Surface.ROTATION_90:
            mToast = Toast.makeText(getActivity(), R.string.landscape, Toast.LENGTH_SHORT);
            mToast.setGravity(Gravity.CENTER, 10, 0);
            mToast.show();
            Log.i(TAG, "LANDSCAPE");
            break;

        case Surface.ROTATION_180:
            mToast = Toast.makeText(getActivity(), R.string.reverse_portrait, Toast.LENGTH_SHORT);
            mToast.setGravity(Gravity.CENTER, 10, 0);
            mToast.show();
            Log.i(TAG, "REVERSE_PORTRAIT");
            break;

        case Surface.ROTATION_270:
            mToast = Toast.makeText(getActivity(), R.string.reverse_landscape, Toast.LENGTH_SHORT);
            mToast.setGravity(Gravity.CENTER, 10, 0);
            mToast.show();
            Log.i(TAG, "REVERSE_LANDSCAPE");
            break;

        default:
            Log.i(TAG, "REVERSE_PORTRAIT");
        }// end switch

    }
};

public void onAttach(android.app.Activity activity) {
    strBuilder.append("@onAttached()./n");
    Log.i(TAG, ""+strBuilder);
};

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    strBuilder.append("@onCreate()./n");
    Log.i(TAG, ""+strBuilder);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    strBuilder.append("@onCreateView()./n");
    Log.i(TAG, ""+strBuilder);

    View v = inflater.inflate(R.layout.activity_screen_orientation_change, container, false);
    Button btn_00 = (Button) v.findViewById(R.id.btn_00);
    btn_00.setOnClickListener(btn_00_Listener);
    return v;
}

logcat的

  10-25 12:52:19.550: E/AndroidRuntime(31627): FATAL EXCEPTION: main
10-25 12:52:19.550: E/AndroidRuntime(31627): Process: com.example.viewpagerwithactiontabstest00, PID: 31627
10-25 12:52:19.550: E/AndroidRuntime(31627): android.support.v4.app.SuperNotCalledException: Fragment Fragment02{4291abe8 #1 id=0x7f080000 android:switcher:2131230720:1} did not call through to super.onAttach()
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.View.measure(View.java:17350)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.View.measure(View.java:17350)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:382)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.View.measure(View.java:17350)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2505)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.View.measure(View.java:17350)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2175)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1316)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1513)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6388)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.Choreographer.doCallbacks(Choreographer.java:603)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.Choreographer.doFrame(Choreographer.java:573)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.os.Handler.handleCallback(Handler.java:733)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.os.Handler.dispatchMessage(Handler.java:95)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.os.Looper.loop(Looper.java:157)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at android.app.ActivityThread.main(ActivityThread.java:5293)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at java.lang.reflect.Method.invokeNative(Native Method)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at java.lang.reflect.Method.invoke(Method.java:515)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
10-25 12:52:19.550: E/AndroidRuntime(31627):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

  1. 您没有调用超级onAttach方法,这就是错误被抛出的原因。
  2. 您忘记了@Override方法的onAttach注释。
  3. 第三,您应该实现clickListener而不是将其创建为成员对象。
  4. 试试这个:

    public class Fragment02 extends Fragment implements View.OnClickListener {
    
        private final String TAG = "Fragment02";
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            Log.i(TAG, "@onAttach");
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            Log.i(TAG, "@onCreate");
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            Log.i(TAG, "@onCreateView");
            View v = inflater.inflate(R.layout.activity_screen_orientation_change, container, false);
            Button btn_00 = (Button) v.findViewById(R.id.btn_00);
            btn_00.setOnClickListener(this);
    
            return v;
        }
    
        @Override
        public void onClick(View view) {
            int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
    
            Toast mToast;
    
            switch(rotation) {
    
                case Surface.ROTATION_0:
                    mToast = Toast.makeText(getActivity(), R.string.portrait, Toast.LENGTH_SHORT);
                    mToast.setGravity(Gravity.CENTER, 10, 0);
                    mToast.show();
                    Log.i(TAG, "PORTRAIT");
                    break;
    
                case Surface.ROTATION_90:
                    mToast = Toast.makeText(getActivity(), R.string.landscape, Toast.LENGTH_SHORT);
                    mToast.setGravity(Gravity.CENTER, 10, 0);
                    mToast.show();
                    Log.i(TAG, "LANDSCAPE");
                    break;
    
                case Surface.ROTATION_180:
                    mToast = Toast.makeText(getActivity(), R.string.reverse_portrait, Toast.LENGTH_SHORT);
                    mToast.setGravity(Gravity.CENTER, 10, 0);
                    mToast.show();
                    Log.i(TAG, "REVERSE_PORTRAIT");
                    break;
    
                case Surface.ROTATION_270:
                    mToast = Toast.makeText(getActivity(), R.string.reverse_landscape, Toast.LENGTH_SHORT);
                    mToast.setGravity(Gravity.CENTER, 10, 0);
                    mToast.show();
                    Log.i(TAG, "REVERSE_LANDSCAPE");
                    break;
    
                default:
                    Log.i(TAG, "REVERSE_PORTRAIT");
            }// end switch
        }
    }