Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6851305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:13:55+00:00 2026-05-27T01:13:55+00:00

Anytime I press the Back Key, my application crashes. I have tried to different

  • 0

Anytime I press the Back Key, my application crashes. I have tried to different ways, and they both crash throwing a NullPointerException…. any ideas?

Way 1:

public boolean onKeyDown(int keyCode, KeyEvent event) {
  // Check if the key event was the BACK key and if there's history
  if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
      myWebView.goBack();
      return true;
  }
  // If it wasn't the BACK key or there's no web page history, bubble up to the default
  // system behavior (probably exit the activity)
  return super.onKeyDown(keyCode, event);
}

Way 2:

    public void onBackPressed() {
      myWebView.goBack();
      return;
}

Stack Trace:

11-19 20:13:12.425: E/AndroidRuntime(1963): FATAL EXCEPTION: main
11-19 20:13:12.425: E/AndroidRuntime(1963): java.lang.NullPointerException
11-19 20:13:12.425: E/AndroidRuntime(1963):     at com.meanwhileinwv.android.MNWVShow.onBackPressed(MNWVShow.java:27)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at android.app.Activity.onKeyUp(Activity.java:1983)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at android.view.KeyEvent.dispatch(KeyEvent.java:1518)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at android.app.Activity.dispatchKeyEvent(Activity.java:2163)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1747)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2702)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2677)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1965)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at android.os.Looper.loop(Looper.java:143)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at android.app.ActivityThread.main(ActivityThread.java:4263)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at java.lang.reflect.Method.invokeNative(Native Method)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at java.lang.reflect.Method.invoke(Method.java:507)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-19 20:13:12.425: E/AndroidRuntime(1963):     at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:13:55+00:00Added an answer on May 27, 2026 at 1:13 am

    The only way to get the stack trace you are getting from the code you’ve shared is for myWebView to be null when myWebView.goBack() is called. To prove this to yourself, modify your onBackPressed():

    public void onBackPressed() {
        if (myWebView != null) {
            myWebView.goBack();
        }
    }
    

    The exception should go away. (Though, obviously, you still won’t get any back behavior occurring.)

    Possible causes:

    • myWebView is not a member variable in your class. You may have declared and set it in another method, which means it’s not in scope of onBackPressed.
    • myWebView isn’t the actual name of the variable that refers to your WebView
    • myWebView used to refer to your WebView but at some point was set to null again
    • The code that you think is pointing myWebView to your WebView is actually setting to null. For instance, a findViewById call that is not actually finding the WebView because you’ve specified the wrong ID will return null.
    • etc.

    If you’re still stuck, post the code where myWebView is declared and where it is assigned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Adobe AIR 3 application. For some reason, anytime I press the
So anytime someone visits this page: http://www.site.com/page/image_(.*).png They get redirected to the cdn url,
I have the following function that is supposed to trigger anytime one of the
As I understand, OSGi services can be unregistered anytime, including when they are in
Strangely enough, anytime I have an & in an HREF to indicate querystring parameters
I have this list http://pastebin.me/dde64f8c185de9dd5e429f84701a01ce Anytime you click on an image extra content appears
I am working on the application,I am having the requirement:if user long press the
Anytime I have to re-import my projects into Eclipse (if I reinstalled Eclipse, or
Question Is there a way to have a method that will always run anytime
I have two tables like that: Products(productID,categoryID,price) MarginOfProfit(categoryID,rate) Anytime we want to change prices

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.