liuling07 / PhotoPicker

An android library to pick photo from gallery
https://raw.githubusercontent.com/liuling07/PhotoPicker/master/sample.apk
Apache License 2.0
330 stars 131 forks source link

app中使用application 后lib无法获取context #13

Open kipeng opened 7 years ago

kipeng commented 7 years ago
10-26 14:41:16.444: I/System.out(17766): java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getExternalCacheDir()' on a null object reference
10-26 14:41:16.444: I/System.out(17766):    at com.lling.photopicker.utils.OtherUtils.getDiskCacheDir(OtherUtils.java:113)
10-26 14:41:16.444: I/System.out(17766):    at com.lling.photopicker.utils.ImageLoader.initDiskCache(ImageLoader.java:145)
10-26 14:41:16.444: I/System.out(17766):    at com.lling.photopicker.utils.ImageLoader.init(ImageLoader.java:63)
10-26 14:41:16.444: I/System.out(17766):    at com.lling.photopicker.utils.ImageLoader.<init>(ImageLoader.java:58)
10-26 14:41:16.444: I/System.out(17766):    at com.lling.photopicker.utils.ImageLoader.getInstance(ImageLoader.java:110)
10-26 14:41:16.444: I/System.out(17766):    at com.lling.photopicker.adapters.PhotoAdapter.getView(PhotoAdapter.java:198)
10-26 14:41:16.444: I/System.out(17766):    at android.widget.AbsListView.obtainView(AbsListView.java:2346)
10-26 14:41:16.444: I/System.out(17766):    at android.widget.GridView.onMeasure(GridView.java:1065)
10-26 14:41:16.444: I/System.out(17766):    at android.view.View.measure(View.java:18811)

你好,当我app也有一个application时,lib会无法获取context,抛NullPointerException

<application
        android:allowBackup="true"
        android:name=".AppApplication"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        tools:replace="android:name">

目前经过查找资料使用的方法是:我的application继承你lib中的application解决的 http://stackoverflow.com/questions/7157501/is-it-possible-to-get-applications-context-in-an-android-library-project 请问还有其他的解决方法吗?

kipeng commented 7 years ago

是否我们的code可以做下修改,添加个init方法将appversion和context在第一次使用的时候当做参数传过去。这样lib中就不需要application

ihewro commented 6 years ago

同样是获取不到context的值