Android启动活动经过长时间的最小化

时间:2014-10-02 06:31:23

标签: 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:无法启动活动   ComponentInfo {com.lionshare.aldkan / com.lionshare.aldkan.AddPayment}:   java.lang.NullPointerException
at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
  在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
  在   android.app.ActivityThread.access $ 600(ActivityThread.java:151)
  在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1335)
  在   android.os.Handler.dispatchMessage(Handler.java:99)
  在   android.os.Looper.loop(Looper.java:155)
  在   android.app.ActivityThread.main(ActivityThread.java:5511)
  在   java.lang.reflect.Method.invokeNative(Native Method)
  在   java.lang.reflect.Method.invoke(Method.java:511)
  在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1029)
  在   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
  在dalvik.system.NativeStart.main(Native   方法)
由以下原因引起:java.lang.NullPointerException
  在   com.lionshare.aldkan.AddPayment.setUpViews(AddPayment.java:188)
  在   com.lionshare.aldkan.AddPayment.onCreate(AddPayment.java:114)
  在   android.app.Activity.performCreate(Activity.java:5066)
  在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
  在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
  ... 11更多

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

MainActivity.settings.getCurrency()

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

1 个答案:

答案 0 :(得分:2)

这是因为Android系统会破坏您的活动实例。请参阅activity lifecycle documentation中的保存和恢复活动状态部分:

  

系统也可能会破坏包含您的活动的进程   如果活动处于已停止状态且尚未恢复,则恢复内存   长时间使用,或前景活动需要更多   资源。

要防止此崩溃,您必须在此方法中保存并恢复实例状态:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
}