简体   繁体   中英

android app crashes if keyboard was shown

I have an activity that I force keyboard to appears using,

InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

keyboard appears properly and also obscured when needed. Problem is when I finish the activity, app crashes. If the activity never shows keyboard or shows it without start editing text, it is finished with no errors but if you just write one single character or more, app will crash. How to solve it? thank you.

method used to finish activity,

    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

Is this your only activity.............??

If yes......Try by removing the finish();

Why don't you try this way:

For showing keyboard:

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(inputEditText, InputMethodManager.SHOW_FORCED);

For Hiding:

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(inputEditText.getWindowToken(), 0);

I think you have the flags reversed:

From Android documentation , the toggleSoftInput function has the following parameters:

public void toggleSoftInput (int showFlags, int hideFlags) 

while in your usage, you are using the hideFlags in place of the showFlags:

inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

Try swapping them as below:

inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);

As a last resort, you can always catch the exception with a try catch block like this:

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

Although, this is not the best approach (maybe the least preferable) but it gets the job done. Since you are exiting the activty, i don't believe that this would mess something up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM