我有一个强制键盘显示的活动,
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
键盘正确显示,并在需要时也模糊不清。问题是,当我完成活动时,应用程序崩溃了。 如果活动从未显示键盘或在没有开始编辑文本的情况下显示它,则表示没有错误,但如果您只编写一个或多个字符,则应用程序将崩溃。怎么解决?谢谢。
用于完成活动的方法,
boto_back.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
finish();
}
});
@Override
public void onDestroy()
{
if (adMob != null)
{
// Destroy the AdView.
adMob.destroy();
}
super.onDestroy();
}
logcat的,
07-07 19:04:25.191: E/AndroidRuntime(8443): FATAL EXCEPTION: main
07-07 19:04:25.191: E/AndroidRuntime(8443): java.lang.RuntimeException: Unable to destroy activity {com.xxxx.xxxx/com.xxxx.projecte1.TabBar_iOSActivity}: java.lang.RuntimeException: Unable to destroy activity {com.xxxx.xxxx/com.xxxx.projecte1.webPush}: java.lang.NullPointerException
07-07 19:04:25.191: E/AndroidRuntime(8443): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2693)
07-07 19:04:25.191: E/AndroidRuntime(8443): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2711)
07-07 19:04:25.191: E/AndroidRuntime(8443): at android.app.ActivityThread.access$2100(ActivityThread.java:121)
07-07 19:04:25.191: E/AndroidRuntime(8443): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:976)
07-07 19:04:25.191: E/AndroidRuntime(8443): at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 19:04:25.191: E/AndroidRuntime(8443): at android.os.Looper.loop(Looper.java:130)
07-07 19:04:25.191: E/AndroidRuntime(8443): at android.app.ActivityThread.main(ActivityThread.java:3701)
07-07 19:04:25.191: E/AndroidRuntime(8443): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 19:04:25.191: E/AndroidRuntime(8443): at java.lang.reflect.Method.invoke(Method.java:507)
07-07 19:04:25.191: E/AndroidRuntime(8443): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
07-07 19:04:25.191: E/AndroidRuntime(8443): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
07-07 19:04:25.191: E/AndroidRuntime(8443): at dalvik.system.NativeStart.main(Native Method)
07-07 19:04:25.191: E/AndroidRuntime(8443): Caused by: java.lang.RuntimeException: Unable to destroy activity {com.xxxx.xxxx/com.xxxx.projecte1.webPush}: java.lang.NullPointerException
07-07 19:04:25.191: E/AndroidRuntime(8443): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2693)
07-07 19:04:25.191: E/AndroidRuntime(8443): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2603)
07-07 19:04:25.191: E/AndroidRuntime(8443): at android.app.LocalActivityManager.dispatchDestroy(LocalActivityManager.java:622)
07-07 19:04:25.191: E/AndroidRuntime(8443): at android.app.ActivityGroup.onDestroy(ActivityGroup.java:85)
07-07 19:04:25.191: E/AndroidRuntime(8443): at com.xxxx.projecte1.TabBar_iOSActivity.onDestroy(TabBar_iOSActivity.java:417)
07-07 19:04:25.191: E/AndroidRuntime(8443): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2680)
07-07 19:04:25.191: E/AndroidRuntime(8443): ... 11 more
答案 0 :(得分:2)
这是你唯一的活动............. ??
如果是......请尝试删除完成();
答案 1 :(得分:1)
你为什么不试试这种方式:
用于显示键盘:
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(inputEditText, InputMethodManager.SHOW_FORCED);
隐藏:
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(inputEditText.getWindowToken(), 0);
答案 2 :(得分:1)
我认为你的旗帜反转了:
从Android documentation,toggleSoftInput
函数具有以下参数:
public void toggleSoftInput (int showFlags, int hideFlags)
在您的使用中,您使用hideFlags代替showFlags:
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
尝试按以下方式交换它们:
inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
答案 3 :(得分:1)
作为最后的手段,您总是可以使用try catch块捕获异常,如下所示:
@Override
public void onDestroy()
{
try {
if (adMob != null)
{
// Destroy the AdView.
adMob.destroy();
}
} catch (NullPointerException e) {
e.printStackTrace(); //Or don't print it if you dont want.
}
super.onDestroy();
}
虽然,这不是最好的方法(可能是最不可取的),但它可以完成工作。由于你要离开这个活动,我不相信这会弄乱一些东西。