The Android imagebutton onclick method was not called
Edit: publish the complete code (except XML, because it is a bunch of absurd tabular format!) please ignore the code that has nothing to do with my problem! I'm providing functionality right now. I'll clean it up later
hello everyone! The first application and the first question. I have studied here for some time and usually find my answer, but I have a bug that may be very obvious. I have an imagebutton that doesn't seem to call the specified method
XML of my imagebutton:
<ImageButton
android:background="@null"
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton1"
android:src="@drawable/stats"
android:layout_gravity="center_vertical">
</ImageButton>
My code:
package com.talismancs;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class sheet extends Activity{
private String selection;
private String pick;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sheet);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
// Get extra from .main and remove spaces
String pick = extras.getString(selection);
pick = pick.replace(" ", "");
//Convert extra from string to int as ID and make image
int imageResource = getResources().getIdentifier(pick, "drawable", getPackageName());
ImageView iv = (ImageView) findViewById(R.id.imageView1);
final Drawable image = getResources().getDrawable(imageResource);
iv.setImageDrawable(image);
// Populate tv's from string
TextView tv1 = (TextView) findViewById(R.id.textView4);
TextView tv2 = (TextView) findViewById(R.id.textView5);
TextView tv3 = (TextView) findViewById(R.id.textView6);
TextView tv4 = (TextView) findViewById(R.id.textView7);
TextView tv5 = (TextView) findViewById(R.id.textView8);
int arrayresource = getResources().getIdentifier(pick, "array", getPackageName());
String[] CharString = getResources().getStringArray(arrayresource);
tv1.setText(CharString[0]);
tv2.setText(CharString[1]);
tv3.setText(CharString[2]);
tv4.setText(CharString[3]);
tv5.setText(CharString[4]);
}
}
public void onClick(View view) {
Intent i = new Intent(sheet.this, stats.class);
i.putExtra(pick, pick);
startActivity(i);
}
}
Seems simple? When I click imagebutton, it does nothing!
Please help.
Edit: logcat after selecting a spinner item, we can access this activity. Sheet
> 03-16 06:15:38.977:
> INFO/ActivityManager(563): Displayed
> activity com.talismancs/.sheet: 766 ms
> 03-16 06:15:42.907:
> DEBUG/dalvikvm(1735): GC freed 448
> objects / 39160 bytes in 58ms 03-16
> 06:15:43.847:
> INFO/NotificationService(563):
> enqueueToast pkg=com.talismancs
> callback=android.app.ITransientNotification$Stub$Proxy@43773720
> duration=1 03-16 06:15:43.877:
> INFO/ActivityManager(563): Starting
> activity: Intent {
> comp={com.talismancs/com.talismancs.sheet} (has extras) } 03-16 06:15:43.917:
> WARN/InputManagerService(563): Window
> already focused, ignoring focus gain
> of:
> com.android.internal.view.IInputMethodClient$Stub$Proxy@43718320
> 03-16 06:15:44.527:
> INFO/ActivityManager(563): Displayed
> activity com.talismancs/.sheet: 646 ms
After that, when I clicked the imagebutton, I did nothing
resolvent:
Your code is wrong. You didn't use clicklistener to assign your button
See this sample code for how to implement it
public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(this);
}
// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
}
...
}
You can visit this link for more references