Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为android webview执行url更改?_Android_Webview - Fatal编程技术网

如何为android webview执行url更改?

如何为android webview执行url更改?,android,webview,Android,Webview,在我的应用程序中,我有一个导航抽屉,可以为不同的网站提供不同的选项。这些代码以字符串形式编码: <string name="title_section1">Google</string> <string name="title_section2">Stackoverflow</string> <string name="title_section3">Twitter</string> <string name="tit

在我的应用程序中,我有一个导航抽屉,可以为不同的网站提供不同的选项。这些代码以字符串形式编码:

<string name="title_section1">Google</string>
<string name="title_section2">Stackoverflow</string>
<string name="title_section3">Twitter</string>
<string name="title_section4">Facebook</string>
现在,理想的情况是,如果我点击“谷歌”,例如,我希望网络视图更改为Google.com。我开始为这个构建代码,但是被卡住了。我的应用程序不断崩溃,我认为这与
findviewbyd
有关

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    //FragmentManager fragmentManager = getSupportFragmentManager();
    //fragmentManager.beginTransaction()
            //.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
            //.commit();
    int id = position.getId ();
    if (id == 'title_section1') {
        String pageUrl = "http://www.google.com";
    } else if (id == 'title_section2') {
        String pageUrl = "http://www.stackoverflow.com";
    } else if (id == 'title_section3') {
        String pageUrl = "http://twitter.com";
    } else if (id == 'title_section4') {
        String pageUrl = "http://facebook.com";
    }
}
我需要对代码添加或更改什么才能使其重定向到实际的URL?我应该注意到,这是我5年PHP经验后的第一个android应用程序

编辑:我添加了崩溃日志。它指出错误在xml/layout文件中,但在我向文件添加
findViewById
时会出现错误,因此我认为这一定是导致错误的原因

06-21 23:13:20.580  19172-19172/test.webviewdrawer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: test.webviewdrawer, PID: 19172
java.lang.RuntimeException: Unable to start activity ComponentInfo{test.webviewdrawer/test.webviewdrawer.login}: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
        at android.app.ActivityThread.access$800(ActivityThread.java:156)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5872)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:375)
        at android.app.Activity.setContentView(Activity.java:1997)
        at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
        at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
        at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
        at test.webviewdrawer.login.onCreate(login.java:48)
        at android.app.Activity.performCreate(Activity.java:5312)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
更新2:从fragment.java文件调用main.java文件中的方法
onNavigationDrawerItemSelected()
,这可能很重要

private void selectItem(int position) {
    mCurrentSelectedPosition = position;
    if (mDrawerListView != null) {
        mDrawerListView.setItemChecked(position, true);
    }
    if (mDrawerLayout != null) {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
    if (mCallbacks != null) {
        mCallbacks.onNavigationDrawerItemSelected(position);
    }
}

我和OP在聊天和Github中调试了这个问题,我真的应该在现在之前回来把解决方案放在这里作为答案,所以它就在这里

您遇到的问题是范围可变。您分别使用方法和块范围定义了WebView和PageURL。这意味着当你以后去利用它们时,它们已经不存在了


解决方法是将声明移动到类级别,并将赋值保持在先前声明的位置

public class MyActivity extends Activity
{
    private WebView myWebView;
    private String pageUrl;

    @Override
    public void onCreate(...)
    {
        ...
        myWebView = (WebView) findViewById(R.id.main_webview);
        pageUrl = "http://nu.nl";  // default page
        ...
    }

    ....
    @Override
    public void onNavigationDrawerItemSelected(int position) {
        switch (position)
        {
            case 0:
                pageUrl = "http://www.google.com";
                break;
            case 1:
                pageUrl = "http://www.stackoverflow.com";
                break;
            case 2:
                pageUrl = "http://twitter.com";
                break;
            case 3:
                pageUrl = "http://facebook.com";
                break;
            default:
                Log.e(TAG, "Unhandled navigation selection: " + position);
                break;
        }
        // preformed asynchronously but this here just for demonstrative purposes
        webView.loadUrl(pageUrl);
    }
    ...
}


我们最终解决它的方式实际上与上面的不同,因为需要做出更好的设计决策,但是这个解决方案解决了前面提到的问题。如果有人对此感兴趣,请在您的问题中添加您的故障日志/堆栈跟踪,这样我们就可以了解您的情况。(同时指出您上面代码中stacktrace中引用的代码行)我在问题中添加了它。我认为您的问题是,您试图使用活动中片段布局中的视图。看看你能不能把它分类。回来告诉我你进展如何,这样我们就可以继续排除故障或将此标记为重复。我认为这是另一种方式-如果可能的话。导航抽屉是一个片段,webview位于活动中。片段调用该方法。再说一次,我不知道我说的是否有道理。我把它加到了问题上。
private void selectItem(int position) {
    mCurrentSelectedPosition = position;
    if (mDrawerListView != null) {
        mDrawerListView.setItemChecked(position, true);
    }
    if (mDrawerLayout != null) {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
    if (mCallbacks != null) {
        mCallbacks.onNavigationDrawerItemSelected(position);
    }
}
public class MyActivity extends Activity
{
    private WebView myWebView;
    private String pageUrl;

    @Override
    public void onCreate(...)
    {
        ...
        myWebView = (WebView) findViewById(R.id.main_webview);
        pageUrl = "http://nu.nl";  // default page
        ...
    }

    ....
    @Override
    public void onNavigationDrawerItemSelected(int position) {
        switch (position)
        {
            case 0:
                pageUrl = "http://www.google.com";
                break;
            case 1:
                pageUrl = "http://www.stackoverflow.com";
                break;
            case 2:
                pageUrl = "http://twitter.com";
                break;
            case 3:
                pageUrl = "http://facebook.com";
                break;
            default:
                Log.e(TAG, "Unhandled navigation selection: " + position);
                break;
        }
        // preformed asynchronously but this here just for demonstrative purposes
        webView.loadUrl(pageUrl);
    }
    ...
}