我在开发应用时遇到了这个错误。当我在手机上启动应用程序时,它会崩溃。
onResume出了点问题。我无法得到它,有什么问题?
Android Studio正在显示这一点:
我的运行日志:
04-15 21:49:17.624 12927-12927/com.developerfromjokela.edison E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.developerfromjokela.edison, PID: 12927
java.lang.RuntimeException: Unable to resume activity {com.developerfromjokela.edison/com.developerfromjokela.edison.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3582)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3622)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2863)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1590)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6499)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at com.developerfromjokela.edison.MainActivity.onResume(MainActivity.java:132)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1355)
at android.app.Activity.performResume(Activity.java:7107)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3557)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3622)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2863)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1590)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6499)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我的主要活动:
package com.developerfromjokela.edison;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private WebView mWebView;
String newUA;
String oldUA;
public MainActivity() {
oldUA = System.getProperty("http.agent");
newUA = "Mozilla/7.0 (X11; U; Linux i686; en-US; rv:1.9.0.4)
Gecko/20170101 Firefox/7.0";
}
@SuppressLint({"ClickableViewAccessibility", "SetJavaScriptEnabled"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
if (savedInstanceState != null) {
((WebView)
findViewById(R.id.webview)).restoreState(savedInstanceState);
}
setSupportActionBar(toolbar);
if (AppStatus.getInstance(getApplicationContext()).isOnline()) {
mWebView = findViewById(R.id.webview);
mWebView.loadUrl("https://edison.fi");
mWebView.setVerticalScrollBarEnabled(true);
mWebView.setHorizontalScrollBarEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient(){
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressBar2);
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressbar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressbar.setVisibility(View.GONE);
}
});
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
} else {
Toast.makeText(getApplicationContext(), R.string.nointernetmessage , Toast.LENGTH_LONG).show();
finish();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
}
@Override
public void onResume() {
super.onResume();
Intent intent = getIntent();
String mAction = intent.getStringExtra("action");
switch (mAction) {
case "owilma":
openservwilma();
break;
case "odocs":
opengdocs();
break;
case "odrive":
opengdrive();
break;
case "oclassroom":
opengclassroom();
break;
} {
}
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
finish();
}
}
public static void clearWebViewCachesCustom(Context context) throws PackageManager.NameNotFoundException {
String dataDir = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.dataDir;
new File(dataDir + "/app_webview/").delete();
}
public void openservwilma() {
mWebView.getSettings().setUserAgentString(oldUA);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.loadUrl("https://app.edison.fi/sso/wilma/login/3");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void navtoback() {
mWebView.getSettings().setUserAgentString(oldUA);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.loadUrl("https://edison.fi");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void opengdocs() {
mWebView.getSettings().setUserAgentString(newUA);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("https://docs.google.com/document/u/0/");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void opengslides() {
mWebView.getSettings().setUserAgentString(newUA);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("https://docs.google.com/presentation/u/0/");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void opengdrive() {
mWebView.getSettings().setUserAgentString(oldUA);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.loadUrl("https://drive.google.com/drive/u/1/");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void opengclassroom() {
mWebView.getSettings().setUserAgentString(oldUA);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.loadUrl("https://classroom.google.com/u/0/");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void opengcalendar() {
mWebView.getSettings().setUserAgentString(oldUA);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("https://calendar.google.com/calendar/b/0/");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void openghangouts() {
Intent intent = getPackageManager().getLaunchIntentForPackage("com.google.android.talk");
if (intent != null) {
// We found the activity now start the activity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
// Bring user to the market or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" + "com.google.android.talk"));
startActivity(intent);
}
}
public void openmoffice() {
mWebView.getSettings().setUserAgentString(oldUA);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.loadUrl("https://www.office.com/?auth=2&home=1");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void openmword() {
mWebView.getSettings().setUserAgentString(newUA);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("https://office.live.com/start/Word.aspx?auth=2");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void openmoutlook() {
mWebView.getSettings().setUserAgentString(newUA);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("https://outlook.office365.com/owa/");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void openaboutscreen() {
Intent OpenAbout = new Intent(MainActivity.this, About.class);
MainActivity.this.startActivity(OpenAbout);
}
public void openssanopmapro() {
mWebView.getSettings().setUserAgentString(newUA);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("https://oppimisymparisto.sanomapro.fi/d2l/lp/auth/saml/login?target=https://oppimisymparisto.sanomapro.fi/d2l/home/36515");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} else if (id == R.id.backtodesktop){
navtoback();
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.opendocs) {
opengdocs();
} else if (id == R.id.openslides) {
opengslides();
} else if (id == R.id.opendrive) {
opengdrive();
} else if (id == R.id.openclassroom) {
opengclassroom();
} else if (id == R.id.opencalendar) {
opengcalendar();
} else if (id == R.id.openhangouts) {
openghangouts();
} else if (id == R.id.openoffice) {
openmoffice();
} else if (id == R.id.openword) {
openmword();
} else if (id == R.id.openoutlook) {
openmoutlook();
} else if (id == R.id.openwilma) {
openservwilma();
} else if (id == R.id.about_app) {
openaboutscreen();
} else if (id == R.id.opensanomapro) {
openssanopmapro();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:0)
可能是你的意图是null或意图没有那个字符串。
在致电String mAction = intent.getStringExtra("action");
之前,请检查if(intent != null && intent.hasString("action")
它将检查你的意图不为空的位置,意图有该字符串。
答案 1 :(得分:0)
更改onResume()方法部分的代码:
@Override
public void onResume() {
super.onResume();
if(getIntent()!=null){
Intent intent = getIntent();
String mAction = intent.getStringExtra("action");
switch (mAction) {
case "owilma":
openservwilma();
break;
case "odocs":
opengdocs();
break;
case "odrive":
opengdrive();
break;
case "oclassroom":
opengclassroom();
break;
}
}
}