gpt4 book ai didi

android - 如何为 android webview 执行 url 更改?

转载 作者:行者123 更新时间:2023-11-29 00:18:58 26 4
gpt4 key购买 nike

在我的应用程序中,我有一个抽屉导航,其中包含针对不同网站的不同选项。这些以字符串编码:

<string name="title_section1">Google</string>
<string name="title_section2">Stackoverflow</string>
<string name="title_section3">Twitter</string>
<string name="title_section4">Facebook</string>

我也 - 显然 - 在名为 activity_app.xml 的文件中有一个 webview:

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

当应用程序打开时,此 webview 默认为名为 Nu.nl 的荷兰新闻网站:

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

mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();

WebView myWebView = (WebView) findViewById(R.id.main_webview);
String pageUrl = "http://www.nu.nl";
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl(pageUrl);
myWebView.setWebViewClient(new WebViewClient());

// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}

现在,理想情况下,如果我点击“Google”,例如,我希望 WebView 更改为 google.com。我开始为此构建代码但卡住了。我的应用程序不断崩溃,我认为这与 findViewById 有关。

@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 对此进行了调试,我真的应该在此之前回来将解决方案作为答案放在这里......

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


解决方法是将声明移至类级别,并将分配保留在之前声明的位置。

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);
}
...
}

我们最终解决它的方式实际上与上面的不同,因为有更好的设计决策需要做出,但这个解决方案解决了所述问题。如果有人感兴趣,请联系 this Github repo

关于android - 如何为 android webview 执行 url 更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24345834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com