首页 文章

super(context,textviewid);显示java.lang.NullPointerException

提问于
浏览 1948
0

试图弄清楚如何传递超级构造函数来阻止这个 NPE ,任何人都可以帮忙吗?

MainActivity.java

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

DownloadFragment.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);
        // ...
    }
}

Exception :

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()方法中的下一行为null .

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

DownloadFragment.java

Cursor a = mDbHelper.getEachDownload(2);

3 回答

  • 0

    我相信这是因为你传递了一个空的上下文 .

    尝试:

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

    DownloadFragment.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(); 
        } 
    }
    
  • 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);
        }
    }
    
  • 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;
        }
    
    }
    

相关问题