长时间最小化后Android开始 Activity

标签 android crash sleep

在用户转到其他应用程序后,我的应用程序没有关闭(一般情况下,它被最小化)但是在我打开应用程序一段时间后它崩溃了,因为 NullPointerException。那是因为 android 清理了一些内存并且一些变量不可访问任何更多。有什么方法可以确定 android 是否清除了我的应用程序的内存?或者什么是解决这个问题的好方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_payment);
    Bundle bundle = getIntent().getExtras();
    personId = bundle.getInt("personId");
    personName = bundle.getString("personName");

    lendStr = getResources().getString(R.string.lend);
    borrowStr = getResources().getString(R.string.borrow);


    setUpViews();
}


private void setUpViews() {
    SqlHelper db = new SqlHelper(this);
    Helper.centerActionBarTitle(this, db.getPerson(personId).getName(), false);
    tf = Typeface.createFromAsset(getAssets(), "FLEXO.TTF");
    tfFlexo = Typeface.createFromAsset(getAssets(), "FLEXO_BOLD.TTF");

    iv_add_row = (ImageView) findViewById(R.id.iv_add_row);
    iv_remove_row = (ImageView) findViewById(R.id.iv_remove_row);
    et_amount = (EditText) findViewById(R.id.et_amount);
    et_item_name = (EditText) findViewById(R.id.et_item_name);
    et_quantity = (EditText) findViewById(R.id.et_quantity);
    et_price = (EditText) findViewById(R.id.et_price);
    et_total_price = (TextView) findViewById(R.id.et_total_price);
    tv_currency = (TextView) findViewById(R.id.tv_currency);



    et_item_name.setText(getResources().getString(R.string.item));
    et_quantity.setText(getResources().getString(R.string.qty));
    et_price.setText(getResources().getString(R.string.price));
    et_total_price.setText(getResources().getString(R.string.total));

    tv_break = (TextView) findViewById(R.id.tv_break);
    tv_payment_date = (TextView) findViewById(R.id.tv_payment_date);
    tv_p_date = (TextView) findViewById(R.id.tv_p_date);
    tv_p_time = (TextView) findViewById(R.id.tv_p_time2);
    tv_maturity = (TextView) findViewById(R.id.tv_maturity);
    tv_m_time = (TextView) findViewById(R.id.tv_m_time);
    tv_m_date = (TextView) findViewById(R.id.tv_m_date);

    ll_list_wrapper = (LinearLayout) findViewById(R.id.ll_list_wrapper);

    tv_operation_type = (TextView) findViewById(R.id.tv_operation_type);
    chk_break_items = (ImageView) findViewById(R.id.chk_break_items);

    et_item_name.setTypeface(tfFlexo);
    et_quantity.setTypeface(tfFlexo);
    et_price.setTypeface(tfFlexo);
    et_total_price.setTypeface(tfFlexo);
    tv_operation_type.setTypeface(tf);
    tv_currency.setTypeface(tf);
    tv_break.setTypeface(tf);
    tv_payment_date.setTypeface(tf);
    tv_p_date.setTypeface(tf);
    tv_p_time.setTypeface(tf);
    tv_maturity.setTypeface(tf);
    tv_m_time.setTypeface(tf);
    tv_m_date.setTypeface(tf);

    int currencyIndex = MainActivity.settings.getCurrency();
    tv_currency.setText(Constants.currencies[currencyIndex]);
}

Logcat 输出:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lionshare.aldkan/com.lionshare.aldkan.AddPayment}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
    at android.app.ActivityThread.access$600(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:155)
    at android.app.ActivityThread.main(ActivityThread.java:5511)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at com.lionshare.aldkan.AddPayment.setUpViews(AddPayment.java:188)
    at com.lionshare.aldkan.AddPayment.onCreate(AddPayment.java:114)
    at android.app.Activity.performCreate(Activity.java:5066)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
    ... 11 more

实际上,在 com.lionshare.aldkan.AddPayment.setUpViews(AddPayment.java:188)

MainActivity.settings.getCurrency()

找不到MainActivity 的静态变量settings。我认为该变量已从内存中清除。

最佳答案

发生这种情况是因为 Android 系统破坏了您的 Activity 实例。请参阅 activity lifecycle documentation 中的保存和恢复 Activity 状态部分:

The system may also destroy the process containing your activity to recover memory if the activity is in the Stopped state and hasn't been used in a long time, or if the foreground activity requires more resources.

为了防止这种崩溃,你必须在这个方法中保存和恢复实例状态:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
}

关于长时间最小化后Android开始 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26155486/

相关文章:

iOS SIGSEGV SEGV_ACCERR 在layoutSubviews 中崩溃

c++ - 检查程序是否崩溃

c - Sleep() 或 sleep() 有多准确

Android淡入淡出动画问题

java - 打开由附加到电子邮件的 HSSFWorkbook 生成的 Excel 工作表时,Excel 崩溃

android - 共享首选项始终返回默认值

objective-c - 有没有办法监控 Mac 何时即将进入休眠状态?

Windows Server 2008 R2 (Windows 7) 上的命令提示符窗口优先级

java - 在 Android Studio 中的类上执行 Junit 测试,无需初始化移动设备/模拟器

java - 第一次安装LibGdx时出现错误