• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Can't execute AsyncTask

Hi, I need to iniciate the GetContacts class:
new GetContacts((NovinkyActivity) getActivity()).execute();

But the debugger says: java.lang.ClassCastException: MainActivity cannot be cast to NovinkyActivity

Seems the getActivity() gives the MainActivity, which started the fragment. But can you please help me what to use there instead?

For better understanding here is my full code:

public class NovinkyFragment extends Fragment{

ArrayList<Actors> actorsList;
ActorAdapter adapter;

@override
public View onCreateView(LayoutInflater inflater,
@nullable ViewGroup container, @nullable Bundle savedInstanceState) {

View vw=inflater.inflate(R.layout.novinky_fragment, container, false);
ListView lv = vw.findViewById(R.id.listView1);

actorsList = new ArrayList<Actors>();
adapter = new ActorAdapter(getActivity(), R.layout.novinkydata_item, actorsList);
lv.setAdapter(adapter);

new GetContacts((NovinkyActivity) getActivity()).execute();
return vw; }

private static class GetContacts extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
String TAG = NovinkyActivity.class.getSimpleName();
ArrayList<Actors> actorsList;
ActorAdapter adapter;

private WeakReference<NovinkyActivity> activityReference;
GetContacts(NovinkyActivity context) {activityReference = new WeakReference<>(context); }

@override
protected void onPreExecute() {
super.onPreExecute();
NovinkyActivity activity = activityReference.get();
dialog = new ProgressDialog(activity);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false); }

@override
protected Boolean doInBackground(String... args) {

HttpHandler sh = new HttpHandler();
String url = "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors";
String jsonStr = sh.makeServiceCall(url);
final NovinkyActivity activity = activityReference.get();

if (jsonStr != null) {
try {JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray actors = jsonObj.getJSONArray("actors");

for (int i = 0; i < actors.length(); i++) {
JSONObject c = actors.getJSONObject(i);

Actors actor = new Actors();

actor.setName(c.getString("name"));
actor.setDescription(c.getString("description"));
actorsList.add(actor); }

} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
activity.runOnUiThread(new Runnable() {
@override
public void run() {
Toast.makeText(activity.getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}); }

return true;

} else {
Log.e(TAG, "Couldn't get json from server.");
activity.runOnUiThread(new Runnable() {
@override
public void run() {
Toast.makeText(activity.getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG).show();
}
});
return false;
}
}
protected void onPostExecute(Boolean result) {

super.onPostExecute(result);
dialog.dismiss();
adapter.notifyDataSetChanged();
}
}
}
 
Back
Top Bottom