在为Android创建CustomWebBrowser时,MainActivity.java中的问题

时间:2012-12-11 03:42:58

标签: java android browser

我正在尝试创建一个CustomWebBrowser。这是MainActivity.java中的代码。

case R.id.bookmark1:            
// send user to bookmark1                   
String bookmark1 = preferences.getString("bookmark1", "http://www.google.com");
webView.loadUrl(bookmark1);     
Toast.makeText(MainActivity.this, bookmark1, Toast.LENGTH_SHORT).show();    
break;                  
case R.id.bookmark2:                    
//send user to bookmark 2           
String bookmark2 = preferences.getString("bookmark2", "http://www.engadget.com");           
webView.loadUrl(bookmark2);
Toast.makeText(MainActivity.this, bookmark2, Toast.LENGTH_SHORT).show();    
break;

错误发生在

String bookmark1 = preferences.getString("bookmark1", "http://www.google.com"); 

String bookmark2 = preferences.getString("bookmark2", "http://www.engadget.com");

显示错误 - “首选项无法解析”。

此外,下面是我的偏好.java代码。

package com.example.webbrowser;

import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // add from resource
        addPreferencesFromResource(R.xml.preferences);
        //add activity into manifest
    }
}

该行

addPreferencesFromResource(R.xml.preferences);

显示警告“不推荐使用PreferenceActivity类型的方法addPreferencesFromResource(int)”。这与MainActivity.Java中的错误有关吗?

以下是我的preferences.xml代码

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="User settings">
        <EditTextPreference android:key="homepage" android:title="Homepage"/>
    </PreferenceCategory>
    <PreferenceCategory>
        <EditTextPreference android:key="username" android:title="Name" android:summary="Please enter your name"/>
    </PreferenceCategory>


</PreferenceScreen>

下面是我的string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">WebBrowser</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="bGo">Go</string>
    <string name="bReload">Rel</string>
    <string name="etUrl"></string>
    <string name="bookmark1"></string>
    <string name="preferences"></string>

</resources>

0 个答案:

没有答案