在我的应用程序中,我有一个导航抽屉,为不同的网站提供不同的选项。它们用字符串编码:
<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”,我希望将网页视图更改为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";
}
}
我需要添加或更改我的代码才能将其重定向到实际的网址?我应该注意,这是我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);
}
}
答案 0 :(得分:2)
我自己和OP在聊天中通过Github进行了调试,我现在真的应该回来把解决方案放在这里作为答案,所以这里是......
您遇到的问题是可变范围之一。您分别使用方法和块范围定义了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
感兴趣的话