2014-10-02 28 views
1

用户转到其他应用程序后,我的应用程序没有关闭(一般情况下,它被最小化),但一段时间后,当我打开应用程序时它崩溃,因为NullPointerException。这是因为android清理一些内存和一些变量不再可用。有没有什么方法可以确定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
       在 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在 android.os.Handler.dispatchMessage(Handler.java:99)(ActivityThread.java:1335)
       
       在 android.os.Looper.loop(Looper.java:155)
       在 android.app.ActivityThread.main(ActivityThread.java:5511)
       在 java.lang.reflect.Method.invokeNative(本机方法)
       在 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(母语 方法)
产生的原因:在显示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多个

Actyually,在com.lionshare.aldkan.AddPayment.setUpViews(AddPayment.java:188)

MainActivity.settings.getCurrency() 

它不能找到静态变量的MainActivitysettings。我想这个变量已经从内存中清除了。

+0

发表您的培训相关的代码 – 2014-10-02 06:33:20

+0

也发表您的logcat显示异常。 – CodeMonkey 2014-10-02 06:36:22

+0

你应该发布你相关的代码和logcat。无论如何,也请阅读:http://developer.android.com/training/basics/activity-lifecycle/recreating.html – fasteque 2014-10-02 06:36:39

回答

2

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

该系统还可以消灭包含您的活动过程中 如果活动处于停止状态,一直没有在一个长期使用 恢复记忆时间,或者如果前台活动需要更多的 资源。

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

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
} 

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) { 
}