我有这个错误我尝试做了很多事情但我找不到解决方案,当我第一次启动应用程序时我获得登录活动并正常工作,我关闭应用程序并重新开始,然后应用程序崩溃,这是堆栈跟踪:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Application.getApplicationContext()' on a null object reference
at com.rrms.jrperera.rapidsentry.poco.AppManager.isConnected(AppManager.java:70)
at com.rrms.jrperera.rapidsentry.activities.MainActivity.CheckInternetConnection(MainActivity.java:54)
at com.rrms.jrperera.rapidsentry.activities.MainActivity.HandleSignIn(MainActivity.java:61)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
当我从android studio启动应用程序时,这个问题不会发生。 当我在某个时刻启动应用程序时,我启动它在后台运行的服务,方法的代码:isConnected和CheckInternetConnection正在一个单独的类中实现,我用它来管理所有应用程序:
public class AppManager {
public static final String SESSION_DATA = "com.rrms.jrperera.rapidsentry.MiOSSession";
public static final String SERVICEURL = "https://xxxxxxx.com/RapidSentry/MiOSService.svc";//https://xxxxxxx.com/RapidSentry/MiOSService.svc//http://10.10.20.137/RapidMiOSApi/MiOSService.svc
private static AppManager manager = null;
private static Application application;
private Boolean isServiceRunning = null;
protected AppManager(Application app) {
this.setApplication(app);
}
public static AppManager getManager(Application app) {
if (manager == null) {
manager = new AppManager(app);
}
return manager;
}
public Application getApplication() {
return application;
}
public void setApplication(Application application) {
this.application = application;
}
public boolean isConnected() {
boolean connected = true;
ConnectivityManager connMgr = (ConnectivityManager)
getApplication().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
connected = true;
return connected;
}
}
当应用程序崩溃时,我再次启动应用程序并再次正常工作。 请知道这是怎么回事? 这是活动代码:
public class MainActivity extends AppCompatActivity {
protected AppManager manager = null;
protected EditText tbLoginUser = null;
protected EditText tbLoginPassword = null;
protected CheckBox cbLoginSaveCredentials = null;
protected ProgressBar pbLogin = null;
protected Button btSignIn = null;
protected Identity myIdentiy = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isMyServiceRunning();
tbLoginUser = (EditText) findViewById(R.id.tb_login_user);
tbLoginPassword = (EditText) findViewById(R.id.tb_login_password);
pbLogin = (ProgressBar) findViewById(R.id.loginProgressBar);
pbLogin.setVisibility(View.INVISIBLE);
btSignIn = (Button) findViewById(R.id.bt_login_signin);
cbLoginSaveCredentials = (CheckBox) findViewById(R.id.cb_login_save_credential);
manager = AppManager.getManager(getApplication());
if (manager.fileExistance("rapid-sentry-identity.txt")) {
pbLogin.setVisibility(View.VISIBLE);
new GetIdentityFile().execute();
}
}
private boolean CheckInternetConnection() {
manager = AppManager.getManager(getApplication());
boolean isConnected = false;
isConnected = manager.isConnected();
return isConnected;
}
public void HandleSignIn(View view) {
pbLogin.setVisibility(View.VISIBLE);
manager = AppManager.getManager(getApplication());
if (!CheckInternetConnection()) {
Toast.makeText(manager.getApplication().getApplicationContext(), R.string.no_internet_connection, Toast.LENGTH_SHORT).show();
} else {
String user = tbLoginUser.getText().toString();
String password = tbLoginPassword.getText().toString();
new Login().execute(user, password);
}
}
private void SaveSharePreferences() {
manager.SaveSharedIdentityObject(myIdentiy);
}
private class Login extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String result = null;
try {
Identity identity = manager.VerifyLogin(params[0], params[1]);
myIdentiy = identity;
} catch (Exception ex) {
result = null;
}
return result;
}
@Override
protected void onPostExecute(String result) {
pbLogin.setVisibility(View.INVISIBLE);
if (cbLoginSaveCredentials.isChecked()) {
new SaveIdentityFile().execute(myIdentiy.getIdentityUser(), myIdentiy.getIdentityPassword());
}
if (myIdentiy == null) {
Animation shake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
tbLoginUser.startAnimation(shake);
tbLoginPassword.startAnimation(shake);
Toast.makeText(manager.getApplication().getApplicationContext(), R.string.login_error_login, Toast.LENGTH_SHORT).show();
} else {
SaveSharePreferences();
pbLogin.setVisibility(View.INVISIBLE);
Intent intentGateway = new Intent(getApplication(), GatewayActivity.class);
startActivity(intentGateway);
}
}
}
private class GetIdentityFile extends AsyncTask<Void, Void, String[]> {
@Override
protected String[] doInBackground(Void... params) {
String[] ident;
try {
ident = manager.RetrieveIdentityFromFile();
} catch (FileNotFoundException ex) {
ident = null;
}
return ident;
}
@Override
protected void onPostExecute(String[] aVoid) {
if (aVoid != null) {
new Login().execute(aVoid[0], aVoid[1]);
}
}
}
private class SaveIdentityFile extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
try {
manager.SaveIdentityFile(params[0], params[1]);
} catch (FileNotFoundException ex) {
return null;
}
return null;
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ((UpdateShareDataService.class).getName().equals(service.service.getClassName())) {
Intent intentService = new Intent(getApplicationContext(), UpdateShareDataService.class);
stopService(intentService);
return true;
}
}
return false;
}
}
答案 0 :(得分:0)
我有这个问题的答案,我在后台池数据中运行服务,但是在这个服务中我使用的是应用程序上下文,当应用程序关闭时,服务重启了好几次。解决方案是调用每个活动的onDestroy()方法,以确保当您关闭应用程序时,服务也将使用stopService销毁。谢谢大家!!
en