4

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
2
  • 4
    is getActivity returning null? when you call this code the fragment is attached? Commented Jun 7, 2013 at 14:51
  • thanks but i tryed working fine but when am communicating fragments synchronuslly again same error is coming how to reslove this error Commented Jun 7, 2013 at 15:29

1 Answer 1

8

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().

Sign up to request clarification or add additional context in comments.

8 Comments

Sorry if i answer and re open the question now but i have the same problem and i didn't understand how exactly write. so if getActivity() == null i don't start the asynktask?
you can do it on your onPostExecute()...for instance: stop the execution of the onPostExecute() if getActivity == null OR getActivity.isFinishing() == true... this is because the result of the onPostExecute() comes asynchronously and by the time it comes it might be possible that the activity was detached OR destroyed (either because the user rotated the device OR pressed back key).
This is my adapter if you can help me stackoverflow.com/questions/24574733/… thank you.
basically you have to put the code of the onPostExecute() inside a if(getActivity() != null)...to prevent cases like rotating screen..BUT where do you start your asynctask? where do you call the logo.execute()? You have to respect the lifecycle of the fragment..for instance, you should call it on the onActivityCreated OR onResume()..not on the Fragment.onCreate() because at this point the activity is not attached yet..therefore getActivity() will always be NULL.
mmh, i call it in the onCreate()... so do you think that if i just call the asynktask in onResume() could be a solution? i didn't notice that.. Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.