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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:00:11+00:00 2026-05-30T06:00:11+00:00

The app consists of two activities: first – a listview with links to HTML

  • 0

The app consists of two activities: first – a listview with links to HTML files from assets, second – a webview with this HTML file.

So there are some links in this HTML file (in the following I call it the first HTML file) leading to other HTML files from assets. After I followed one of this links I want to go back to the first HTML file. But this method shows me a white screen instead of the file’s content.

PS: I reference the second asset file from first one by an HTML link. For example (code of link in the first HTML file):

<a href="file:///android_asset/second.html">Second file</a>

Here is the WebViewActivity code:

public class WebViewActivity extends Activity implements OnClickListener {
    WebView wWebView;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (wWebView.canGoBack() == true) {
                    wWebView.goBack();
                } else {
                    finish();
                }
                return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bundle bundle = getIntent().getExtras();

        String htmlFileName = "m" + bundle.getString("defStrID") + ".html";
        Context context = getBaseContext();

        try {
            String text = readAssetTextFile(context, htmlFileName);

            wWebView = (WebView) findViewById(R.id.webview);
            String summary = "<html><body>" + text + "</body></html>";
            wWebView.getSettings().setJavaScriptEnabled(true);
            wWebView.loadDataWithBaseURL("x-data://base", summary, "text/html",
                    "utf-8", null);
        } catch (IOException e) {
            Log.e("TAG", "Exception thrown", e);
        }
    }

    public static String readAssetTextFile(Context ctx, String fileName)
            throws IOException
    {
        InputStream inputStream = ctx.getAssets().open(fileName);

        InputStreamReader inputreader = new InputStreamReader(inputStream);
        BufferedReader buffreader = new BufferedReader(inputreader);
        String line;
        StringBuilder text = new StringBuilder();

        try {
            while ((line = buffreader.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
        } catch (IOException e) {
            Log.e("TAG", "Exception thrown", e);
        }
        return text.toString();
    }
}
  • 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-30T06:00:13+00:00Added an answer on May 30, 2026 at 6:00 am

    Your problem is that the wWebView class member variable is not initialized so it is null. See this part of your code:

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

    Perfect, you are initializing, but not what you think you are: you declare a local variable for the method and initialize it. However you shadow the class member wWebView, because of the local variable with the same name. Thus you do not initialize the class member, it is null and you get your NPE. Change the above line to (note the absence of the type):

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

    EDIT I was never able to make your way of navigating between the pages work. However I did certain changes to your onCreate method and now everything seems fine on my device:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        TextView title = (TextView) findViewById(R.id.app_name);
        title.setText(getString(R.string.app_name));
    
        Button backButton = (Button) findViewById(R.id.button_back);
        backButton.setOnClickListener(this);
    
        Button infoButton = (Button) findViewById(R.id.button_info);
        infoButton.setOnClickListener(this);
    
        Bundle bundle = getIntent().getExtras();
    
        String htmlFileName = "m" + bundle.getString("defStrID") + ".html";
        String LINK_TO_ASSETS = "file:///android_asset/";
        wWebView = (WebView) findViewById(R.id.webview);
        wWebView.getSettings().setJavaScriptEnabled(true);
        wWebView.loadURL(LINK_TO_ASSETS + htmlFileName);
    }
    

    Now you need to change also the contents of the HTML files you load. First of all add the <html><body> at the beginning of each file in the assets and </body></html> at the end. Second now you will need to change your links. For example the link you have included in your question:

    <a href="second.html">Second file</a>
    

    As you see, these become now relative links. All these changes might make your readAssetTextFile useless, but I believe this is easier-to-understand code.

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

Sidebar

Related Questions

I have an app that so far consists of two Activities: The Main Menu
I have a webview app tool, which essentially consists of the webview and two
I have a .NET 4 app which consists of the app and two class
My app is document-based, but the document consists of two folders, not one file.
I have this application that consists of two phases. Queuing phase and chatting Phase.
The installer for my .NET app consists of two file MyApp.msi and setup.exe. I
My jQuery Mobile app consists of a single index.html page and contains only one
I have two NSMutableArrays, collectables and collectableViews. My app consists of a character moving
I have a page that consists of two frames: one being a flash app,
I have an iphone app. The view is consists of two children: a tableview

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.