working on InterstitialAd excercise, onClick of a button, calling tellJoke(). tellJoke() calls a AsyncTask . In AsyncTask, params size is 0 , getting IndexOutOfBundException, so context is null.
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(LOG_TAG, "after setting activity main");
}
public static void tellJoke(View view){
new EndpointsAsyncTask().execute();
}
}
MainActivityFragment.java
public class MainActivityFragment extends Fragment {
InterstitialAd mInterstitialAd;
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_main, container, false);
AdView mAdView = (AdView) root.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mAdView.loadAd(adRequest);
//interstitialAd part starts here
mInterstitialAd = new InterstitialAd(getActivity());
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
MainActivity.tellJoke(getView());
}
});
return root;
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mInterstitialAd.loadAd(adRequest);
}
}
EndpointsAsyncTask.java
public class EndpointsAsyncTask extends AsyncTask<Context, Void, String> {
private static String LOG_TAG = EndpointsAsyncTask.class.getSimpleName();
private static MyApi myApiService = null;
private Context context ;
@Override
protected String doInBackground(Context... params) {
if(myApiService == null) { // Only do this once
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
.setRootUrl("https://build-it-bigger.appspot.com/_ah/api/");
// end options for devappserver
myApiService = builder.build();
}
if (params != null) {
context = params[0];
}
try {
return myApiService.fetchJoke().execute().getData();
} catch (IOException e) {
return e.getMessage();
}
}
@Override
protected void onPostExecute(String result) {
if (context != null) {
Intent intent = new Intent(context, JokeDisplayActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, result);
context.startActivity(intent);
}
}}
please help, where am i doing wrong ? and please suggest if other way to do this.
Just provide the parameter for your AsyncTask when you're calling execute
(also remove static):
public void tellJoke(View view){
new EndpointsAsyncTask().execute(getActivity());
}
this
refers to the activity and thus implements the Context
interface (if called from an activity).
EDIT:
Move your tellJoke
method to the Fragment and call it like this:
tellJoke();
Notice that now you do not pass this
but rather getActivity()
since you're in a Fragment.