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 8331761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:30:53+00:00 2026-06-09T02:30:53+00:00

Here is my program code. I am new to Android and Java so it

  • 0

Here is my program code. I am new to Android and Java so it may seem messy. When I press the back button after adding in this code i get a Force Close.

    package com.alexriggs.android.gansterstreetwars;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class GangsterStreetWarsActivity extends Activity {
    /** Called when the activity is first created. */
    final Activity activity = this;
    WebView myWebView;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.getSettings().setLoadWithOverviewMode(true);
        myWebView.getSettings().setUseWideViewPort(true);
        myWebView.loadUrl("http://mafia.itwontwork.net/?pk_campaign=Android&pk_kwd=MainApp");
    }
    private class MyWebViewClient extends WebViewClient {


        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("mafia.itwontwork.net")) {
                // Designate Urls that you want to load in WebView still.
                return false;
            }           
            // Otherwise, give the default behavior (open in browser)
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
        }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
    }
}

A logcat of the error is here: http://itwontwork.net/logcat/1.txt

I have narrowed it down to the line “myWebView.goBack();”. It still does not work even when i take out all the Ifs and just leave that line.

I got the Back button code from Here and it worked for 18 people. But not me 🙁

  • 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-06-09T02:30:54+00:00Added an answer on June 9, 2026 at 2:30 am

    The problem is that you’ve not actually got an instance of the WebView in the myWebView member variable, only in the local variable in the onCreate method. (This is why you have a NullPointerException when the code runs – myWebView hasn’t ever been initialized.)

    You can fix it by changing the line

    WebView myWebView = (WebView) findViewById(R.id.webview);
    

    to

    myWebView = (WebView) findViewById(R.id.webview);
    

    in onCreate. (I haven’t checked for other errors, but this should fix your problem.)

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

Sidebar

Related Questions

I am new to android,actually this is my second program.I am programming from last
I'm new here, and I just learned how to code an Android application. Right
I am trying to run the code here -- http://code.google.com/p/android-market-api/wiki/HowDownloadApps --, and have this
Here's some code (full program follows later in the question): template <typename T> T
here is the server side code of my program, the problem is, its accepting
Here's a piece of code that takes most time in my program, according to
I am writing a program in Haskell here it is the code module Main
I've got a program that worked until recently. The offending code is shown here:
First, here is the C# code and the disassembled IL: public class Program<T> {
So I've noticed some very weird behaviour from my program. Here's a simplified code

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.