4

lang.NullPointerException android.widget.ArrayAdapter.init(ArrayAdapter.java) 只是将 Arraylist 值添加到 customAdapter 类中并将该值设置到 listview 中,请参见下面的代码帮助我

谢谢

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;
    }

}

当我运行此代码时,在 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
4

1 回答 1

8

此问题是由于 getActivity() 在您创建适配器时返回 null 的事实引起的:

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

如果 Activity 尚未附加到片段或已分离,则可能会发生这种情况。要解决它,您可以确保仅在附加活动的有效状态下调用fetchCallLogsDetails方法,或者如果getActivity() == nullgetActivity.isFinishing()则可以忽略该调用。

于 2013-10-31T11:30:20.870 回答