Android 尝试在AsyncTask中填充ListView时发生ClassCastException
获取错误:Android 尝试在AsyncTask中填充ListView时发生ClassCastException,android,listview,android-asynctask,Android,Listview,Android Asynctask,获取错误:java.lang.ClassCastException:android.app.Application无法强制转换为android.app.Activity 尝试在AsyncTask的onPostExecute()中填充列表视图时 为什么我会得到这个 活动 public class NotificationsActivity extends Activity { private static final String FILENAME_NOTIFICATIONS = "notifi
java.lang.ClassCastException:android.app.Application无法强制转换为android.app.Activity
尝试在AsyncTask
的onPostExecute()
中填充列表视图时
为什么我会得到这个
活动
public class NotificationsActivity extends Activity {
private static final String FILENAME_NOTIFICATIONS = "notifications.json";
ListView listView;
NotificationsAdapter adapter;
private JSONReader reader;
private ArrayList<Notification> notificationsArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifications);
getActionBar().setTitle("");
reader = new JSONReader(getApplicationContext());
notificationsArray = new ArrayList<Notification>();
listView = (ListView)findViewById(R.id.notifications_listview);
if (Utilities.isNetworkAvailable(getApplicationContext())) {
new UpdateNotifications().execute();
}
else {
Toast.makeText(getApplicationContext(), "Network Error: No Connectivity", Toast.LENGTH_SHORT).show();
if (Utilities.checkIfFileExists(getApplicationContext(), FILENAME_NOTIFICATIONS)) {
createNotificationsArray();
}
else {
Toast.makeText(getApplicationContext(), "No files found locally. Please connect to the internet and try again.", Toast.LENGTH_LONG).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.notifications, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_update:
if (Utilities.isNetworkAvailable(this)) {
new UpdateNotifications().execute();
}
else {
Toast.makeText(getApplicationContext(), "Network Error: No Connectivity", Toast.LENGTH_SHORT).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void createNotificationsArray() {
try {
reader.readFromInternal(FILENAME_NOTIFICATIONS);
notificationsArray = reader.getNotificationsArray();
}
catch (IOException e) { e.printStackTrace(); }
}
private class UpdateNotifications extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
protected void onPreExecute() {
progressDialog = new ProgressDialog(NotificationsActivity.this);
progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("Updating notifications. Please wait.");
progressDialog.show();
progressDialog.setCanceledOnTouchOutside(false);
}
protected Void doInBackground(Void... params) {
HTTPGetRequest getRequest = new HTTPGetRequest();
JSONWriter writer = new JSONWriter(getApplicationContext());
// Updates local JSON file containing notifications
String notificationsJsonString = "";
notificationsJsonString = getRequest.getNotifications();
try { writer.writeToInternal(FILENAME_NOTIFICATIONS, notificationsJsonString); }
catch (IOException e) { e.printStackTrace(); }
createNotificationsArray();
Log.v("SIZE", Integer.toString(notificationsArray.size()));
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
listView = (ListView)findViewById(R.id.notifications_listview);
adapter = new NotificationsAdapter(getApplicationContext(), R.layout.listview_notification_row, notificationsArray);
listView.setAdapter(adapter);
return;
}
}
在适配器的getView()
中,更改此代码
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
到
在适配器中进行更改
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
与
请参阅:
希望获得此帮助尝试此代码适配器=新建NotificationsAdapter(NotificationsActivity.this,R.layout.listview\u notification\u row,notificationsArray);另外,我建议不要使用getApplicationContext()
,而是使用this
或NotificationsActivity。这
代替了NotificationsActivity
中的内容。所有这些都可以正常工作了。您能简要解释一下为什么不使用getApplicationContext()
?非常感谢。根据SO上关于上下文的一些帖子,我发现通过使用getApplicationContext()
,如果处理不正确,可能会出现内存泄漏。还有,有详细的解释。
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
LayoutInflater inflater = LayoutInflater.from(context);