我的应用程序中出现以下崩溃:
11-12 14:27:10.180: E/AndroidRuntime(30384): FATAL EXCEPTION: main
11-12 14:27:10.180: E/AndroidRuntime(30384): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cookie.jar/com.cookie.jar.activities.SupportInformationActivity}: java.lang.NullPointerException
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.app.ActivityThread.access$700(ActivityThread.java:140)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.os.Looper.loop(Looper.java:137)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.app.ActivityThread.main(ActivityThread.java:4921)
11-12 14:27:10.180: E/AndroidRuntime(30384): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 14:27:10.180: E/AndroidRuntime(30384): at java.lang.reflect.Method.invoke(Method.java:511)
11-12 14:27:10.180: E/AndroidRuntime(30384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-12 14:27:10.180: E/AndroidRuntime(30384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-12 14:27:10.180: E/AndroidRuntime(30384): at dalvik.system.NativeStart.main(Native Method)
11-12 14:27:10.180: E/AndroidRuntime(30384): Caused by: java.lang.NullPointerException
11-12 14:27:10.180: E/AndroidRuntime(30384): at com.cookie.jar.utils.aUtils.getConnectionType(aUtils.java:160)
11-12 14:27:10.180: E/AndroidRuntime(30384): at com.cookie.jar.fragments.SupportInfoFragment.populateValues(SupportInfoFragment.java:95)
11-12 14:27:10.180: E/AndroidRuntime(30384): at com.cookie.jar.fragments.SupportInfoFragment.onCreateView(SupportInfoFragment.java:59)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
11-12 14:27:10.180: E/AndroidRuntime(30384): at com.cookie.jar.activities.AbsBaseaActivity.onStart(AbsBaseaActivity.java:127)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.app.Activity.performStart(Activity.java:5198)
11-12 14:27:10.180: E/AndroidRuntime(30384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2083)
11-12 14:27:10.180: E/AndroidRuntime(30384): ... 11 more
这是我的支持信息活动:
public class SupportInformationActivity extends AbsBaseSingleButtonActivity {
public static void newInstance(final Activity activity) {
final Intent intent = new Intent(activity, SupportInformationActivity.class);
activity.startActivity(intent);
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_support_info);
final Button infoButton = (Button) findViewById(R.id.button_done);
infoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
finish();
}
});
BangoHelper.eventSupport();
SupportInfoFragment.newInstance(getSupportFragmentManager());
}
@Override public void onPause()
{
super.onPause();
}
@Override public void onResume()
{
super.onResume();
}
@Override
protected int getTitleId() {
return R.string.title_support_info;
}
@Override
public void retryLoginDelayedData(){}
}
以下是我在平板电脑sw600dp布局下添加的逻辑:
<bool name="isDeviceSupported">true</bool>
<bool name="isTablet">true</bool>
我的AbsBaseaActivity代码: 公共抽象类AbsBaseaActivity扩展SherlockFragmentActivity实现NoticeDialogListener,LoginListener {
private static final long LOGOUT_PERIOD_IN_MS = 60 * 60 * 1000; // 1 hour
public final static String TAG_LOGIN_DIALOG = "LoginDialog";
public final static String TAG_LOGOUT_DIALOG = "LogoutDialog";
public static final String FAULT_CODE = "FaultCode";
public static final int RESULT_CLOSE_ALL = 5;
protected Fragment mActiveFragment;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
String action = intent.getAction();
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
Log.d("Login","Connectivity changed");
if (!NetworkUtils.isOnline()) {
Log.d("Login","Offline ...");
final String text = context.getResources().getString(R.string.offline_message);
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
SharedPreferencesManager.getInstance().saveIsOffline(true);
} else {
Log.d("Login","Not Offline");
if (shouldRetryLogin()) {
Log.d("Login","should retry login is true");
Fragment topFragment = FragmentStackManager.getInstance().getTopFragment();
/**
* Retry Login for all fragments except the Login Fragment
* topFragment is null if we are on the Login screen or the dashboard
*/
if (topFragment != null || (mActiveFragment!= null && DashboardFragment.getInstance().getClass().getName().equals(mActiveFragment.getClass().getName()))) {
Log.d("Login","retrying Login");
retryLogin();
if (isHttpCookieRemoved()) {
Log.d("Login","restoreCookies");
restoreCookies();
}
}
SharedPreferencesManager.getInstance().saveIsOffline(false);
}
}
} else if (BroadcastActions.MULTIPLE_ACTIVE_SESSIONS.equals(action) ) {
Log.i("ESO1","AbsaActivity: " + AbsBaseaActivity.this.getClass().getName());
final int faultCode = intent.getIntExtra(AbsBaseaActivity.FAULT_CODE, -1);
showLoginNoticeDialog(faultCode);
} else if (BroadcastActions.UPDATE_USER_PREFERENCES.equals(action)) {
final Bundle bundle = intent.getExtras();
String colorUp = bundle.getString(GenericColumns.COLOR_UP);
String colorDown = bundle.getString(GenericColumns.COLOR_DOWN);
String colorFlashing = bundle.getString(GenericColumns.COLOR_FLASHING);
checkUserPreferencesForUpdates(colorUp, colorDown, colorFlashing);
}
}
private boolean shouldRetryLogin() {
return SharedPreferencesManager.getInstance().getIsOffline();
}
};
private void checkUserPreferencesForUpdates(final String colorUp, final String colorDown, final String colorFlashing) {
String oldColorUp = SharedPreferencesManager.getInstance().getPreferences(MovementSettings.COLOR_UP);
String oldColorDown = SharedPreferencesManager.getInstance().getPreferences(MovementSettings.COLOR_DOWN);
String oldColorFlashing = SharedPreferencesManager.getInstance().getPreferences(MovementSettings.COLOR_FLASHING);
if (!colorUp.equalsIgnoreCase(oldColorUp) || !colorDown.equalsIgnoreCase(oldColorDown) || !colorFlashing.equalsIgnoreCase(oldColorFlashing)) {
SharedPreferencesManager.getInstance().savePreferences(MovementSettings.COLOR_UP, colorUp);
SharedPreferencesManager.getInstance().savePreferences(MovementSettings.COLOR_DOWN, colorDown);
SharedPreferencesManager.getInstance().savePreferences(MovementSettings.COLOR_FLASHING, colorFlashing);
refreshScreen();
}
}
public void refreshScreen() {
}
public abstract void manageActionBar();
@Override
protected void onCreate(final Bundle savedInstanceState) {
manageActionBar();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_a);
FragmentUtils.setActivityOrientation(this);
}
@Override
protected void onStart() {
super.onStart();
if (hasTimedOut()) {
SharedPreferencesManager.getInstance().setBackgroundTimestamp(0);
if (SharedPreferencesManager.getInstance().shouldAutoSingIn()) {
if (!LoginActivity.class.getName().equals(this.getClass().getName())) {
retryLogin();
}
} else {
goToLoginScreen();
}
} else if (isHttpCookieRemoved()) {
restoreCookies();
}
}
public void restoreCookies() {
CookieStore cookieStore = SharedPreferencesManager.getInstance().getCookieStore();
if (cookieStore != null) {
((NetworkRequestLauncher) NetworkRequestLauncher.getInstance()).setCookieStore(cookieStore);
}
}
public boolean isHttpCookieRemoved() {
CookieStore cookieStore = ((NetworkRequestLauncher) NetworkRequestLauncher.getInstance()).getCookieStore();
if (cookieStore != null) {
List<Cookie> cookies = cookieStore.getCookies();
if (cookies == null || cookies.size() == 0) {
return true;
}
}
return false;
}
private boolean hasTimedOut() {
long backgrountTimestamp = SharedPreferencesManager.getInstance().getBackgroundTimestamp();
if (backgrountTimestamp > 0) {
long currentTime = System.currentTimeMillis();
long backgroundDuration = currentTime - backgrountTimestamp;
return backgroundDuration > LOGOUT_PERIOD_IN_MS;
}
return false;
}
@Override
protected void onResume() {
final IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(BroadcastActions.MULTIPLE_ACTIVE_SESSIONS);
filter.addAction(BroadcastActions.UPDATE_USER_PREFERENCES);
registerReceiver(mReceiver, filter);
LoginController.getInstance().addLoginListener(this);
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(mReceiver);
LoginController.getInstance().clearAuthenticateListeners();
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
SharedPreferencesManager.getInstance().setBackgroundTimestamp(System.currentTimeMillis());
}
public void showLoginNoticeDialog(int faultCode) {
showLoginNoticeDialog(faultCode, false);
}
public void showLoginNoticeDialog(int faultCode, boolean isLogin) {
//We are assuming that a dialog instance is null when not showing and vice versa This
//is true currently because the only time we are generating a dialog is when we are
//trying to show it.
if (LoginCancelSigninDialogFragment.isShowing() || LoginOkDialogFragment.isShowing()) {
return;
}
//DialogFragment.show() is causing illegalstate exception of not allow to do this after onsaveinstancestate
DialogFragment dialogFragment = LoginDialogFactory.generateLoginDialog(faultCode, isLogin);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(dialogFragment, TAG_LOGIN_DIALOG);
ft.commitAllowingStateLoss();
}
@Override
public void goToLoginScreen() {
setResult(RESULT_CLOSE_ALL);
LoginActivity.newInstance(this);
FragmentStackManager.getInstance().clearBackStack(getSupportFragmentManager());
finish();
}
@Override
public void retryLogin() {
final String username = SharedPreferencesManager.getInstance().getLoginUsername();
final String password = SharedPreferencesManager.getInstance().getDecryptedLoginPassword();
LoginController.getInstance().login(username, password, true);
}
public AbsBaseaActivity() {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case RESULT_CLOSE_ALL:
finish();
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onLoginSucceeded() {
//do nothing
}
@Override
public void onLoginFailed(int faultCode) {
goToLoginScreen();
}
}
我有不同的活动和基本活动支持大多数片段,包括这一个。但是,这个活动在手机中正常工作但在平板电脑上崩溃(不确定是否由于添加了逻辑),崩溃日志中的任何想法?
答案 0 :(得分:1)
检查移动连接是否正在检查空值,如下所示:(解决了我的问题)
State mobile = ((conMan.getNetworkInfo(0) == null) ? null : connect to stuff
希望这有帮助