Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NullPointerException at android.widget.ArrayAdapter.init(ArrayAdapter.java)

lang.NullPointerException android.widget.ArrayAdapter.init(ArrayAdapter.java) just am adding Arraylist values into customAdapter class and setting that values into listview see below code helpm me

thanks

private void fetchCallLogsDetails(String selectedId) {
        this.SelectedLogId = selectedId;
        new FetchCallLogDetailsAsyncTask() {
            protected void onPostExecute(Boolean result) {

                if (mCallLogModel.getmPhoto() != null) {
                    mCallLogPhoto.setImageBitmap(mCallLogModel.getmPhoto());
                }

                mCallLogDetailName.setText(mCallLogModel.getmName());
                mCallLogDetailNumber.setText(mCallLogModel.getmNumber());

                mCallLogDetailName.setTextSize(12);
                mCallLogDetailNumber.setTextSize(10);

                mLogAuditUtilList = mCallLogModel.getmLogAuditUtilList();
                if (mLogAuditUtilList != null) {
                    mCallLogAuditArrayAdapter = new CallLogAuditArrayAdapter(
                            getActivity(), R.id.details_audit_log_list,
                            mLogAuditUtilList);

                    mAuditListView.setAdapter(mCallLogAuditArrayAdapter);
                }

            };
        }.execute("");
    }
    class FetchCallLogDetailsAsyncTask extends
            AsyncTask<String, Integer, Boolean> {
        @Override
        protected Boolean doInBackground(String... params) {
            // reading call logs from contentReslover
            mCallLogUtil = CallLogUtil.newInstance(mconContentResolver);
            mCallLogModel = mCallLogUtil.selectedLogDetails(SelectedLogId);
            return false;
        }

    }



customeAdapter class
-------------------------

public class CallLogAuditArrayAdapter extends ArrayAdapter<LogAuditUtil> {
    private LayoutInflater minflater;
    private ImageView mCallTypeImage;

    public CallLogAuditArrayAdapter(Context context, int textViewResourceId,
            List<LogAuditUtil> objects) {
        super(context, textViewResourceId, objects);

        minflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final LogAuditUtil mLogAuditUtil = this.getItem(position);

        convertView = minflater.inflate(R.layout.call_logs_details_list_view,
                null);
        TextView mCallType = (TextView) convertView
                .findViewById(R.id.call_logs_details_list_view_type_id);
        TextView mDetailsDate = (TextView) convertView
                .findViewById(R.id.call_logs_details_list_view_date_id);
        mCallTypeImage = (ImageView) convertView
                .findViewById(R.id.call_logs_details_list_view_type_image_id);

        TextView mCallDuration = (TextView) convertView
                .findViewById(R.id.call_logs_details_list_view_call_duration_id);

        mCallType.setTextSize(12);
        mDetailsDate.setTextSize(10);
        mCallDuration.setTextSize(8);

        switch (Integer.parseInt(mLogAuditUtil.getmAuditType())) {
        case 1:
            mCallType.setText(R.string.text_incoming_call);
            mCallTypeImage.setImageResource(R.drawable.incoming_call);
            break;

        case 2:
            mCallTypeImage.setImageResource(R.drawable.outgoing_call);
            mCallType.setText(R.string.text_outgoing_call);
            break;

        case 3:
            mCallTypeImage.setImageResource(R.drawable.missed_call);
            mCallType.setText(R.string.text_missed_call);
            break;
        }

        mDetailsDate.setText(mLogAuditUtil.getmAuditDate());
        mCallDuration.setText(mLogAuditUtil.getmAuditDuration());
        return convertView;
    }

}

when am runing this code am getting blow errors in catlog

E/AndroidRuntime(3047): FATAL EXCEPTION: main
06-07 19:58:56.257: E/AndroidRuntime(3047): java.lang.NullPointerException
06-07 19:58:56.257: E/AndroidRuntime(3047):     at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at com.movirtu.contactsUtil.CallLogsArrayAdapter.<init>(CallLogsArrayAdapter.java:30)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at com.movirtu.fragments.CallLogsFragment$2.onPostExecute(CallLogsFragment.java:88)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at com.movirtu.fragments.CallLogsFragment$2.onPostExecute(CallLogsFragment.java:1)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at android.os.AsyncTask.finish(AsyncTask.java:602)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at android.os.Looper.loop(Looper.java:137)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at android.app.ActivityThread.main(ActivityThread.java:4340)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at java.lang.reflect.Method.invokeNative(Native Method)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at java.lang.reflect.Method.invoke(Method.java:511)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-07 19:58:56.257: E/AndroidRuntime(3047):     at dalvik.system.NativeStart.main(Native Method)
06-07 19:58:58.917: I/Process(3047): Sending signal. PID: 3047 SIG: 9
like image 963
venu Avatar asked Feb 16 '23 07:02

venu


1 Answers

This issue is caused by the fact that the getActivity() is returning null on the time you're creating the Adapter:

 new CallLogAuditArrayAdapter(getActivity(), R.id.details_audit_log_list, mLogAuditUtilList);

This can happen if the Activity is not yet attached to the fragment, or it has been detached. To solve it you could either ensure that the method fetchCallLogsDetails is only called under a valid state of attached activity OR you could ignore the call if getActivity() == null or getActivity.isFinishing().

like image 119
Alécio Carvalho Avatar answered Apr 09 '23 19:04

Alécio Carvalho