gpt4 book ai didi

java.lang.NullPointerException : (ListFragment) 异常

转载 作者:行者123 更新时间:2023-11-30 08:45:24 26 4
gpt4 key购买 nike

在 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.androidbelieve.drawerwithswipetabs E/AndroidRuntime:在 android.widget.ArrayAdapter.getCount(ArrayAdapter.java:337)
10-23 21:01:32.268 12036-12036/com.androidbelieve.drawerwithswipetabs E/AndroidRuntime:在 android.widget.ListView.setAdapter(ListView.java:491)
10-23 21:01:32.268 12036-12036/com.androidbelieve.drawerwithswipetabs E/AndroidRuntime:在 com.androidbelieve.drawerwithswipetabs.History.onActivityCreated(History.java:26)

编辑:

我想我开始明白错误是什么了:

我正在用一个 ArrayList 填充一个 ListFragment,它在另一个 Fragment 中获得它的值,主要的,但是这个 code 最初用于基于 Activities 的 App,使用 Button 将这些值传递给其他 Activity。但是现在它们都是 fragments 并且在同一个 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.

putStringArrayListExtra docs

您需要将包名称添加到用于存储数组列表的字符串中。

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com