WebView错误强制关闭应用程序

时间:2013-05-30 03:23:29

标签: android webview

这是我的代码,我正在尝试从RSS提要加载已解析的数据。但我只使用google.com测试我的应用,并且出现了错误。

  public class WebsiteOpen extends Activity {

        WebView web;

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

            Intent intent = getIntent();

        String message = intent.getStringExtra("rss-url");
        web = (WebView) findViewById(R.id.webView1);
        web.getSettings().setJavaScriptEnabled(true);

        web.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                Toast.makeText(WebsiteOpen.this, description,
                        Toast.LENGTH_SHORT).show();
            }
        });

        web.loadUrl("http://google.com");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.website_open, menu);
        return true;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        web.saveState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        web.restoreState(savedInstanceState);
    }

    }

这是我的清单

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_dilc"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itcuties.multicategoryrssreader.RssTabsActivity"
            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.itcuties.multicategoryrssreader.RssChannelActivity" />
        <activity android:name="com.itcuties.multicategoryrssreader.Upfm" />
        <activity
            android:name="com.itcuties.multicategoryrssreader.listeners.WebsiteOpen"
            android:label="@string/title_activity_website_open"
            android:configChanges="orientation|screenSize" >
        </activity>
    </application>

</manifest>
请帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

我认为你可以用这个替换Intent和message的代码:

Bundle bundle = getIntent().getExtras();
String message = "";
try{
    message = bundle.getString("rss-url"");
}catch(nullpointerException e){
    //System.out.println("No String Extra");
}

但是,我认为以下代码是无用的。应该删除它。

Intent intent = getIntent();
String message = intent.getStringExtra("rss-url");

如果不起作用,请确保ID“webView1”包含在布局“activity_website_open.xml”中。