我正在使用谷歌示例代码创建一个软键盘,我正在尝试实现一个设置首选项,允许我更改键盘的键的样式。当我尝试调用Input方法服务Preference Activity(未经编辑的示例代码)时,我得到一个空指针异常。
//show settings menu on click of menu key
ImePreferences p = new ImePreferences();
Intent i = p.getIntent();
startActivity(i);
这是我的ImePreference。
public class ImePreferences extends PreferenceActivity {
/**
* Adds intent extras so fragment opens
*/
@Override
public Intent getIntent() {
final Intent modIntent = new Intent(super.getIntent());
modIntent.putExtra(EXTRA_SHOW_FRAGMENT, Settings.class.getName());
modIntent.putExtra(EXTRA_NO_HEADERS, true);
return modIntent;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We overwrite the title of the activity, as the default one is "Voice Search".
setTitle(R.string.IME_settings);
}
public static class Settings extends InputMethodSettingsFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setInputMethodSettingsCategoryTitle(R.string.language_selection_title);
setSubtypeEnablerTitle(R.string.select_language);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.ime_preferences);
}
}
}
这是我的日志
07-09 13:36:29.577: E/AndroidRuntime(19994): FATAL EXCEPTION: main
07-09 13:36:29.577: E/AndroidRuntime(19994): java.lang.NullPointerException
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.content.Intent.<init>(Intent.java:3217)
07-09 13:36:29.577: E/AndroidRuntime(19994): at orsto.board.orstoboardv3.ImePreferences.getIntent(ImePreferences.java:19)
07-09 13:36:29.577: E/AndroidRuntime(19994): at orsto.board.orstoboardv3.Orstoboard.onKey(Orstoboard.java:450)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.inputmethodservice.KeyboardView.detectAndSendKey(KeyboardView.java:824)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.inputmethodservice.KeyboardView.onModifiedTouchEvent(KeyboardView.java:1341)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.inputmethodservice.KeyboardView.onTouchEvent(KeyboardView.java:1204)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.View.dispatchTouchEvent(View.java:5553)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2019)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1754)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2019)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1754)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2019)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1754)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2019)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1754)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2019)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1754)
07-09 13:36:29.577: E/AndroidRuntime(19994): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1930)
07-09 13:36:29.577: E/AndroidRuntime(19994): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1389)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.app.Dialog.dispatchTouchEvent(Dialog.java:738)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.inputmethodservice.SoftInputWindow.dispatchTouchEvent(SoftInputWindow.java:75)
07-09 13:36:29.577: E/AndroidRuntime(19994): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1878)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.View.dispatchPointerEvent(View.java:5733)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3104)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2666)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:900)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2675)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.os.Looper.loop(Looper.java:154)
07-09 13:36:29.577: E/AndroidRuntime(19994): at android.app.ActivityThread.main(ActivityThread.java:4624)
07-09 13:36:29.577: E/AndroidRuntime(19994): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 13:36:29.577: E/AndroidRuntime(19994): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 13:36:29.577: E/AndroidRuntime(19994): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
07-09 13:36:29.577: E/AndroidRuntime(19994): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
07-09 13:36:29.577: E/AndroidRuntime(19994): at dalvik.system.NativeStart.main(Native Method)
任何想法为什么会发生这种情况,这是我第一次将键盘作为服务构建而且让我感到难过。
由于 马克
答案 0 :(得分:0)
我认为你应该像这样得到你的意图:
Intent i = new Intent(this, ImePreferences.class);
而不是:
ImePreferences p = new ImePreferences();
Intent i = p.getIntent();
this
关键字指的是当前活动。它是Android系统,当您致电ImePreferences
时,他会实例化startActivity(i)
活动。