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

Apps ImageButton Force Close?

I want to write an app containing an ImageButton. In order to do this, I've googled a few tutorials and browsed the Android developer site.

It seems simple enough, but everytime I've tried to get one working, I get an "application <X> has stopped unexpectedly. Please Try Again" message.

Any ideas what could be going on?

I have a very simple app here with two trivial sized images - 17k. The app simply is supposed to change the image when clicked.

Is there an issue with ImageButton in any version of the SDK?

MyImageButton.java
Code:
package com.perfectreign.imagebuttonexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ImageButton;

public class MyImageButton extends Activity {
    /** Called when the activity is first created. */
	
	ImageButton imageButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        addListenerOnButton();
        
    }
    
    public void addListenerOnButton() {
    	
    	imageButton = (ImageButton) findViewById(R.id.imageButton1);
    	
    	imageButton.setOnClickListener(new OnClickListener() {
    		@Override
    		public void onClick(View arg0) {
    			Toast.makeText(MyImageButton.this, "Image Button is Clicked.", Toast.LENGTH_SHORT).show();
    		}
    	});
    }
}


main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageButton
	android:id="@+id/imageButton1"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:src="@drawable/android_button" />

</LinearLayout>
 

Attachments

  • ScreenShot510.png
    ScreenShot510.png
    85.5 KB · Views: 134
Back
Top Bottom