开发者

Embeded Web View in Android Application

I am trying to create my own browser, such that it wil开发者_StackOverflowl use Web View to display a page of my choosing. My issue is that, although I can get Web View working as a separate application (by following the tutorial at http://developer.android.com/resources/tutorials/views/hello-webview.html), I can't get it to display within my application.

I thought that just creating a WebViewer (my implementation of Web View) would run the program.

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

    WebViewer wv = new WebViewer();
}

(WebViewer was set up to display google automatically), but all I get is a white box when I run the program. The main.xml file has a WebViewer object, and internet rights are established in the manifest.

When that didn't work, I tried just implementing Android's Web Viewer directly.

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

    WebView wv = new WebView(this);
    setContentView(wv);
    wv.loadUrl("http://stackoverflow.org/");
}

That just crashes the program as soon as I try to run it.

I know I'm very new at this, and perhaps I'm just missing something small, but what am I doing wrong?


Going back to your first attempt, what is in your main.xml? Most of the time, you should have your Views in your XML (like in the example you linked to) and you should be instantiating your views with findViewById(), (also in the example). The second attempt may be crashing due to the WebView not having any layout parameters, but I'm not 100% sure. Why not build more off of the example?


It looks like you need to find the WebView in your onCreate and do the loadUrl on that. You are also setting you content view once and then again. Unless you are doing something really crazy you should only really need to that once. Try this:

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

    WebView wv = findViewById(R.id.my_wv_id);
    wv.loadUrl("http://stackoverflow.org/");
}

The twitter android sdk actually has a really nice example on how to do this: https://github.com/sugree/twitter-android-sdk/blob/master/twitter/src/com/sugree/twitter/TwDialog.java .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜