我正在使用此网站https://github.com/johnkil/SideNavigation中的示例Sidenavigation应用程序。我有一个示例应用程序加载到我的手机上没有任何问题。我想要实现的是将菜单项链接到webview上的各种网站。在下面的代码中,我只在sidenavigation面板中有一个按钮。我尝试使用以下代码将按钮链接到webview,但是当我加载应用程序并单击按钮时,应用程序每次都会崩溃。
`case R.id.side_navigation_menu_item1:
setContentView(R.layout.tab2);
break;`
请参阅下面的完整代码。
任何帮助都会非常感激,因为我已经长时间陷入困境。
以下是应用关闭时出现的错误: 10-27 08:36:45.886:D / TilesManager(10889):来自框架的新EGLContext:5182a990 10-27 08:36:45.886:D / GLWebViewState(10889):重新启动着色器 10-27 08:36:45.922:D / GLWebViewState(10889):重新启动transferQueue 10-27 08:36:46.050:D / dalvikvm(260):GC_CONCURRENT释放2099K,19%免费23941K / 29255K,暂停2ms + 10ms,总计217ms 10-27 08:36:46.050:D / dalvikvm(260):WAIT_FOR_CONCURRENT_GC阻塞了90ms 10-27 08:36:46.050:D / dalvikvm(260):WAIT_FOR_CONCURRENT_GC阻止50ms
谢谢。
MainActivity.java
`package com.devspark.sidenavigation.sample;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.devspark.sidenavigation.sample.R;
import com.devspark.sidenavigation.ISideNavigationCallback;
import com.devspark.sidenavigation.SideNavigationView;
import com.devspark.sidenavigation.sample.News;
public class MainActivity extends SherlockActivity implements
ISideNavigationCallback {
public static final String EXTRA_TITLE = "com.devspark.sidenavigation.sample.extra.MTGOBJECT";
public static final String EXTRA_RESOURCE_ID = "com.devspark.sidenavigation.sample.extra.RESOURCE_ID";
private ImageView icon;
private SideNavigationView sideNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
icon = (ImageView) findViewById(android.R.id.icon);
sideNavigationView = (SideNavigationView) findViewById(R.id.side_navigation_view);
sideNavigationView.setMenuItems(R.menu.side_navigation_menu);
sideNavigationView.setMenuClickCallback(this);
if (getIntent().hasExtra(EXTRA_TITLE)) {
String title = getIntent().getStringExtra(EXTRA_TITLE);
int resId = getIntent().getIntExtra(EXTRA_RESOURCE_ID, 0);
setTitle(title);
icon.setImageResource(resId);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
sideNavigationView.toggleMenu();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
public void onSideNavigationItemClick(int itemId) {
switch (itemId) {
case R.id.side_navigation_menu_item1:
setContentView(R.layout.tab2);
break;
default:
return;
}
finish();
}
@Override
public void onBackPressed() {
// hide menu if it shown
if (sideNavigationView.isShown()) {
sideNavigationView.hideMenu();
} else {
super.onBackPressed();
}
}
/**
* Start activity from SideNavigation.
*
* @param title
* title of Activity
* @param resId
* resource if of background image
*/
private void invokeActivity(String title, int resId) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(EXTRA_TITLE, title);
intent.putExtra(EXTRA_RESOURCE_ID, resId);
// all of the other activities on top of it will be closed and this
// Intent will be delivered to the (now on top) old activity as a
// new Intent.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
// no animation of transition
overridePendingTransition(0, 0);
}
}`
News.java
`import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
public class News extends Activity {
private static String TAG = "WebViewTest";
private WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Entering onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView)findViewById(R.id.webview);
mWebView.clearCache(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
Log.i(TAG,"Exiting onCreate");
}
}`
acitivity_main.xml
`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="@string/image_desc"
android:src="@drawable/android" />
<com.devspark.sidenavigation.SideNavigationView
android:id="@+id/side_navigation_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>`
tab2.xml
`<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarStyle="outsideOverlay"
android:scrollbarFadeDuration="5"
android:fillViewport="true"/>
</RelativeLayout>`
side_navigation_menu.xml
`<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/side_navigation_menu_item1"
android:icon="@drawable/ic_action_side_navigation"
android:title="News"/>
</menu>`