我正在尝试使用HttpResponseCache缓存网络数据,但是当我将安装缓存代码放入OnCreate和OnPause方法时,应用程序崩溃。我使用了HttpResponseCache 文档中的代码。并出现如下所示的编译错误。使用后
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
为了解决它消失的错误,我运行应用程序但仍然崩溃。当我删除该代码时,它会恢复正常行为。
protected void onCreate(Bundle savedInstanceState) {
...
try {
File httpCacheDir = new File(context.getCacheDir(), "http");
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize); //error
} catch (IOException e) {
Log.i(TAG, "HTTP response cache installation failed:" + e);
}
}
protected void onStop() {
...
HttpResponseCache cache = HttpResponseCache.getInstalled();//error
if (cache != null) {
cache.flush();//error
}
}}
我是初学者,如果有人可以提供帮助,我将不胜感激。这是 MainActivity 代码
public class MainActivity extends AppCompatActivity {
private MovieAdapter movieAdapter;
private Context context;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ------ caching data ------------------
try {
File httpCacheDir = new File(context.getCacheDir(), "http");
long httpCacheSize = 2 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize); //error
} catch (IOException e) {
Log.i("caching failed", "HTTP response cache installation failed:" + e);
}
//-----------------------
DataSyncTask dataSyncTask = new DataSyncTask();
dataSyncTask.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
private class DataSyncTask extends AsyncTask<Void, Void, ArrayList<Movie>> {
@Override
protected ArrayList<Movie> doInBackground(Void... params) {
JSONHttpClient jsonHttpClient = new JSONHttpClient(MainActivity.this);
JSONDataParser finalData = new JSONDataParser();
/*
using shared preference to obtain data
*/
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String sortingOrder = sharedPreferences.getString(
getString(R.string.pref_findMovies_key),
getString(R.string.pref_popular_value));
String streamData = jsonHttpClient.getData(sortingOrder);
return finalData.getParsedData(streamData);
}
@Override
protected void onPostExecute(final ArrayList<Movie> movies) {
movieAdapter = new MovieAdapter(getApplicationContext(), movies);
GridView gridView = (GridView) findViewById(R.id.movies_grid);
gridView.setAdapter(movieAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Movie movie = (Movie) adapterView.getItemAtPosition(i);
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("MovieTitle", movie.getTitle());
intent.putExtra("voteAverage", movie.getVoteAverage());
intent.putExtra("ReleaseDate", movie.getReleaseDate());
intent.putExtra("MovieOverview", movie.getOverview());
intent.putExtra("PosterPath", movie.getPosterPath());
startActivity(intent);
}
});
}
}
@Override
protected void onResume() {
DataSyncTask dataSyncTask = new DataSyncTask();
dataSyncTask.execute();
super.onResume();
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onStop() {
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.flush();
}
super.onStop();
}
}
应用程序崩溃后出现在 logcat 中的内容
12-19 19:22:58.465 17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
12-19 19:22:58.465 17548-17548/com.example.geekymind.displayjsondatafrominternet I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
12-19 19:22:58.465 17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to resolve interface method 14585: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
12-19 19:22:58.465 17548-17548/com.example.geekymind.displayjsondatafrominternet I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
12-19 19:22:58.465 17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to resolve interface method 14589: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
12-19 19:22:58.505 17548-17548/com.example.geekymind.displayjsondatafrominternet I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-19 19:22:58.505 17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to resolve virtual method 451: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-19 19:22:58.515 17548-17548/com.example.geekymind.displayjsondatafrominternet I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-19 19:22:58.515 17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ VFY: unable to resolve virtual method 473: Landroid/content/res/TypedArray;.getType (I)I
12-19 19:22:58.585 17548-17548/com.example.geekymind.displayjsondatafrominternet W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415ffc80)