我在这里有三个活动: -Main2Activity.java -InsecOnline.java -Login.java
程序从登录页面开始,从我们到Main2Activity。 打算做的是,在Main2Activity中创建一个导航抽屉,并使用导航抽屉中的菜单项导航到不同的活动。 在导航抽屉中,我有一个名为“InsecOnline Page”的菜单项,当点击它时,它应该将我们带到名为InsecOnline的Activity。但相反,它将我带回登录活动。我不明白!
Main2Activity.java
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.inseconline) {
Intent news= new Intent(this,InsecOnline.class);
startActivity(news);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Login.java
public class Login extends AppCompatActivity {
private static Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void nextActivity(View view){
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
}
}
InsecOnline.java
public class InsecOnline extends Activity {
private WebView webView;
private ProgressBar progressBar;
private LinearLayout layoutProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insec_online);
webView = (WebView) findViewById(R.id.webView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.getIndeterminateDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
layoutProgress = (LinearLayout) findViewById(R.id.layoutProgress);
webView.setVisibility(View.GONE);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
//settings.setDisplayZoomControls(false);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().endsWith("inseconline.org")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
webView.setVisibility(View.VISIBLE);
layoutProgress.setVisibility(View.GONE);
progressBar.setIndeterminate(false);
super.onPageFinished(view, url);
webView.loadUrl("javascript:(function() { " +
"document.getElementsByClassName('col-lg-4 col-sm-4')[0].style.display='none'; })()");
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
layoutProgress.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
super.onPageStarted(view, url, favicon);
}
});
if(isOnline()) {
webView.loadUrl("http://www.inseconline.org/");
} else {
String summary = "<html><body><font color='red'>No Internet Connection</font></body></html>";
webView.loadData(summary, "text/html", null);
toast("No Internet Connection.");
}
}
private void toast(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
private boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return (netInfo != null && netInfo.isConnected());
}
@Override
public void onBackPressed(){
if (webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
private class MyAppWeViewClient extends WebViewClient{
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.subinasharma.myapp"
>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="com.example.subinasharma.myapp.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Login" />
<activity
android:name=".Main2Activity"
android:label="@string/title_activity_main2"
android:theme="@style/AppTheme" />
<intent-filter>
<action android:name="com.example.subinasharma.myapp.Main2Activity" />
</intent-filter>
<activity android:name=".News" />
<activity android:name=".InsecOnline"></activity>
</application>
</manifest>
这是整个Logcat:
11-22 16:24:36.867 5375-5375/? I/art: Late-enabling -Xcheck:jni
11-22 16:24:37.037 5375-5375/com.example.subinasharma.myapp W/System: ClassLoader referenced unknown path: /data/app/com.example.subinasharma.myapp-2/lib/arm64
11-22 16:24:37.041 5375-5375/com.example.subinasharma.myapp I/InstantRun: Instant Run Runtime started. Android package is com.example.subinasharma.myapp, real application class is null.
11-22 16:24:39.902 5375-5375/com.example.subinasharma.myapp W/System: ClassLoader referenced unknown path: /data/app/com.example.subinasharma.myapp-2/lib/arm64
11-22 16:24:39.936 5375-5396/com.example.subinasharma.myapp W/art: Suspending all threads took: 8.149ms
11-22 16:24:40.402 5375-5375/com.example.subinasharma.myapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-22 16:24:40.604 5375-5375/com.example.subinasharma.myapp I/HwSecImmHelper: mSecurityInputMethodService is null
11-22 16:24:40.680 5375-5375/com.example.subinasharma.myapp E/HAL: load: id=gralloc != hmi->id=gralloc
11-22 16:24:40.798 5375-5631/com.example.subinasharma.myapp E/HAL: load: id=gralloc != hmi->id=gralloc
11-22 16:24:40.799 5375-5631/com.example.subinasharma.myapp I/OpenGLRenderer: Initialized EGL, version 1.4
11-22 16:24:40.851 5375-5631/com.example.subinasharma.myapp W/OpenGLRenderer: load: so=/system/lib64/libhwuibp.so
dlopen failed: library "/system/lib64/libhwuibp.so" not found
11-22 16:24:40.851 5375-5631/com.example.subinasharma.myapp W/OpenGLRenderer: Initialize Binary Program Cache: Load Failed
11-22 16:24:40.851 5375-5631/com.example.subinasharma.myapp E/HAL: load: id=gralloc != hmi->id=gralloc
11-22 16:24:43.445 5375-5375/com.example.subinasharma.myapp I/WebViewFactory: Loading com.google.android.webview version 54.0.2840.85 (code 284008550)
11-22 16:24:43.657 5375-5375/com.example.subinasharma.myapp I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.TokenBindingManagerAdapter>
11-22 16:24:43.658 5375-5375/com.example.subinasharma.myapp I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.TokenBindingManagerAdapter>
11-22 16:24:43.665 5375-5375/com.example.subinasharma.myapp I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.ServiceWorkerControllerAdapter>
11-22 16:24:43.665 5375-5375/com.example.subinasharma.myapp I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.ServiceWorkerControllerAdapter>
11-22 16:24:43.712 5375-5375/com.example.subinasharma.myapp I/cr_LibraryLoader: Time to load native libraries: 13 ms (timestamps 4433-4446)
11-22 16:24:43.712 5375-5375/com.example.subinasharma.myapp I/cr_LibraryLoader: Expected native library version number "54.0.2840.85", actual native library version number "54.0.2840.85"
11-22 16:24:43.746 5375-5375/com.example.subinasharma.myapp I/cr_LibraryLoader: Expected native library version number "54.0.2840.85", actual native library version number "54.0.2840.85"
11-22 16:24:43.754 5375-5375/com.example.subinasharma.myapp I/chromium: [INFO:library_loader_hooks.cc(151)] Chromium logging enabled: level = 0, default verbosity = 0
11-22 16:24:43.772 5375-5375/com.example.subinasharma.myapp I/cr_BrowserStartup: Initializing chromium process, singleProcess=true
11-22 16:24:44.347 5375-5375/com.example.subinasharma.myapp I/Choreographer: Skipped 83 frames! The application may be doing too much work on its main thread.
11-22 16:24:47.087 5375-5375/com.example.subinasharma.myapp I/Choreographer: Skipped 34 frames! The application may be doing too much work on its main thread.
11-22 16:24:49.588 5375-5375/com.example.subinasharma.myapp W/PathParser: Points are too far apart 4.000000596046461
11-22 16:24:51.261 5375-5375/com.example.subinasharma.myapp W/PathParser: Points are too far apart 4.000000596046461
11-22 16:24:51.488 5375-5375/com.example.subinasharma.myapp I/Process: Sending signal. PID: 5375 SIG: 9
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main2"
app:menu="@menu/activity_main2_drawer" />
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:1)
在您的Login.java代码中:
public void nextActivity(View view){
Intent intent = new Intent(Login.this, Main2Activity.class);
startActivity(intent);
finish();
}
在你的Main2Activity.java
中public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.inseconline) {
Intent news= new Intent(Main2Activity.this,InsecOnline.class);
startActivity(news);
}