尝试使用多个URL启动webview Activity时出错

时间:2013-11-05 13:49:03

标签: android url webview

我正在尝试使用webview创建一个简单的应用程序。我想使用onclicklistener在单个webview中加载多个URL。

我有一个主视图,因为我有4个按钮。如果我单击一个按钮,它将在同一webview中打开其指定的URL。 但是当我在模拟器中尝试时,它就会停止。我不知道这段代码有什么问题。 这是我的代码。

package com.shiraz.shirazdesigner;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

Button aas, fcbk, twtr, ytb;
WebView webview;

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

private void initialize() {
    // TODO Auto-generated method stub
    aas = (Button)findViewById(R.id.aasBtn);
    fcbk = (Button)findViewById(R.id.fbBtn);
    twtr = (Button)findViewById(R.id.twtrBtn);
    ytb = (Button)findViewById(R.id.ytBtn);
    webview = (WebView)findViewById(R.id.webView);
    aas.setOnClickListener(this);
    fcbk.setOnClickListener(this);
    twtr.setOnClickListener(this);
    ytb.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
        case R.id.aasBtn:
            webview.loadUrl("http://www.shirazdesigner.com");
            Intent aasint = new Intent (MainActivity.this, Webview.class);
            startActivity(aasint);
            break;
        case R.id.fbBtn:
            webview.loadUrl("http://www.facebook.com/shiraz.designer");
            Intent fbint = new Intent (MainActivity.this, Webview.class);
            startActivity(fbint);
            break;
        case R.id.twtrBtn:
            webview.loadUrl("http://www.twitter.com/shirazdesigner");
            Intent twtrInt = new Intent (MainActivity.this, Webview.class);
            startActivity(twtrInt);
            break;
        case R.id.ytBtn:
            webview.loadUrl("http://www.youtube.com/shirazdesigner");
            Intent ytInt = new Intent (MainActivity.this, Webview.class);
            startActivity(ytInt);               
            break;
    }
}
}

这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shiraz.shirazdesigner"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.shiraz.shirazdesigner.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.shiraz.shirazdesigner.Webview"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

1 个答案:

答案 0 :(得分:0)

您似乎正在启动一项新活动(Webview)。 在这种情况下,

webview = (WebView)findViewById(R.id.webView);

将返回null,因为此窗口小部件位于由startActivity()启动的Webview活动的布局中。 如果您在第一个活动(MainActivity)中没有Webview小部件,则以下行将导致错误。

webview.loadUrl("http://www.shirazdesigner.com");

您可以将主要活动的URL发送到WebView活动:

Intent fbint = new Intent (MainActivity.this, Webview.class);
fbint.putExtra("url", "http://www.fsdfsdgfsdgsdgsd.com")
startActivity(fbint);

在您的WebView活动中,您将获得它并将其分配给WebView小部件

String theUrl = getIntent().getExtras().getString("url");
WebView webview = (WebView)findViewById(R.id.webView);
webview.loadUrl(theUrl );