I’m a bit confused, I set up my app as a simple converter but it isn’t working. My log says it has to do with myclickhandler, but I’m not sure where to go with that.
package hmdywifinal.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class Butterfliesstart extends Activity {
private ImageButton ConvertButterflies = null;
private ImageButton Butterfliesstart1 = null;
private EditText YourWeight = null;
private TextView Output = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.butterfliesstart);
//GO BACK
ImageButton Butterfliesstart1 = (ImageButton)findViewById(R.id.testbutton2);
Butterfliesstart1.setOnClickListener(new View.OnClickListener() {
public void onClick(View argo) {
setResult(RESULT_OK);
finish();
};
});
//Set IDS
ImageButton ConvertButterflies = (ImageButton)findViewById(R.id.testbutton1);
//Set Values
class ToButterflies implements View.OnClickListener
{
public void onClick(View v)
{
convertToButterflies();
}
private void convertToButterflies()
{
String ButterflyString = YourWeight.getText().toString();
double f = Double.parseDouble(ButterflyString);
double c = 5.0/9.0 * (f - 32.0);
EditText YourWeight = (EditText)findViewById(R.id.editText1);
Output.setText(String.valueOf(c));
}
}
Output = new EditText(this);
EditText Output = (EditText)findViewById(R.id.editText2);
//Initialize
Butterfliesstart1 = new ImageButton(this);
ConvertButterflies = new ImageButton(this);
YourWeight = new EditText (this);
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Butterfliesstart"></TextView>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<ImageButton
android:text="and back."
android:src="@drawable/dirtbutton"
android:id="@+id/testbutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick=""
>
</ImageButton>
<ImageButton
android:src="@drawable/bumblebees"
android:id="@+id/testbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
>
</ImageButton>
</LinearLayout>
After I press the button to convert it, it force closes. Did I miss anything in the code?
Unless you haven’t posted all of it, your
ImageButtonwith the id oftestbutton1has anandroid:onClickdefined asmyClickHandlerbut you haven’t defined that method. That’ll crash quite successfully every time you press it.I agree with JoxTraex, though… this is pretty easy stuff to track if you take the time to learn to use the tools you have. Read up on DDMS.