在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调用它。
logcat的:
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)
答案 0 :(得分:0)
片段的onCreateView
方法仅在此片段可见时才调用。所以我认为你在片段初始化之前调用((MyFragment) mTabsAdapter.getItem(0)).myOnKeyDown(keyCode);
。在任何情况下都可能存在此事件发生时的状态,但片段尚未初始化。因此,您应该检查myView
是否为空值。