ファイルが完全にダウンロードされたときに共有設定にダウンロード ステータスを保存するベスト プラクティスは何ですか?
私の質問はこれに似ていますが、不完全なダウンロードファイルの扱いですが、これを行うための最良の方法が見つかりません。
私asynctask
onPostExecute
の例でファイルが完全にダウンロードされたことを保存したいと思います:
protected void onPostExecute(Void result) {
mDownloadPrefs.setDownloadState(item.FileName());
}
しかし...ダウンロードステータスを取得しようとすると、アプリケーションが閉じます:
Boolean exist = mDownloadPrefs.getDownloadState(item.FileName());
if(exist){
...
設定ファイルは次のようになると思います。
- FileName001 = true
- FileName002 = true
- FileName003 = true
DownloadPreferences クラス:
public class DownloadPreferences {
private static final String APP_SHARED_PREFS = DownloadPreferences.class.getSimpleName(); // Name of the file -.xml
private SharedPreferences _sharedPrefs;
private Editor _prefsEditor;
public DownloadPreferences(Context context) {
this._sharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
this._prefsEditor = _sharedPrefs.edit();
}
public Boolean getDownloadState(String name) {
boolean isdownload = _sharedPrefs.getBoolean(name, false);
return isdownload;
}
public void setDownloadState(String name) {
_prefsEditor.putBoolean(name, true);
_prefsEditor.commit();
}
}
更新: Logcat の結果:
AndroidRuntime: Shutting down VM
dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40f1c300)
AndroidRuntime:java.lang.NullPointerException
AndroidRuntime: at com.myapp.reader.AdapterReader.getView(AdapterReader.java:145)
AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2267)
AndroidRuntime: at android.widget.GridView.onMeasure(GridView.java:1030)
AndroidRuntime: at android.view.View.measure(View.java:15172)
AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
AndroidRuntime: at android.view.View.measure(View.java:15172)
AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
AndroidRuntime: at android.view.View.measure(View.java:15172)
AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
AndroidRuntime: at android.view.View.measure(View.java:15172)
AndroidRuntime: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:555)
AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:525)
AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:615)
AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4745)
AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
ありがとう