There might be a bit of a pre-cursor to this so I better explain that too. I want only the webview to show an action bar. So in the sytles.xml I called this for the base theme:
parent="android:style/Theme.Holo.Light.NoActionBar"
I assigned that theme to my MainActivity, NOT to the entire application.
Since I want an actionbar in my WebView activity I assigned the following to that activity in the manifest:
<activity
android:name=".WebViewActivity"
android:theme="@style/Theme.Sherlock.Light">
</activity>
I've made sure to call the internet permission in my manifest and here is the Activity code and layout file for the webview:
package com.name.here; //Removed for this question
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends SherlockActivity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webster);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
//webView.loadUrl("http://www.google.com");
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
url ="http://google.com";
view.loadUrl(url);
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.AboutMenuItem:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
and the layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
So as you can see, I've tried to open Google. The problem is that it loads a blank page. Well, the action bar loads just fine but the rest of the activity, where the WebView is supposed to load Google is empty. This is what I'm left with:
I should note that the action bar DOES populate with the item and the app's name. I just took those out in this image. The action bar loads just fine, but that's all that loads.
The Logcat provides nothing, no errors or warnings. Any help appreciated. I thought setting up a webview would be quite simple.
Your code for shouldOverrideUrlLoading
is broken. It should return true or false, and not do its own loading. The view.loadUrl()
will simply trigger a call back to shouldOverrideUrlLoading
and you wind up with a crash.
Do this instead:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// do something to the url here if needed
return false;
}