使用异步任务加载联系人时抛出空指针异常

时间:2015-03-19 20:22:55

标签: android android-asynctask

当我尝试使用异步任务加载联系人时,它会抛出异常,但是当我将代码移动到onCrate方法时,它不会抛出任何错误。

这是我的logcat输出

FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1457)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:723)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:583)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1457)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:723)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:583)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1457)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:723)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:583)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4928)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
        at android.view.View.measure(View.java:15356)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1858)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1110)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1283)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1008)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4222)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:525)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4793)
        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:808)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
        at dalvik.system.NativeStart.main(Native Method)

和异步任务

new AsyncTask<Void, Void, Void>() {


        @Override
        protected Void doInBackground(Void... params) {
            Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME);

            if (c.getCount() > 0) {
                while (c.moveToNext()) {
                    x.add(c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
                }
            }
            c.close();

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if (mContactsRecyclerView != null) {
                mAdapter = new ContactsListAdapter(MainActivity.this, x);
                mContactsRecyclerView.setAdapter(mAdapter);
                mContactsRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
            } else {
                Toast.makeText(MainActivity.this, "Recycler View is null", Toast.LENGTH_SHORT).show();
            }
            Toast.makeText(MainActivity.this,"Contacts Loading Finished",Toast.LENGTH_SHORT).show();

            Log.e("Sony", "finished loading");
        }
    }.execute();

任何人都可以告诉我们这段代码中有什么问题。

2 个答案:

答案 0 :(得分:2)

将以下行移至OnCreate()方法

 mContactsRecyclerView.setLayoutManager(newLinearLayoutManager(MainActivity.this));

您尚未在LinearLayoutManager

中将RecyclerView设为Oncreate()

类似的东西:

LinearLayoutManager layoutManager = new LinearLayoutManager(context);
mContactsRecyclerView.setLayoutManager(layoutManager);
//mContactsRecyclerView.setAdapter(new CustomAdapter());

答案 1 :(得分:1)

此错误不是因为AsyncTask而发生的。仔细看看你的堆栈跟踪,你会明白,该应用程序在启动时崩溃了。看一下最后一行,它指向android.support.v7.widget.RecyclerView.onMeasureAOSP是一个开源项目,因此您可以查看源代码here。如果您对其进行分析,您会理解该应用崩溃,因为mLayoutnullRecyclerView在显示数据之前应该有布局管理器(在为任何适配器提供数据之前),并且设置它的最佳方法是使用onCreate方法初始化所有视图。

onCreate

mContactsRecyclerView = (RecyclerView) findViewById(R.id.recView);
mContactsRecyclerView.setLayoutManager(new LinearLayoutManager(this));

布局管理器应设置一次,因此您可以从onPostExecute中删除冗余布局管理器设置器。