在我的Android应用程序被销毁后,为什么我的List数据为null?

时间:2014-07-25 01:43:16

标签: java android nullpointerexception

我的应用程序被销毁后,当我单击列表视图中的某个项目时,它应该将数据复制到剪贴板而不是数据返回null,尽管我确实计算了列表中的一些项目。

以下是一些代码:

@Override
protected void onSaveInstanceState(final Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putStringArrayList("list", listFragment.getLocations());
}

public void populateList(final StorageManager topItem,
                         final StorageManager bottomItem) {
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            locations = topItem.readFile();
            times = bottomItem.readFile();
            for (int index = 0; index < locations.size() && index < times.size(); index++) {
                Map<String, UserLocation> data = new HashMap<String, UserLocation>();
                data.put("Top", new UserLocation(index, locations.get(index)));
                data.put("Bottom", new UserLocation(index, times.get(index)));
                list.add(data);
            }
            Collections.reverse(list);
            Collections.reverse(locations);
        }
    });
    thread.run();
}

    @Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String dataToSend = TwoItemListFragment.locations.get(position);

    if (null != mListener) {
        mListener.onFragmentInteraction(TwoItemListFragment.list.get(position).hashCode());
    }
    try {
        copyData(dataToSend);
    } catch (NullPointerException e) {
        Log.d(TAG, dataToSend + "");
    }
}

     */
public void copyData(String data) {
    Context context = getActivity().getApplicationContext();
    String copied = getString(R.string.copied);
    Toast copyToast = Toast.makeText(context, copied, Toast.LENGTH_SHORT);

    try {
        cl.setPrimaryClip(ClipData.newPlainText("copy", data));
        copyToast.show();
    } catch (NullPointerException e) {
        Log.e(TAG, "Could not copy data: NullPointerException");
        Log.d(TAG, String.valueOf(TwoItemListFragment.locations.size()));
        Log.d(TAG, String.valueOf(TwoItemListFragment.locations.get(1)));
        copyToast = Toast.makeText(context, getString(R.string.null_text), Toast.LENGTH_LONG);
        copyToast.show();
    }
}

一些Logcat数据

07-25 02:30:42.172  13968-13968/com.DXC0.locationmanagertests.app E/com.DXC0.locationmanagertests.app.TwoItemListFragment﹕ Could not copy data: NullPointerException
07-25 02:30:42.172  13968-13968/com.DXC0.locationmanagertests.app D/com.DXC0.locationmanagertests.app.TwoItemListFragment﹕ 5

修改 刚检查过,我发送的数据不是空的,它确实存在;但由于某种原因,它仍然返回null。问题似乎是这一行

            cl.setPrimaryClip(ClipData.newPlainText("copy", data));

1 个答案:

答案 0 :(得分:0)

由于某些原因,ClipboardManager“cl”为空,所以在我的try块中,我重新初始化并且它有效。数据永远不会为空。

    try {
        cl = ((MainActivity) getActivity()).clipboardManager();
        cl.setPrimaryClip(ClipData.newPlainText("copy", data));
        copyToast.show();
    } catch (NullPointerException e) {
        e.printStackTrace();
        Log.e(TAG, "Could not copy data: NullPointerException");
        copyToast = Toast.makeText(context, getString(R.string.null_text), Toast.LENGTH_LONG);
        copyToast.show();
    }