Search code examples
androidtabsandroid-fragmentactivitybaseadapterandroid-context

Custom Adapter with FragmentActivity - NullPointerException


I'm developing an application which has 2 tabs.

On both tabs there's a ListView and I'm using custom Adapter with both tabs .

I know, that to get the context for tabs, I should use getActivity().
It works well with the first tab, but it doesn't work with the second tab.

Here's the custom adapter code:

public class DownloadedAdapter extends BaseAdapter {

    public static ArrayList<Downloads_item> DownloadedList;
    Context context;
    Typeface face;
    LayoutInflater inflater;

    public DownloadedAdapter(Context cont, ArrayList<Downloads_item> ls) {
        cont = context;
        DownloadedList=ls;
        //face = Typeface.createFromAsset(cont.getAssets(), "font.ttf");
    }

    @Override
    public int getCount() {
        return DownloadedList.size();
    }

    @Override
    public Object getItem(int position) {
        return DownloadedList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View itemView = inflater.inflate(R.layout.download_item, parent, false);

            Downloads_item current_item = DownloadedList.get(position);

            TextView txt_download_item_name = (TextView) itemView
                    .findViewById(R.id.txt_downloads_item_name);
            TextView txt_download_item_date = (TextView) itemView
                    .findViewById(R.id.txt_downloads_item_date);

            txt_download_item_name.setText(current_item.get_sora()
                    .get_arabic_name());
            txt_download_item_date
                    .setText("\u062A\u0645 \u0627\u0644\u062A\u062D\u0645\u064A\u0644 \u0641\u064A: "
                            + current_item.get_date());

            txt_download_item_name.setTypeface(face);


        return itemView;
    }
}

And this is the second tab code:

public class DownloadedListTab extends SherlockFragment {

    ListView list_downloads_list;
    public static ArrayList<Downloads_item> array_list;
    public static ArrayList<Downloads_item> copy_array_list;
    Database database_obj;
    Context context;
    DownloadedAdapter adapter;

    TextView txt_search_name;
    EditText edit_search_value;
    TextView txt_no_results;
    LayoutInflater inflater;
    Typeface face;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.downloadedtab,container,false);
        prepare_array_list();
        list_downloads_list = (ListView)   view.findViewById(R.id.list_downloads_list);
        txt_no_results = (TextView) view.findViewById(R.id.txt_no_results);
        database_obj = new Database(getActivity());
        adapter = new DownloadedAdapter(getActivity(),copy_array_list);     
        list_downloads_list.setAdapter(adapter);

        if (copy_array_list.size() == 0) {

            txt_no_results
                    .setText("\u0644\u0645 \u064A\u062A\u0645 \u062A\u062D\u0645\u064A\u0644 \u0627\u064A \u0633\u0648\u0631\u0629 \u062D\u062A\u064A \u0627\u0644\u0623\u0646");
            txt_no_results.setVisibility(View.VISIBLE);
            txt_no_results.setTypeface(face);
            txt_no_results.setTextSize(30);
            txt_no_results.setTextColor(Color.parseColor("#878787"));
            list_downloads_list.setVisibility(View.INVISIBLE);
        } else {
            // adapter.notifyDataSetChanged();
            txt_no_results.setVisibility(View.INVISIBLE);
            list_downloads_list.setVisibility(View.VISIBLE);
        }
        return view;
    }

    private void prepare_array_list() {
        array_list = new ArrayList<Downloads_item>();
        copy_array_list = new ArrayList<Downloads_item>();
        TreeMap<String, String> temp = new TreeMap<String, String>();
        temp = database_obj.get_all_sower();

        for (String key : temp.keySet()) {
            array_list.add(new Downloads_item(Splash_1.Data_Tree.Tree_of_Soras
                    .get(key), temp.get(key)));
            copy_array_list.add(new Downloads_item(
                    Splash_1.Data_Tree.Tree_of_Soras.get(key), temp.get(key)));
        }
    }
}

I know that the problem lies here from the context getActivity. I tried getApplicationContext, getBaseContext, getApplication and nothing works.
Any idea how can I solve this issue?

database_obj = new Database(getActivity());
adapter = new DownloadedAdapter(getActivity(), copy_array_list);

LogCat

08-13 12:08:06.772: E/AndroidRuntime(1974): FATAL EXCEPTION: main
08-13 12:08:06.772: E/AndroidRuntime(1974): Process: com.idroid.elsidees, PID: 1974
08-13 12:08:06.772: E/AndroidRuntime(1974): java.lang.NullPointerException
08-13 12:08:06.772: E/AndroidRuntime(1974):     at com.idroid.elsidees.DownloadedListTab.prepare_array_list(DownloadedListTab.java:73)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at com.idroid.elsidees.DownloadedListTab.onCreateView(DownloadedListTab.java:44)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at android.os.Handler.handleCallback(Handler.java:733)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at android.os.Looper.loop(Looper.java:136)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at java.lang.reflect.Method.invoke(Method.java:515)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-13 12:08:06.772: E/AndroidRuntime(1974):     at dalvik.system.NativeStart.main(Native Method)

Solution

  • Try to invert the context assignament in the costructor

    public DownloadedAdapter(Context cont, ArrayList < Downloads_item > ls) {
        cont = context; //<---- no correct
        DownloadedList = ls;
        //face = Typeface.createFromAsset(cont.getAssets(), "font.ttf");
    }
    

    in

    public DownloadedAdapter(Context cont, ArrayList < Downloads_item > ls) {
        this.context = cont; //<--- this is correct.
        DownloadedList = ls;
        //face = Typeface.createFromAsset(cont.getAssets(), "font.ttf");
    }
    

    end

    private void prepare_array_list() {
            ...
        temp = database_obj.get_all_sower(); // <---- database_obj is NULL
        ...
    }
    

    initzialize database_obj before call prepare_array_list()

    database_obj = new Database(getActivity());
    prepare_array_list();