我正在玩这个网站https://github.com/johnkil/SideNavigation的示例 Sidenavigation 应用程序。我已经将示例应用程序加载到我的手机上,没有任何问题。我想要实现的是将菜单项链接到 webview 上的各种网站。在下面的代码中,我在侧导航面板中只有一个按钮。我尝试使用以下代码将按钮链接到 webview,但是当我加载应用程序并单击按钮时,应用程序每次都会崩溃。
`case R.id.side_navigation_menu_item1:
setContentView(R.layout.tab2);
break;`
完整代码见下文。
任何帮助将不胜感激,因为我长期以来一直在这个循环中陷入困境。
以下是应用程序关闭时出现的错误:10-27 08:36:45.886: D/TilesManager(10889): new EGLContext from framework: 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 +10 毫秒,总计 217 毫秒 10-27 08:36:46.050:D/dalvikvm(260):WAIT_FOR_CONCURRENT_GC 阻塞 90 毫秒 10-27 08:36:46.050:D/dalvikvm(260):WAIT_FOR_CONCURRENT_GC 阻塞 50 毫秒
谢谢你。
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);
}
}`
新闻.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");
}
}`
活动主文件.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>`