试图弄清楚如何通过超级构造函数来阻止它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);