0

Here is part of my Activity codes:

button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                IParser = new ImageParser(MainActivity.this, EarthqpicUrl);
                IParser.execute();
            }
        });

And this is part of my AsyncTask Codes:

AsyncResponse lempar; *//this is An interface*
public ImageParser(Context actv, String EarthqpicUrl){
   this.actv=actv;
   this.EarthqpicUrl=EarthqpicUrl;
   this.lempar = (AsyncResponse) actv;
}

This is the interface code:

import android.graphics.Bitmap;

public interface AsyncResponse {
    public void processFinish(Bitmap output);
}

I use the same code for parsing a JSONData, It works. But when I use the same constructor after some modification like class name, it show an error while debugging:

Thread [<1> main] (Suspended (Exception ClassCastException))
ImageParser.<init>(Context, String) Line:25
MainActivity$1.onCllick(View) line: 23 

what's wrong with this code? Can somebody help me? Thanks a lot

These is part of codes I use in JSONParser:

AsyncResponse delegate;

public JSONParser(Context actv,String url){
    this.actv=actv;
    this.url = url;
    this.delegate = (AsyncResponse) actv;
}

same as JSONParser, I use an interface named AsyncResponse.

stsho89
  • 23
  • 1
  • 5

1 Answers1

1

You are casting an object of type Context to AsyncResponse:

this.lempar = (AsyncResponse) actv;

This is what's causing the error.

Vikram
  • 51,313
  • 11
  • 93
  • 122
  • Thanx for reply, but I use this way because it works before for JSONParser instead of ImageParser. With the same code, why it can't work for ImageParser? Would you help me @vikram ? – stsho89 Jul 18 '13 at 10:40
  • 1
    Problem Solved. Found that it have to implements interface in MainActivity Class. – stsho89 Jul 19 '13 at 05:01
  • @stsho89 Sorry, I assumed from the beginning that you had already implemented the `AsyncResponse`interface in `MainActivity` because your `JSONParser` was functioning. Glad that your issue is solved. – Vikram Jul 19 '13 at 23:55