sboehnke
sboehnke

Reputation: 287

Java Android - ImageButton displays but does not allow me to click it

I create a new instance of the "runGraphics.java" class from a class that has a surfaceView. When it opens this class, it now runs into an issue when the setContentView is put in. Before it was called in a different function before the calling of the runGraphics class. Here is my activity:

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

    public class runGraphics extends Activity 
    {
        ImageButton polarCap1;

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState); 
        setContentView(R.layout.graphics);
        polarCap1 = (ImageButton) findViewById(R.id.polarCapButton);
        polarCap1.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {   
                Toast.makeText(runGraphics.this, "IT WORKED", Toast.LENGTH_LONG).show();
            }//end onClick function         
        });//end setOnClickListener


        }//end onCreate function
    }//end runGraphics class

here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >  

    <ImageButton 
        android:id="@+id/polarCapButton"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/polarcap" />

</LinearLayout>

and here is my logcat after I added the setContentView:

05-21 16:52:38.726: E/AndroidRuntime(18959): FATAL EXCEPTION: Thread-17
05-21 16:52:38.726: E/AndroidRuntime(18959): java.lang.RuntimeException: Can't create handler    inside thread that has not called Looper.prepare()
05-21 16:52:38.726: E/AndroidRuntime(18959):    at android.os.Handler.<init>(Handler.java:121)
05-21 16:52:38.726: E/AndroidRuntime(18959):    at android.app.Activity.<init>(Activity.java:704)
05-21 16:52:38.726: E/AndroidRuntime(18959):    at com.twentytwentythree.sab.runGraphics.<init>(runGraphics.java:11)
05-21 16:52:38.726: E/AndroidRuntime(18959):    at com.twentytwentythree.sab.GraphicsSurface$SetupGraphicsSurface.run(GraphicsSurface.java:280)
05-21 16:52:38.726: E/AndroidRuntime(18959):    at java.lang.Thread.run(Thread.java:1019)

Upvotes: 1

Views: 86

Answers (3)

Social_Engineerer
Social_Engineerer

Reputation: 108

     public class runGraphics extends Activity implements OnClickListener {

    ImageButton polarCap1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

    polarCap1 = (ImageButton) findViewById(R.id.polarCapButton);
    polarCap1.setOnClickListener(this);
    }//end onCreate function


   @Override
   public void onClick(View v) {
   switch (v.getId()) {
   case R.id.polarCapButton:
   Toast.makeText(runGraphics.this, "IT WORKED", Toast.LENGTH_LONG).show();            
   break;
   }
  }
 }

Upvotes: 0

Jorgesys
Jorgesys

Reputation: 126563

you are missing

setContentView(R.layout.activity_main);

this must be your onCreate() method :

      @Override
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.activity_main); //add the line to load your .xml layout
            polarCap1 = (ImageButton) findViewById(R.id.polarCapButton);
            polarCap1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){   
                Toast.makeText(runGraphics.this, "IT WORKED", Toast.LENGTH_LONG).show();
            }//end onClick function         
        });//end setOnClickListener
      }//end onCreate function

Upvotes: 1

Chefes
Chefes

Reputation: 1942

public class runGraphics extends Activity {
    ImageButton polarCap1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout.xml);//You need this code line.

        polarCap1 = (ImageButton) findViewById(R.id.polarCapButton);
        polarCap1.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                        Toast.makeText(runGraphics.this, "IT WORKED",
                        Toast.LENGTH_LONG).show();
               }//end onClick function         
        });//end setOnClickListener


    }//end onCreate function
}//end runGraphics class

Upvotes: 1

Related Questions