有问题,把按钮经过的WebView(Having problems putting button o

2019-10-18 10:11发布

我试图把一个按钮在网页视图,我发现这样做是通过XML的最简单的方法。 我的MainActivity的onCreate看起来这样的:

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);

    }

我的XML看起来是这样的:

<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>

出于某种原因,我得到一个致命的错误有一个空指针异常。 任何人有任何见解,为什么这是怎么回事?

Answer 1:

试试下面的

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

您可以findViewById设置为活动当前视图层次结构。 您需要首先将内容设置为活动。 如果不是你的初始化失败导致NullPointerException

你也需要在XML按钮,它已经在你的初始化onCreate 。 没有必要将其添加到webview

mWebview.addView(buttonClick); // not required 

还你的WebView高度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>


Answer 2:

将其更改为:

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");
    }

你必须寻找那些在它里面定义视图之前加载XML文件。 这是工作setContentView(R.layout.yourlayout)调用。



文章来源: Having problems putting button over webview