0

我有一个活动,我强制使用键盘出现,

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();
      }

日志猫,

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
4

4 回答 4

2

这是你唯一的活动......??

如果是......尝试删除finish();

于 2012-07-07T16:49:40.717 回答
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);
于 2012-07-10T08:00:11.560 回答
1

我认为你的旗帜颠倒了:

从 Android文档中,该toggleSoftInput函数具有以下参数:

public void toggleSoftInput (int showFlags, int hideFlags) 

在您使用时,您正在使用 hideFlags 代替 showFlags:

inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

尝试如下交换它们:

inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
于 2012-07-10T09:33:40.557 回答
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();
  }

虽然,这不是最好的方法(也许是最不可取的),但它可以完成工作。既然你要退出活动,我不相信这会搞砸一些事情。

于 2012-07-10T19:59:22.037 回答