2

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?

Man Person
  • 1,122
  • 8
  • 20
  • 34

2 Answers2

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.

Nerkatel
  • 1,805
  • 16
  • 25
2

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>
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Always post the stacktrace in case of a crash. useful info about the crash is logged. – Raghunandan Jul 23 '13 at 14:20
  • Alright, I'll do so next time. The only issue I have now is when I do mWebview.loadUrl the button disappears. – Man Person Jul 23 '13 at 14:38
  • @OstapHnatyuk edited my post. this happens because you have `fill_parent` for webview which take the full height. try the above edited post. – Raghunandan Jul 23 '13 at 14:39
  • @OstapHnatyuk also make sure you don't open url in browser. your web content should be in webview. if its a browser you can't put a button there. you can check that if it opens in a browser you click back button it will navigate to your app screen. – Raghunandan Jul 23 '13 at 14:48