0

试图弄清楚如何通过超级构造函数来阻止它NPE,有人可以帮忙吗?

MainActivity.java

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        ((DownloadFragment) downloads).refreshlist();
    }
}

下载Fragment.java

public class MyAdapter extends ArrayAdapter<AdapterItem> {
    public MyAdapter(Context context, int textviewid) {
        super(context, textviewid); // NPE here
    }

    public void refreshlist() {
        items.clear();
        adapter = new MyAdapter(getActivity(), 0);
        // ...
    }
}

例外 :

java.lang.NullPointerException
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:104)
at com.DownloadFragment$MyAdapter.<init>(DownloadFragment.java:142)
at com.DownloadFragment.refreshlist(DownloadFragment.java:479)
at com.MainActivity.onWindowFocusChanged(MainActivity.java:387)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2466)
at android.view.View.dispatchWindowFocusChanged(View.java:7456)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:935)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3264)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

现在 refreshList() 方法中的下一行在执行此操作后为空。

@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); 适配器 = new MyAdapter(getActivity(), 0); }

下载Fragment.java

Cursor a = mDbHelper.getEachDownload(2);
4

3 回答 3

0

我相信这是因为你传递给它一个空上下文。

尝试:

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        ((DownloadFragment) downloads).refreshlist(this);
    }
}

下载Fragment.java

public class MyAdapter extends ArrayAdapter<AdapterItem> {
    public MyAdapter(Context context, int textviewid) {
        super(context, textviewid); // NPE here
    }

    public void refreshlist(Context context) {
        items.clear();
        adapter = new MyAdapter(context, 0);
        // ...
    }
}

您是否尝试使用新数据刷新适配器?您是否尝试过 notifyDataSetChanged()?

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus); 
    if (hasFocus) { 
       DownloadFragment.adapter.notifyDataSetChanged(); 
    } 
}
于 2013-10-31T05:36:51.933 回答
0

Fragment 类中的getActivity() 方法返回null,直到它附加到Activity,即直到调用onAttach(Activity) 方法。所以最好在 Fragment 的 onActivityCreated() 方法中为 MyAdapter 类创建对象。

试试这个...

public class DownloadFragment extends Fragment {
    private MyAdapter adapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new MyAdapter(getActivity(),0);
    }
}
于 2013-10-31T05:37:24.003 回答
0

你的适配器应该是这样的

package com.example.demo;

import java.util.ArrayList;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class SmartListAdapterWithHolder extends ArrayAdapter<AdapterItem> {

    private Context context;
    private ArrayList<String> items;

    public SmartListAdapterWithHolder(Context context, int resource, ArrayList<AdapterItem> items) {
        super(context, resource, items);
        this.items = items;
        this.context = context;
    }

    public ArrayList<AdapterItem> getSmartListItems() {
        return items;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        Log.v("SmartListAdapter", observer.toString());
        super.registerDataSetObserver(observer);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        return convertView;
    }

}
于 2013-10-31T05:46:47.683 回答