在 ListFragment 上遇到此问题,我认为这是适配器的错误,但无法弄清楚为什么会发生这种情况:
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
可能包含错误的代码:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getActivity().getIntent();
ArrayList<String> history = intent.getStringArrayListExtra("HistoryList");
ListView historyList = getListView();
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, history);
historyList.setAdapter(listAdapter);
}
启动历史一的 fragment :
public class PrimaryFragment extends Fragment {
@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.primary_layout, null);
}
ArrayList<String> history = new ArrayList<String>();
//START//
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NumberFormat nf = new DecimalFormat("##.###");
SharedPreferences mDefaultPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
Set<String> set = mDefaultPreferences.getStringSet("key", null);
if(set != null){
history = new ArrayList<String>(set);
}
TextView money = (TextView) getActivity().findViewById(R.id.textCool);
String storedValue = mDefaultPreferences.getString("last known value", String.valueOf(0));
money.setText(storedValue);
}
+
public void history(View view){
Intent intent = new Intent(getActivity(), History.class);
intent.putStringArrayListExtra("HistoryList", history);
startActivity(intent);
}
logcat 最相关的部分:
java.lang.NullPointerException:尝试在空对象引用上调用接口(interface)方法“int java.util.List.size()”
10-23 21:01:32.268 12036-12036/com.android believe.drawerwithswipetabs E/AndroidRuntime: 在 android.widget.ArrayAdapter.getCount(ArrayAdapter.java:337)
10-23 21:01:32.268 12036-12036/com.android believe.drawerwithswipetabs E/AndroidRuntime:在 android.widget.ListView.setAdapter(ListView.java:491)
10-23 21:01:32.268 12036-12036/com.android believe.drawerwithswipetabs E/AndroidRuntime: 在 com.android believe.drawerwithswipetabs.History.onActivityCreated(History.java:26)
编辑:
我想我开始理解错误是什么:
我正在使用 ArrayList
填充 ListFragment
,该数组在另一个 Fragment
(主要的)中获取其值,但是这个 code
首次在基于 Activities
的应用程序上使用,使用 Button
将这些值传递给其他 Activity
。
但现在这些都是 fragment
并且位于同一个ViewPager
中,那么我就没有Buttons
来启动History
> fragment ,我只需滑动。所以我的新问题是:
如何将 ArrayString
值传递给 History ListFragment
知道我无法通过 Button
启动它,但我通过滑动来实现?
我尝试研究LifeCycle
,但一无所获,请告诉我您会发现什么!
最佳答案
使用 putStringArrayListExtra 时需要包前缀
public Intent putStringArrayListExtra (String name, ArrayList value)
Added in API level 1 Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".
Parameters name The name of the extra data, with package prefix. value The ArrayList data value.
您需要将包名称添加到用于额外存储数组列表的字符串中。
public void history(View view){
Intent intent = new Intent(getActivity(), History.class);
intent.putStringArrayListExtra("com.androidbelieve.drawerwithswipetabs.historylist", history);
startActivity(intent);
}
关于java.lang.NullPointerException : (ListFragment),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33311426/