我需要在用户按下音量增大按钮时发出通知,我无法直接在sherlockfragment上获得keydown,所以我创建了一个从sherlockfragmentactivity调用的方法,但我得到一个空指针异常intent.putExtras(束)
这是我的SherlockFragmentActivity的代码:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
((MyFragment) mTabsAdapter.getItem(0)).myOnKeyDown(keyCode);
return super.onKeyDown(keyCode, event);
}
SherlockFragment中的代码:
@Override
public void onActivityCreated (Bundle savedInstanceState)
{
intent = new Intent(getActivity(), NotifyService.class);
}
public void myOnKeyDown(int key_code){
if(key_code == KeyEvent.KEYCODE_VOLUME_UP){
notifyBar();
}
}
服务意图方法:
public void notifyBar(){
Bundle bundle = new Bundle();
bundle.putLong("data1", d1);
bundle.putLong("data2", d2);
intent.putExtras(bundle); // NPE HERE
getActivity().startService(intent);
}
Log cat:
07-28 00:04:28.587: E/AndroidRuntime(1737): FATAL EXCEPTION: main
07-28 00:04:28.587: E/AndroidRuntime(1737): java.lang.NullPointerException
07-28 00:04:28.587: E/AndroidRuntime(1737): at com.my.app.MyFragment.notifyBar(MyFragment.java:692)
07-28 00:04:28.587: E/AndroidRuntime(1737): at com.my.app.MyFragment.myOnKeyDown(MyFragment.java:393)
07-28 00:04:28.587: E/AndroidRuntime(1737): at com.my.app.MyFragmentActivity.onKeyDown(MyFragmentActivity.java:171)
07-28 00:04:28.587: E/AndroidRuntime(1737): at android.view.KeyEvent.dispatch(KeyEvent.java:1037)
07-28 00:04:28.587: E/AndroidRuntime(1737): at android.app.Activity.dispatchKeyEvent(Activity.java:2068)
07-28 00:04:28.587: E/AndroidRuntime(1737): at com.actionbarsherlock.app.SherlockFragmentActivity.dispatchKeyEvent(SherlockFragmentActivity.java:122)
07-28 00:04:28.587: E/AndroidRuntime(1737): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
07-28 00:04:28.587: E/AndroidRuntime(1737): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
07-28 00:04:28.587: E/AndroidRuntime(1737): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
07-28 00:04:28.587: E/AndroidRuntime(1737): at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
07-28 00:04:28.587: E/AndroidRuntime(1737): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 00:04:28.587: E/AndroidRuntime(1737): at android.os.Looper.loop(Looper.java:123)
07-28 00:04:28.587: E/AndroidRuntime(1737): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 00:04:28.587: E/AndroidRuntime(1737): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 00:04:28.587: E/AndroidRuntime(1737): at java.lang.reflect.Method.invoke(Method.java:521)
07-28 00:04:28.587: E/AndroidRuntime(1737): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-28 00:04:28.587: E/AndroidRuntime(1737): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-28 00:04:28.587: E/AndroidRuntime(1737): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您能告诉我们更多信息吗? 有两个类:
片段(包含您在FragmentActivity中单击按钮后要处理的控件)
FragmentActivity(包含您要点击并通知片段的按钮)
如果那是你想要的,我可以给你一个建议:
你应该创建一个这样的界面:
interface INotifyBar {
void notify(Object valueForTransfer); // If you want to transfer :D
}
在 FragmentActivity 中 就在这里,请创建一个INotifyBar实例
private INotifyBar mINotifyBar;
onKeyDown() {
mINotifyBar.notify(keyCode); // Here is just example. :D
}
并在片段中实现notify();
@Override
public void notify(Object obj) {
// Handle something with or without obj. It's up to you.
}
我可以比你更多地帮助你,但我需要了解更多。 :d