问题
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
回答1:
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.
回答2:
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 !
回答3:
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 namedClickMe
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
回答4:
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);
}
});
}
}
回答5:
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
来源:https://stackoverflow.com/questions/24432183/how-to-fire-different-activities-when-buttons-are-clicked-in-android