我在以下方法中获得了NullPointerException
,并且我已经尝试了所有我无法想到的事情。
private void showCustomListView(int year, int month, int day) {
List<Transaction> mTransactionList = new ArrayList<Transaction>();
mTransactionList = AppManager.getTransactionsByDay(year, month, day);
DailyTransactionAdapter mAdapter = new DailyTransactionAdapter(getActivity(), 0, mTransactionList);
dailyTransactionListView.setAdapter(mAdapter);
}
以下是上下文:我有一个活动:Activity_A
托管Fragment_A
。从Fragment_A的onResume()
回调中调用上述方法,这是o nResume()
方法
@Override
public void onResume() {
super.onResume();
//When the Fragment resumes set the shown date to today
mYear = selectedDate.get(Calendar.YEAR);
mMonth = selectedDate.get(Calendar.MONTH);
mDay = selectedDate.get(Calendar.DAY_OF_MONTH);
tvDisplayDate.setText(new StringBuilder()
.append(mMonth + 1).append("-").append(mDay).append("-")
.append(mYear).append(" "));
showCustomListView(mYear, mMonth, mDay);
btnChangeDate.setOnClickListener(new View.OnClickListener() {
@Override
//After the view is created set the onclick lister for the
//Change date button
public void onClick(View view) {
showDatePicker();
}
});
}
此适配器的数据来自内存列表,如此。这是 AppManager
中的代码部分 public static List<Transaction> getTransactionsByDay(int year, int month, int day){
List<Transaction> dayTransactions = new ArrayList<Transaction>();
List<Transaction> allTransactions = getTransactionList();
Log.i(TAG, "Size: " + allTransactions.size());
for (Transaction tempTransaction : allTransactions){
if (tempTransaction.getTransactionDate().get(Calendar.YEAR) == year &&
tempTransaction.getTransactionDate().get(Calendar.MONTH) == month &&
tempTransaction.getTransactionDate().get(Calendar.DAY_OF_MONTH) == day)
{
dayTransactions.add(tempTransaction);
}
}
return dayTransactions;
}
当我启动应用程序时,这可以正常运行,就像在IDE(Android Studio)中运行应用程序一样。问题出现在我启动另一个Activity - Activity_B,在上面的列表运行并且可见之后,在Activity_B中我将一个项目添加到内存列表中,列表的大小从10到11开始。
当我现在从Activity_B转到Fragment_A时,应用程序在上面的showCustomListView()
方法中与NPE崩溃。我试图清除列表,我已经尝试了adapter.notifydatasetchanged()
但都没有成功,只有在我在应用程序运行时添加到列表后才会崩溃,只有在我重新启动应用程序后它才会正常运行。< / p>
我还应该尝试什么:这里有一些NPE
11-04 09:36:19.147 6464-6464/com.valuecardnfc.valuecardnfc E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.valuecardnfc.valuecardnfc, PID: 6464
java.lang.NullPointerException
at com.valuecardnfc.valuecardnfc.Models.AppManager.getTransactionsByDay(AppManager.java:80)
at com.valuecardnfc.valuecardnfc.Fragments.DailyTransactionFragment.showCustomListView(DailyTransactionFragment.java:160)
at com.valuecardnfc.valuecardnfc.Fragments.DailyTransactionFragment.onResume(DailyTransactionFragment.java:102)
at android.support.v4.app.Fragment.performResume(Fragment.java:1521)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:963)
at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:823)
at android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1128)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1477)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
答案 0 :(得分:2)
tempTransaction.getTransactionDate().get(Calendar.YEAR)
NullPointerException
有两种可能性:
tempTransaction
为null
。
getTransactionDate()
返回null
。
由于您在多个位置使用getTransactionDate()
的返回值,因此您应该创建一个新变量:
Calendar transactionDate = tempTransaction.getTransactionDate();
现在,您可以检查此transactionDate
是否为null
,并且可以在if
条件下重复使用它。您还会立即知道tempTransaction
是否为null
,因为此行会在这种情况下抛出NullPointerException
。