boakye_wozniac
boakye_wozniac

Reputation: 37

Error Implementing multiple Image Buttons in Android Eclipse Development

I am trying to create a simple android quiz app. I have sucessfully created the activity class and the xml file, which shows no errors. but when i run the program in my emulator , soon as it gets to that specific activity class where i have multiple image buttons, the programs stops unexpectedly. I don't know what to do. need help!

HEre is a sample of my logcat. ![enter image description here][1]

This is my code

package com.example.brainlab;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

import android.view.Menu;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
//import android.widget.TextView;
import android.widget.Toast;

public class Question9 extends Activity implements OnClickListener{
   private ImageButton button1;
   private ImageButton button2;
   private ImageButton button3;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_queston9);

    button1 = (ImageButton) findViewById(R.id.imageButton1);
    button1.setOnClickListener(this); 
    button2 = (ImageButton) findViewById(R.id.imageButton2);
    button2.setOnClickListener(this); 
    button3 = (ImageButton) findViewById(R.id.imageButton3);
    button3.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.question1, menu);
    return true;
}

@Override
public void onClick(View v) {

    Intent intent = new Intent(Question9.this, Question2.class);
            switch(v.getId()){

            case (R.id.imageButton1):{
                                    Toast.makeText(getApplicationContext(), "Answer     saved.", Toast.LENGTH_SHORT).show();
                startActivity(intent);

            }
            break;

            case (R.id.imageButton2):
            {

                Toast.makeText(getApplicationContext(), "Answer       saved.", Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }

            break;

            case (R.id.imageButton3):
            {

Toast.makeText(getApplicationContext(), "Answer saved.", Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }
            }

}
}

Upvotes: 0

Views: 131

Answers (2)

boakye_wozniac
boakye_wozniac

Reputation: 37

Thanks. i i fixed it. I also had a problem with the activity not been declared in the manifest file which i have done. Now everything works fine..

Upvotes: 0

M D
M D

Reputation: 47807

I think your Switch Case is wrong. try to correct with below

 switch(v.getId()){

        case R.id.imageButton1:

         //Do your job
         break;
        case R.id.imageButton2:

         //Do your job
         break;
        case R.id.imageButton3:

         //Do your job
         break;

        default:
     break; 
     }

Upvotes: 1

Related Questions