0

I am currently developing an android app. I have 4 layouts in my app(1 main layout and 3 sub layouts). In my main layout I am using three imagebuttons and if each button is clicked it starts an activity. i.e., when a button is clicked it transitions over to the next layout. I used the onClicklistener() method to handle the event when the button is clicked. The problem is that when the first button is clicked it changes to the next layout successfully but when the other two buttons are clicked the app force closes. In each sub layout I use a listview to display some content. Here is the code for the mainactivity:

public class MainactivityActivity extends Activity implements View.OnClickListener  {
 @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainactivity);  
 ImageButton m1 = (ImageButton) findViewById(R.id.imageButton1);
 ImageButton m2 = (ImageButton) findViewById(R.id.imageButton2);   
 ImageButton m3 = (ImageButton) findViewById(R.id.imageButton3);
m1.setOnClickListener(this);
m2.setOnClickListener(this);
m3.setOnClickListener(this);
}


@Override
public void onClick(View v) {

           switch(v.getId()) {
               case R.id.imageButton1:
                    Intent intent= new Intent(MainactivityActivity.this,Inspire.class);
                    startActivity(intent);
                    break;
               case R.id.imageButton2:
                   Intent inte = new Intent(MainactivityActivity.this,Love.class);
                   startActivity(inte);
                   break;
               case R.id.imageButton3:
                   Intent inten = new Intent(MainactivityActivity.this,Other.class);
                   startActivity(inten);
                   break;
                   default:
       }
}}

I also added the value android:onclick="onClick" in the xml layout for each button. when the first image button it transfers to the next layout but when the other image buttons are clicked my app force closes and I get errors. I tried googling it but I couldn't find a perfect solution. Please help me solve this. Thanks in advance

eashdroidian
  • 33
  • 2
  • 6
  • Can you post the logcat of when the app crashes please? – Rob Jun 26 '14 at 13:52
  • Can you please try to use some of the Android terms rather than saying layout for all of them? I do not mean to be rude. It makes clearer when you try to explain your problem. – hoomi Jun 26 '14 at 14:00

5 Answers5

0

You have to remove android:onclick="onClick" from your xml. Because in your activity your implement onClickListener and when you click on button it picks onClick method on activity but it won't find anything that's the reason you getting error.

duggu
  • 37,851
  • 12
  • 116
  • 113
0

use either setOnClickListener() to set the click listener pragmatically or declare the onClick method in your manifest but don't do both. Try this first and see if it works. If it does not work, then you can update the question by posting the logcat error message, this will help us to narrow down the problem for you.

here's a tutorial on using logcat. :) good luck !

cyc115
  • 635
  • 2
  • 14
  • 27
0

You have mixed two ways of setting clicklistener available in android. I will clarify them for you.

  • One way is to use android:onclick="ClickMe" attribute in your xml file. If you do this you DO NOT need to implement onClickListener or setOnClicklistener in your java file. you simple need to create a method in your java file that is named ClickMe and that takes View as its parameter. example below

following line should be in your xml file

android:onclick="ClickMe"

and following method should be in your activity.java file

public void ClickMe(View v){
    switch(v.getId()) {
        case R.id.imageButton1:
               Intent intent= new Intent(MainactivityActivity.this,Inspire.class);
               startActivity(intent);
               break;
        case R.id.imageButton2:
              Intent inte = new Intent(MainactivityActivity.this,Love.class);
              startActivity(inte);
              break;
        case R.id.imageButton3:
              Intent inten = new Intent(MainactivityActivity.this,Other.class);
              startActivity(inten);
              break;
        default:    
    }
}

Thats it, it should work as expected but don't forget to remove the setOnClicklisteners from java file.

  • Second method is to dynamically set the onClickListener from activity.java file.

Remove following line/s from xml file

android:onclick="onClick"

Thats it. your code should work.

PS Don't forget to add your activities in Manifest.xml

madteapot
  • 2,208
  • 2
  • 19
  • 32
  • I removed the android:onclick="onClick " and yeah I actually forgot to add the activities in the manifest. I added them and now it is working fine. Thank You so much :) – eashdroidian Jun 26 '14 at 14:41
0

Try this way,hope this will help you to solve your problem.

Note : before do below code remove this "android:onclick="onClick"" line all ImageButton from xml.

public class MainactivityActivity extends Activity {
    ImageButton m1;
    ImageButton m2;
    ImageButton m3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);
        m1 = (ImageButton) findViewById(R.id.imageButton1);
        m2 = (ImageButton) findViewById(R.id.imageButton2);
        m3 = (ImageButton) findViewById(R.id.imageButton3);

        m1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainactivityActivity.this, Inspire.class);
                startActivity(intent);
            }
        });
        m2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent inte = new Intent(MainactivityActivity.this, Love.class);
                startActivity(inte);
            }
        });
        m3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent inten = new Intent(MainactivityActivity.this, Other.class);
                startActivity(inten);
            }
        });
    }
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

Remove the line android:onclick="onClick" from your xml. Because in your activity you are already using onclicklistener. See this

Furthermore Inspire.java, Love.java & Other.java classes should also be declared in the manifest.xml. See this

Community
  • 1
  • 1
Vivek Warde
  • 1,936
  • 8
  • 47
  • 77
  • 1
    Yeah i removed it and declared the other classes in the manifest and now it is working fine. Thanks for your valuable suggestion. – eashdroidian Jun 26 '14 at 14:42