如何在应用程序内点按钮上打开网页?

时间:2017-10-31 07:37:07

标签: android android-intent onclick webpage

我在网上搜索,并尝试了stackoverflow中的大部分类似主题,但无法解决这个问题。这就是我发帖的原因。

我想创建一个非常简单的Android应用程序。有一个按钮,如果用户点击该按钮,它应该打开该应用程序中的特定网站 。那就是它。

我已为Android 4.1设置此示例项目的设置。

编写代码后,我执行 构建/制作项目 ,然后 构建/构建APK ,然后在我的Android手机(Android 7.0)中安装此APK。

安装并打开应用程序后,我按 CONNECT 按钮,但就在那时,应用程序消失(进入后台);它没有打开网站。

以下是我的代码:

MainActivity.java

package example_company.test_2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void goToGoogle (View view) {
        goToUrl ( "http://www.google.com/");
    }

    private void goToUrl (String url) {
        Uri uriUrl = Uri.parse(url);
        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
        startActivity(launchBrowser);
    }

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="example_company.test_2.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:text="CONNECT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button"
        android:onClick="goToGoogle (MainActivity)" />
</RelativeLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example_company.test_2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="http" android:host="www.google.com" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

那么,问题是什么? 我错过了什么!?

EDIT1

根据@pleft的答案更改代码后,按下CONNECT按钮时出现logcat错误:

10-31 13:51:25.681 12178-12178/example_company.test_2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: example_company.test_2, PID: 12178
                                                                      java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                          at android.view.View.performClick(View.java:5646)
                                                                          at android.view.View$PerformClick.run(View.java:22459)
                                                                          at android.os.Handler.handleCallback(Handler.java:761)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                          at android.os.Looper.loop(Looper.java:156)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6531)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
                                                                       Caused by: java.lang.reflect.InvocationTargetException
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                          at android.view.View.performClick(View.java:5646) 
                                                                          at android.view.View$PerformClick.run(View.java:22459) 
                                                                          at android.os.Handler.handleCallback(Handler.java:761) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:98) 
                                                                          at android.os.Looper.loop(Looper.java:156) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6531) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831) 
                                                                       Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {example_company.test_2/example_company.test_2.WebViewActivity}; have you declared this activity in your AndroidManifest.xml?
                                                                          at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1850)
                                                                          at android.app.Instrumentation.execStartActivity(Instrumentation.java:1544)
                                                                          at android.app.Activity.startActivityForResult(Activity.java:4391)
                                                                          at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
                                                                          at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
                                                                          at android.app.Activity.startActivityForResult(Activity.java:4335)
                                                                          at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
                                                                          at android.app.Activity.startActivity(Activity.java:4697)
                                                                          at android.app.Activity.startActivity(Activity.java:4665)
                                                                          at example_company.test_2.MainActivity.goToUrl(MainActivity.java:25)
                                                                          at example_company.test_2.MainActivity.goToGoogle(MainActivity.java:19)
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                          at android.view.View.performClick(View.java:5646) 
                                                                          at android.view.View$PerformClick.run(View.java:22459) 
                                                                          at android.os.Handler.handleCallback(Handler.java:761) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:98) 
                                                                          at android.os.Looper.loop(Looper.java:156) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6531) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831) 

6 个答案:

答案 0 :(得分:6)

1)更新您的清单以允许互联网连接

<manifest ... >
    <uses-permission android:name="android.permission.INTERNET" />
    ...
</manifest>

2)创建webview

的布局

<强> webview.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

3)创建WebViewActivity

<强> WebViewActivity

public class WebViewActivity extends Activity {

    private WebView webView;

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

        Intent i = getIntent();
        String url= i.getStringExtra("url");
        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(url);

    }

}

4)将您的MainActivity.goToUrl代码更新为以下内容:

private void goToUrl (String url) {
    Intent intent = new Intent(this, WebViewActivity.class);
    intent.putExtra("url", url);
    startActivity(intent);
}

5)更改activity_main.xml行:

android:onClick="goToGoogle (MainActivity)"

android:onClick="goToGoogle"

6)要解决问题中发布的logcat堆栈跟踪中的错误,必须在manifest中声明新活动。所以就在线上

<activity android:name=".MainActivity">

添加以下内容:

<activity android:name=".WebViewActivity" android:theme="@android:style/Theme.NoTitleBar"/>

<强> 更新

最终工作代码如下所示:

<强> MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void goToGoogle (View view) {
        goToUrl ( "http://www.google.com/");
    }

    private void goToUrl (String url) {
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.putExtra("url", url);
        startActivity(intent);
    }

}

<强> WebViewActivity.java

package example_company.test_2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewActivity extends Activity {

    private WebView webView;

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

        Intent i = getIntent();
        String url= i.getStringExtra("url");
        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(url);

    }

}

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="example_company.test_2.MainActivity">

    <Button
        android:text="CONNECT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button"
        android:onClick="goToGoogle"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

</RelativeLayout>

<强> webview.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

<强>的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example_company.test_2">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".WebViewActivity" android:theme="@android:style/Theme.NoTitleBar"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

答案 1 :(得分:2)

在Manifest中,添加<uses-permission android:name="android.permission.INTERNET" />

&安培;在activity_main.xmlandroid:onClick="goToGoogle (MainActivity)"更改为android:onClick="goToGoogle"

由于

答案 2 :(得分:1)

两种解决方案 -

android:onClick="goToGoogle (MainActivity)"更改为android:onClick="goToGoogle"

或者

以编程方式在您的按钮上添加了一个点击监听器

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            goToGoogle(view);
        }
    });

您还可以在布局中使用webview并在该webview中加载网站

答案 3 :(得分:0)

尝试以下代码:

ImageView Button = (ImageView)findViewById(R.id.yourButtonsId);
Button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_BROWSABLE);
    intent.setData(Uri.parse("http://www.yourURL.com"));
    startActivity(intent);

  }

});

答案 4 :(得分:0)

您收到的错误是

  

java.lang.IllegalStateException:找不到方法   goToGoogle(MainActivty)(查看)在父级或祖先的上下文中   android:在视图类上定义的onClick属性   带有id&#39;按钮&#39;

的android.support.v7.widget.AppCompatButton

onclick期待一种覆盖方法,你正在传递不同的争论。

更改onClick
 android:onClick="goToGoogle (MainActivity)" 

android:onClick="goToGoogle"

答案 5 :(得分:0)

使用以下代码,它将帮助您打开。它为我工作。

webView.setWebViewClient(new WebViewClient(){

 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url){
   view.loadUrl(url);
  return true;
 }
});

您也可以尝试这种方式:

 webView = (WebView) findViewById(R.id.webView1);
 webView.getSettings().setJavaScriptEnabled(true);
 webView.loadUrl("Your URL goes here");