按钮显示网址不再工作

时间:2013-06-07 10:30:32

标签: android button webview

我正在使用webView工作,但Flash视频不起作用。

经过一番搜索,我发现了一个显示视频的webview代码。 webview video

这是我的旧代码

private static final String LOG_TAG = "Web";

private WebView mWebView;
public static final String URL = "";

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.web);

    mWebView = (WebView) findViewById(R.id.webView);

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setSavePassword(false);
    webSettings.setSaveFormData(false);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportZoom(false);



    String turl = getIntent().getStringExtra(URL);

    mWebView.loadUrl(turl);
;

}

@Override
public void onBackPressed() {

    finish();
}

/**
 * Provides a hook for calling "alert" from javascript. Useful for debugging
 * your javascript.
 */
final class MyWebChromeClient extends WebChromeClient {
    @Override
    public boolean onJsAlert(WebView view, String url, String message,
            JsResult result) {
        Log.d(LOG_TAG, message);
        result.confirm();
        return true;
    }
}

所以我用源中的mainactivity替换了它并添加了

    String turl = getIntent().getStringExtra(URL);

   public static final String URL = "";

并进行了一些更改,因此它加载了我使用的类名

所以现在我有了这个

   private WebView webView;
   public static final String URL = "";
   private FrameLayout customViewContainer;
   private WebChromeClient.CustomViewCallback customViewCallback;
   private View mCustomView;
   private myWebChromeClient mWebChromeClient;
   private myWebViewClient mWebViewClient;


   /**
    * Called when the activity is first created.
    */
   @Override
   public void onCreate(Bundle icicle) {
       super.onCreate(icicle);
       setContentView(R.layout.main);
       customViewContainer = (FrameLayout) findViewById(R.id.customViewContainer);
       webView = (WebView) findViewById(R.id.webView);

       mWebViewClient = new myWebViewClient();
       webView.setWebViewClient(mWebViewClient);


       mWebChromeClient = new myWebChromeClient();
       webView.setWebChromeClient(mWebChromeClient);
       webView.getSettings().setJavaScriptEnabled(true);
       webView.getSettings().setAppCacheEnabled(true);
       webView.getSettings().setBuiltInZoomControls(true);
       webView.getSettings().setSaveFormData(true);
       String turl = getIntent().getStringExtra(URL);
       webView.loadUrl(turl);
   }

   public boolean inCustomView() {
       return (mCustomView != null);
   }

   public void hideCustomView() {
       mWebChromeClient.onHideCustomView();
   }

   @Override
   protected void onPause() {
       super.onPause();    //To change body of overridden methods use File | Settings | File Templates.
       webView.onPause();
   }

   @Override
   protected void onResume() {
       super.onResume();    //To change body of overridden methods use File | Settings | File Templates.
       webView.onResume();
   }

   @Override
   protected void onStop() {
       super.onStop();    //To change body of overridden methods use File | Settings | File Templates.
       if (inCustomView()) {
           hideCustomView();
       }
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
       if (keyCode == KeyEvent.KEYCODE_BACK) {

           if (inCustomView()) {
               hideCustomView();
               return true;
           }

           if ((mCustomView == null) && webView.canGoBack()) {
               webView.goBack();
               return true;
           }
       }
       return super.onKeyDown(keyCode, event);
   }

   class myWebChromeClient extends WebChromeClient {
       private Bitmap mDefaultVideoPoster;
       private View mVideoProgressView;

       @Override
       public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
          onShowCustomView(view, callback);    //To change body of overridden methods use File | Settings | File Templates.
       }

       @Override
       public void onShowCustomView(View view,CustomViewCallback callback) {

           // if a view already exists then immediately terminate the new one
           if (mCustomView != null) {
               callback.onCustomViewHidden();
               return;
           }
           mCustomView = view;
           webView.setVisibility(View.GONE);
           customViewContainer.setVisibility(View.VISIBLE);
           customViewContainer.addView(view);
           customViewCallback = callback;
       }

       @Override
       public View getVideoLoadingProgressView() {

           if (mVideoProgressView == null) {
               LayoutInflater inflater = LayoutInflater.from(Web.this);
               mVideoProgressView = inflater.inflate(R.layout.video_progress, null);
           }
           return mVideoProgressView;
       }

       @Override
       public void onHideCustomView() {
           super.onHideCustomView();    //To change body of overridden methods use File | Settings | File Templates.
           if (mCustomView == null)
               return;

           webView.setVisibility(View.VISIBLE);
           customViewContainer.setVisibility(View.GONE);

           // Hide the custom view.
           mCustomView.setVisibility(View.GONE);

           // Remove the custom view from its container.
           customViewContainer.removeView(mCustomView);
           customViewCallback.onCustomViewHidden();

           mCustomView = null;
       }
   }

   class myWebViewClient extends WebViewClient {
       @Override
       public boolean shouldOverrideUrlLoading(WebView view, String url) {
           return super.shouldOverrideUrlLoading(view, url);    //To change body of overridden methods use File | Settings | File Templates.
       }
   }

我使用此代码作为我的按钮在webview中打开网址

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button1:
        Intent k = new Intent(this, Web.class);
        k.putExtra(com.papers.test.Web.URL,
                "http://www.telegraaf.mobi");
        startActivity(k);
        break;


    }

}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.onlinekranten);
    View secondButton = findViewById(R.id.button1);
    secondButton.setOnClickListener(this);

}

在我的旧web.class中工作得很好但是现在当我按下按钮我得到一个fc时,有人可以帮我解决这个问题吗?

logcat的

06-07 12:38:55.310: E/AndroidRuntime(3236): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.papers.test/com.papers.test.Web}: java.lang.NullPointerException

1 个答案:

答案 0 :(得分:-1)

看起来你的按钮需要声明 我喜欢在上面创建onCreate以便在任何地方使用它

按钮secondButton;