0

在 MyFragmentActivity.java 中,我使用 onKeyDown 方法来检测用户是否按下音量增大按钮,然后我将密钥传递给 MyFragment 类中的其他方法,在这里当我调用 myView.postInvalidate(); 我得到NPE。我错过了什么?

MyFragment.java 代码:

    public class MyFragment  extends SherlockFragment implements OnTouchListener{

public MyView myView= null;

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
            {       

                myView = new MyView(getActivity());
                myView.setOnTouchListener(this);    
                return myView;      
            }



    public void myOnKeyDown(int key_code){    


    if(key_code == KeyEvent.KEYCODE_VOLUME_UP){

    myView.postInvalidate();    //myView is null

                }

        }

MyFragmentActivity.java 代码:

public class MyFragmentActivity extends SherlockFragmentActivity{

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {


            ((MyFragment) mTabsAdapter.getItem(0)).myOnKeyDown(keyCode);

            if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
                return true;
            }

        return super.onKeyDown(keyCode, event);
    }


}

问题是当我在 MyFragment 类之外调用 myView.postInvalidate() 时,直接从 MyFragment 调用它没有问题。

日志猫:

07-28 21:48:34.222: E/AndroidRuntime(579): FATAL EXCEPTION: main
07-28 21:48:34.222: E/AndroidRuntime(579): java.lang.NullPointerException
07-28 21:48:34.222: E/AndroidRuntime(579):  at com.My.App.MyFragment.myOnKeyDown(MyFragment.java:398)
07-28 21:48:34.222: E/AndroidRuntime(579):  at com.My.App.MyFragmentActivity.onKeyDown(MyFragmentActivity.java:173)
07-28 21:48:34.222: E/AndroidRuntime(579):  at android.view.KeyEvent.dispatch(KeyEvent.java:1037)
07-28 21:48:34.222: E/AndroidRuntime(579):  at android.app.Activity.dispatchKeyEvent(Activity.java:2068)
07-28 21:48:34.222: E/AndroidRuntime(579):  at com.actionbarsherlock.app.SherlockFragmentActivity.dispatchKeyEvent(SherlockFragmentActivity.java:122)
07-28 21:48:34.222: E/AndroidRuntime(579):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
07-28 21:48:34.222: E/AndroidRuntime(579):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
07-28 21:48:34.222: E/AndroidRuntime(579):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
07-28 21:48:34.222: E/AndroidRuntime(579):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
07-28 21:48:34.222: E/AndroidRuntime(579):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 21:48:34.222: E/AndroidRuntime(579):  at android.os.Looper.loop(Looper.java:123)
07-28 21:48:34.222: E/AndroidRuntime(579):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 21:48:34.222: E/AndroidRuntime(579):  at java.lang.reflect.Method.invokeNative(Native Method)
07-28 21:48:34.222: E/AndroidRuntime(579):  at java.lang.reflect.Method.invoke(Method.java:521)
07-28 21:48:34.222: E/AndroidRuntime(579):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-28 21:48:34.222: E/AndroidRuntime(579):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-28 21:48:34.222: E/AndroidRuntime(579):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

只有当这个片段变得可见时,片段的onCreateView方法才会被调用。所以我认为你 ((MyFragment) mTabsAdapter.getItem(0)).myOnKeyDown(keyCode);在片段初始化之前调用。在任何情况下,当该事件已经发生但片段尚未初始化时,可能存在状态。所以你应该检查myView空值。

于 2013-07-29T02:41:58.327 回答