I am facing one problem in AsyncTask in my application in Android. The problem is, I am starting another Activity from AsyncTask in my application which runs fine when its layout has simple Button, but when I am using ImageButton its giving me error of handler and looper.loop. I am not getting the reason why this kind of error is occurring. I am displaying menus with images in calling Activity.
Can anybody suggest me what is the problem in this and how can I achieve this kind of functionality?
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (Dialog != null && Dialog.isShowing()) {
Dialog.dismiss();
locationManager.removeUpdates(locationListener);
Intent homeIntent = new Intent(ATMActivity.this.getApplicationContext(), HomeMenuActivity.class);
homeIntent.putExtra("lat", latitude);
homeIntent.putExtra("lng", longitude);
startActivity(homeIntent);
}
}
XML File :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/atm_icon"
android:onClick="buttonClicked" />
</LinearLayout>
Another Activity Class :-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button btn = (Button) findViewById(R.id.imageButton1);
btn.setHint("ATM");
float latitude = getIntent().getFloatExtra("lat", 0);
float longitude = getIntent().getFloatExtra("lng", 0);
Toast.makeText(getApplicationContext(), "Location Floats:- " + latitude + "," + longitude, Toast.LENGTH_LONG).show();
}
Without the full stack trace it’s hard to say for sure, but my best guess is you’re actually getting a
ClassCastException.Your
ATMActivityis trying to castimageButton1, defined in xml as anImageButton, to aButton. This is not possible, as the latter is not a superclass of the former.Since you’re starting the activity from the onPostExecute of an AsyncTask, you’ll probably find some stuff mentioning a looper/handler in the stack trace.