【问题标题】:java.lang.NullPointerException: (ListFragment)java.lang.NullPointerException: (ListFragment)
【发布时间】:2016-01-23 12:38:00
【问题描述】:

在 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);
}

启动历史一的片段:

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: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 10-23 21:01:32.268 12036-12036/com.androidbelieve.drawerwithswipetabs E/AndroidRuntime: at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:337) 10-23 21:01:32.268 12036-12036/com.androidbelieve.drawerwithswipetabs E/AndroidRuntime: at android.widget.ListView.setAdapter(ListView.java:491) 10-23 21:01:32.268 12036-12036/com.androidbelieve.drawerwithswipetabs E/AndroidRuntime: at com.androidbelieve.drawerwithswipetabs.History.onActivityCreated(History.java:26)

编辑:

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

我正在用一个ArrayList 填充一个ListFragment,它在另一个Fragment(主要)中获得了它的值,但是这个code 第一次用于基于Activities 的应用程序,使用了@ 987654331@ 将这些值传递给另一个 Activity。 但是现在这些都是fragments 并且在同一个ViewPager 中,那么我没有Buttons 来启动History 片段,我只需要滑动。所以我的新问题是:

我如何将 ArrayString 值传递给 History ListFragment,因为我无法通过 Button 启动它,但我可以通过 Swipe 来启动它? 我已经尝试研究LifeCycle,但想不出任何东西,让我知道你会想出什么!

【问题讨论】:

  • 如果没有完整的堆栈跟踪,我们将很难为您提供帮助。但是,您应该确认history 不是null,因为这可以解释您的症状。
  • 您确定没有使用setArguments 传递List
  • 做了一些编辑@CommonsWare
  • ArrayList&lt;String&gt; history 为空
  • 为什么是@Blackbelt?

标签: java android listview android-intent android-listfragment


【解决方案1】:

使用 putStringArrayListExtra 时需要包前缀

public Intent putStringArrayListExtra(字符串名称,ArrayList 值)

在 API 级别 1 中添加 将扩展数据添加到意图。名字必须 包括一个包前缀,例如应用程序 com.android.contacts 会使用“com.android.contacts.ShowAll”之类的名称。

参数名称 额外数据的名称,带有包前缀。 value ArrayList 数据值。

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: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference@CoryRoy
  • 问题已更新@CoryRoy
【解决方案2】:

我建议将 Bundle 与您的 Intent 一起使用,并通过 Bundle.putParcelableArrayList() 将 ArrayList 粘贴到 Bundle 中。

Intent intent = new Intent(getActivity(), History.class);
Bundle extras = new Bundle();
extras.putParcelableArrayList("HistoryList", history);
// intent.putExtras(extras);
startActivity(intent, extras); // can put as above, or do this instead

+

Intent intent = getActivity().getIntent();
Bundle extras = intent.getExtras();
ArrayList<String> history = extras.getParcelableArrayList("HistoryList");

由于您使用的是 Fragment,因此您应该可以这样做。或者,如果您不想在 Activity 中创建 Bundle 对象,您可以通过 within the Fragment 进行操作。

Bundle extras = new Bundle();
extras.putParcelableArrayList("HistoryList", history);
MyFragment frag = MyFragment.getInstance();
frag.setArguments(extras);
/* Fragment Transition code */

+

public class MyFragment {
    public MyFragment() { ... } 

    public MyFragment getInstance( ) { ... } 

    public void onCreate( ... ) {
        this.history = new ArrayList<String>();
    }

    public View onCreateView( ... ) {
        Bundle args = getArguments();
        if (args != null && args.containsKey("History")) {
            List<String> temp = getArguments().getParcelableArrayList("History");
            this.history.addAll(temp);
            getListAdapter().notifyDataSetChanged();
        }
    }
}

【讨论】:

  • 它说我不能在 Fragment 中使用那个包
  • 问题已更新@cricket_007
  • 答案更新@InssideYou
  • 谢谢大佬试试,顺便说一下在我遇到一些错误之前的方法,这就是为什么我做不到
  • 好的,但是我应该在括号内放什么而不是... @cricket_007 ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-20
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多