在单个webview Android中从一个页面导航到另一个页面

时间:2012-05-27 23:08:58

标签: java android webview

当用户点击图片时,我正试图在已加载的网页浏览中从一个页面转到另一个页面。 html页面存储在资产文件夹的子文件夹中。

这是java代码

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

    }

当活动启动时,第一页显示正常,但是当我按下“向右箭头”转到下一页时,日志显示它找到了正确的页面(即finalFile现在是'page_9')但是webview不加载。

dalvikm报告'threadid = 1:线程退出未捕获的异常 然后运行时给出致命异常和Null PointerException

我错过了什么? 感谢

1 个答案:

答案 0 :(得分:3)

WebView wv;语句中删除onCreate,因为这会使其成为onCreate中的 local 变量