Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigating from one page to another in a single webview Android

I'm trying to go from one page to another in an already loaded webview when the user clicks on an image. The html pages are stored in a sub-folder in the asset folder.

Here's the java code

package com.vm;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

public class VmReadPreface extends VampireMusicActivity{

private WebView wv = null;    
private int pageIndex;
private String pagePath;    
private String[] pageList = {"file:///android_asset/preface/page_8.html",
        "file:///android_asset/preface/page_9.html","file:///android_asset/preface/page_10.html",
        "file:///android_asset/preface/page_11.html"};  
private int SelectedRow;
public static final String DEBUG_TAG = "VmMusic";   

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preface);
        WebView wv;
        wv = (WebView) findViewById(R.id.web_holder);

         System.out.println(pageList[0]);
         System.out.println(pageList.length);
         pagePath=pageList[pageIndex];
         Log.i(DEBUG_TAG,"Page Path " + pagePath);

         wv.loadUrl(pagePath);


        final ImageView getNext_Page =(ImageView) findViewById(R.id.arrow_right);
        getNext_Page.setOnClickListener(NextClick);

        final ImageView getPrevious_Page =(ImageView) findViewById(R.id.arrow_left);
        getPrevious_Page.setOnClickListener(PreviousClick);
    }


    View.OnClickListener PreviousClick = new View.OnClickListener(){
        public void onClick(View v){
            GoPrevious();
        }
    };
    View.OnClickListener NextClick = new View.OnClickListener(){
        public void onClick(View v){
            GoNext();
        }
        };

    private void loadWebView(String s){
         Log.i(DEBUG_TAG,"String Is " + s);
         String finalFile = s;
         Log.i(DEBUG_TAG,"Final File Is " + finalFile);
         wv.loadUrl(finalFile);
       }

    private void GoNext(){
        int TotalRows = pageList.length;
        if((SelectedRow + 1)<TotalRows)
            {
            SelectedRow=SelectedRow +1;
            String s=pageList[SelectedRow];
            loadWebView(s);
            }
    }   

    private void GoPrevious(){
        if(SelectedRow>0)
        {
            SelectedRow = SelectedRow-1;
            String s=pageList[SelectedRow];
            loadWebView(s);
        }   
        }

    }

When the activity is launched the first page displays properly, however when I press the 'right arrow' to go to the next page, the log shows it finds the correct page (i.e. finalFile is now 'page_9') but the webview doesn't load.

dalvikm reports 'threadid=1: thread exiting with uncaught exception and then the runtime gives a fatal exception and a Null PointerException

What am I missing? Thanks

like image 1000
Hank Wilson Avatar asked Dec 28 '25 20:12

Hank Wilson


1 Answers

Delete WebView wv; from your onCreate-statement, as this makes it a local variable in onCreate

like image 87
Nick Avatar answered Dec 31 '25 10:12

Nick