用戶轉到其他應用程序後,我的應用程序沒有關閉(一般情況下,它被最小化),但一段時間後,當我打開應用程序時它崩潰,因爲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()
它不能找到靜態變量的MainActivity
settings
。我想這個變量已經從內存中清除了。
發表您的培訓相關的代碼 – 2014-10-02 06:33:20
也發表您的logcat顯示異常。 – CodeMonkey 2014-10-02 06:36:22
你應該發佈你相關的代碼和logcat。無論如何,也請閱讀:http://developer.android.com/training/basics/activity-lifecycle/recreating.html – fasteque 2014-10-02 06:36:39