Having problems putting button over webview

北战南征 提交于 2019-12-11 11:53:31

问题


I'm trying to place a button over a webview, and I found the easiest way to do it is through xml. My MainActivity onCreate looks as such:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button buttonClick = (Button)findViewById(R.id.playButton);

        mWebview  = (WebView)findViewById(R.id.webview);
        mWebview.getSettings().setJavaScriptEnabled(true); // enables javascript


        System.out.println("Loading Webpage...");
        new tts().execute("");
        mWebview.loadUrl("http://www.aljazeera.com/news/americas/2013/07/20137113200544375.html");
        mWebview.addView(buttonClick);
        setContentView(mWebview);

    }

My xml looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

<WebView 
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
    <Button
        android:id="@+id/playButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Pause" />

</RelativeLayout>

For some reason I'm getting a fatal error with a null pointer exception. Anyone have any insight as to why this is happening?


回答1:


Try the below

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout); // this should come before initialization
...//rest of the code
}

You can findViewById current view hierarchy set to the activity. You need to set the content to the activity first. If not your initialization fails leading to NullPointerException.

Also you have button in xml its already initialized in your onCreate. No need to add it to webview.

mWebview.addView(buttonClick); // not required 

Also you have webview height fill_parent

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<WebView 
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/playButton"
/>
<Button
    android:id="@+id/playButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Pause" />

</RelativeLayout>



回答2:


Change it to :

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourlayout);
        Button buttonClick = (Button)findViewById(R.id.playButton);

        mWebview  = (WebView)findViewById(R.id.webview);
        mWebview.getSettings().setJavaScriptEnabled(true); // enables javascript


        System.out.println("Loading Webpage...");
        new tts().execute("");
        mWebview.loadUrl("http://www.aljazeera.com/news/americas/2013/07/20137113200544375.html");
    }

You have to load the xml file before looking for View that are defined inside it. That is the work of the setContentView(R.layout.yourlayout) call.



来源:https://stackoverflow.com/questions/17812675/having-problems-putting-button-over-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!