Вопрос

I am trying to use commonsware's LoaderEx in my project. So I tested LoaderExDemo project. It works fine. But when I change the device from portrait mode to landscape mode the loader variable is going to be null.

In onCreate method we initialize the loader like following

getLoaderManager().initLoader(0, null, this);

then why loader is not initializing when configuration change ? Then how to fix this ?

Logcat report

11-06 19:08:39.515: E/AndroidRuntime(1666): FATAL EXCEPTION: main
11-06 19:08:39.515: E/AndroidRuntime(1666): java.lang.NullPointerException
11-06 19:08:39.515: E/AndroidRuntime(1666):     at com.commonsware.cwac.loaderex.demo.ConstantsBrowser.processAdd(ConstantsBrowser.java:176)
11-06 19:08:39.515: E/AndroidRuntime(1666):     at com.commonsware.cwac.loaderex.demo.ConstantsBrowser.access$1(ConstantsBrowser.java:170)
11-06 19:08:39.515: E/AndroidRuntime(1666):     at com.commonsware.cwac.loaderex.demo.ConstantsBrowser$1.onClick(ConstantsBrowser.java:134)
11-06 19:08:39.515: E/AndroidRuntime(1666):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
11-06 19:08:39.515: E/AndroidRuntime(1666):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 19:08:39.515: E/AndroidRuntime(1666):     at android.os.Looper.loop(Looper.java:137)
11-06 19:08:39.515: E/AndroidRuntime(1666):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-06 19:08:39.515: E/AndroidRuntime(1666):     at java.lang.reflect.Method.invokeNative(Native Method)
11-06 19:08:39.515: E/AndroidRuntime(1666):     at java.lang.reflect.Method.invoke(Method.java:511)
11-06 19:08:39.515: E/AndroidRuntime(1666):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-06 19:08:39.515: E/AndroidRuntime(1666):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-06 19:08:39.515: E/AndroidRuntime(1666):     at dalvik.system.NativeStart.main(Native Method)

Thanks

Это было полезно?

Решение

Ah, excellent point -- that's a bug in my demos.

Add the following line to onLoadFinished():

this.loader=(SQLiteCursorLoader)loader;

That will be called as part of the configuration change, to deliver our last-loaded Cursor, and we can hold onto the Loader there.

I will update my repository with this fix soonish.

Thanks!

Другие советы

When you change the orientation the view is recreated.

you can prevent this by adding

android:configChanges="orientation"

to your activity in the manifest and handle the orientation change by yourself by overriding the onConfigurationChanged trigger.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top