NullPointerException in loader: onLoadFinished() cursor is null Android -
i'm building app gets info cursorloader via custom contentprovider. info inserted through other app i'm building. i'm getting nullpointerexception in app has data. exception happening in onloadfinished() method cursor returning null. unusual because have followed tutorial , i've done exact same steps. if utilize uri of tutorial retrieves inserted info , cursor not returning null. if utilize own uri cursor returns null. can please help me out?
here code of app retrieve info 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 menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); homecoming 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); homecoming 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(); } }
layout 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>
here logcat exceptions:
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): @ com.example.contentprovideruser.mainactivity.onloadfinished(mainactivity.java:46) 11-05 17:23:13.672: e/androidruntime(2044): @ com.example.contentprovideruser.mainactivity.onloadfinished(mainactivity.java:1) 11-05 17:23:13.672: e/androidruntime(2044): @ android.support.v4.app.loadermanagerimpl$loaderinfo.callonloadfinished(loadermanager.java:427) 11-05 17:23:13.672: e/androidruntime(2044): @ android.support.v4.app.loadermanagerimpl$loaderinfo.onloadcomplete(loadermanager.java:395) 11-05 17:23:13.672: e/androidruntime(2044): @ android.support.v4.content.loader.deliverresult(loader.java:103) 11-05 17:23:13.672: e/androidruntime(2044): @ android.support.v4.content.cursorloader.deliverresult(cursorloader.java:81) 11-05 17:23:13.672: e/androidruntime(2044): @ android.support.v4.content.cursorloader.deliverresult(cursorloader.java:35) 11-05 17:23:13.672: e/androidruntime(2044): @ android.support.v4.content.asynctaskloader.dispatchonloadcomplete(asynctaskloader.java:221) 11-05 17:23:13.672: e/androidruntime(2044): @ android.support.v4.content.asynctaskloader$loadtask.onpostexecute(asynctaskloader.java:61) 11-05 17:23:13.672: e/androidruntime(2044): @ android.support.v4.content.modernasynctask.finish(modernasynctask.java:461) 11-05 17:23:13.672: e/androidruntime(2044): @ android.support.v4.content.modernasynctask.access$500(modernasynctask.java:47) 11-05 17:23:13.672: e/androidruntime(2044): @ android.support.v4.content.modernasynctask$internalhandler.handlemessage(modernasynctask.java:474) 11-05 17:23:13.672: e/androidruntime(2044): @ android.os.handler.dispatchmessage(handler.java:102) 11-05 17:23:13.672: e/androidruntime(2044): @ android.os.looper.loop(looper.java:136) 11-05 17:23:13.672: e/androidruntime(2044): @ android.app.activitythread.main(activitythread.java:5017) 11-05 17:23:13.672: e/androidruntime(2044): @ java.lang.reflect.method.invokenative(native method) 11-05 17:23:13.672: e/androidruntime(2044): @ java.lang.reflect.method.invoke(method.java:515) 11-05 17:23:13.672: e/androidruntime(2044): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 11-05 17:23:13.672: e/androidruntime(2044): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 11-05 17:23:13.672: e/androidruntime(2044): @ dalvik.system.nativestart.main(native method)
i getting null cursor when forgot declare provider in androidmanifest.xml.
something like:
<provider android:name="com.ah.wholesalers.myprovider" android:authorities="com.ah.wholesalers.myprovider" android:exported="false"/>
android android-contentprovider loader android-cursorloader android-loadermanager
No comments:
Post a Comment