为什么我的WebView不起作用?

时间:2016-04-17 05:59:35

标签: java android xml webview

嗯,我完全是初学者,我正在尝试为webview做一些练习。但是,我找不到它的问题。有人可以帮助我吗?感谢。

这是我的java类:

public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1)
    ;
   Uri uri = Uri.parse("www.google.com");
    final Intent intent = new Intent(Intent.ACTION_VIEW,uri);

    button.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(intent);
                }
            }
    );
}
}

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jinyu.webview">
<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=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

这是我的日志:

4-16 22:52:03.234 23525-23525/com.example.jinyu.webview E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.example.jinyu.webview, PID: 23525
                                                                       android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.baidu.com }
                                                                           at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
                                                                           at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
                                                                           at android.app.Activity.startActivityForResult(Activity.java:3930)
                                                                           at android.app.Activity.startActivityForResult(Activity.java:3890)
                                                                           at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
                                                                           at android.app.Activity.startActivity(Activity.java:4213)
                                                                           at android.app.Activity.startActivity(Activity.java:4181)
                                                                           at com.example.jinyu.webview.MainActivity$1.onClick(MainActivity.java:29)
                                                                           at android.view.View.performClick(View.java:5204)
                                                                           at android.view.View$PerformClick.run(View.java:21153)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-16 22:52:04.687 23525-23525/com.example.jinyu.webview I/Process: Sending signal. PID: 23525 SIG: 9

2 个答案:

答案 0 :(得分:2)

"http://""https://"添加到您的网址。

答案 1 :(得分:0)

修改(供将来访问者使用):在实际http://之前添加https://url

实现这一目标的最短途径是

public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
                }
            }
    );
}
}