loader中的NullPointerException:onLoadFinished()游标为null Android

时间:2014-11-06 22:22:10

标签: android android-contentprovider loader android-cursorloader android-loadermanager

我正在构建一个应用程序,它通过自定义内容提供者使用游标加载器获取数据。数据通过我正在构建的其他应用程序插入。但是我在必须获取数据的应用程序中得到NullPointerException。异常发生在onLoadFinished()方法中,光标返回null。这真的很奇怪,因为我已经按照教程完成了相同的步骤。如果我使用教程的URI,它将检索插入的数据,并且游标不返回null。但如果我使用自己的URI,则游标返回null。有人可以帮帮我吗?

以下是检索数据MainActivity.class的应用程序代码:

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
    TextView resultView=null;       
    CursorLoader cursorLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        resultView= (TextView) findViewById(R.id.res);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void onClickDisplayNames(View view) {
        getSupportLoaderManager().initLoader(1, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        String[] column = {"name"};
        cursorLoader= new CursorLoader(this, Uri.parse("content://com.ah.wholesalers.MyProvider/brand"),column, null, null, null);
        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
        cursor.moveToFirst();
        StringBuilder res=new StringBuilder();
        while (!cursor.isAfterLast()) {
            res.append("\n"+ cursor.getString(1));
            cursor.moveToNext();
        }
        resultView.setText(res);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}

布局activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/btnRetrieve"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onClickDisplayNames"
        android:text="Display names" />
    <TextView
        android:id="@+id/res"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:clickable="false"
        android:ems="10" >
    </TextView>
</LinearLayout>

这是我的logcat,但有以下所有例外:

11-05 17:23:13.672: E/AndroidRuntime(2044): FATAL EXCEPTION: main
11-05 17:23:13.672: E/AndroidRuntime(2044): Process: com.example.contentprovideruser, PID: 2044
11-05 17:23:13.672: E/AndroidRuntime(2044): java.lang.NullPointerException
11-05 17:23:13.672: E/AndroidRuntime(2044): at com.example.contentprovideruser.MainActivity.onLoadFinished(MainActivity.java:46)
11-05 17:23:13.672: E/AndroidRuntime(2044): at com.example.contentprovideruser.MainActivity.onLoadFinished(MainActivity.java:1)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.Loader.deliverResult(Loader.java:103)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:81)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:221)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.os.Looper.loop(Looper.java:136)
11-05 17:23:13.672: E/AndroidRuntime(2044): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-05 17:23:13.672: E/AndroidRuntime(2044): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 17:23:13.672: E/AndroidRuntime(2044): at java.lang.reflect.Method.invoke(Method.java:515)
11-05 17:23:13.672: E/AndroidRuntime(2044): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-05 17:23:13.672: E/AndroidRuntime(2044): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-05 17:23:13.672: E/AndroidRuntime(2044): at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

当我忘记在AndroidManifest.xml中声明提供程序时,我得到了null游标。

类似的东西:

<provider android:name="com.ah.wholesalers.MyProvider"
  android:authorities="com.ah.wholesalers.MyProvider" 
  android:exported="false"/>

答案 1 :(得分:0)

如果您使用的是Android Studio,则需要了解清单文件的属性包名称

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="no.glennbech.foodfacts">

被build.gradle中的值覆盖

defaultConfig {
      applicationId "no.yourdomain.yourapp"
      ...
}

错误通常来自应用程序中的程序包名称(在build.gradle中定义)与提供程序&#34; android:authorities&#34;之间的不匹配。属性。

<provider
    android:name="com.yoiurdomain.packagename.ClassOfProfivder"
    android:authorities="no.yourdomain.yourapp" />