Andrew Brooke
Andrew Brooke

Reputation: 12153

Android App Crashes on Button Click

I have been attempting to make my first android application (a simple temperature converter) in Eclipse, but when I click the button on my phone the app crashes. Here is the full java code

package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
String number;
int number2;
int output;
boolean F;
public void onBtnClicked(View view){

    EditText mEdit = (EditText)findViewById(R.id.editText1);
    TextView myTextView = (TextView) findViewById(R.id.label);

number = mEdit.getText().toString();
number2 = Integer.parseInt(number);

if(F=true){
output=number2*9/5+32;
}
else{
output=number2-32*5/9;
}

myTextView.setText(output);
} 

public void onRadioButtonClicked(View view) {

    boolean checked = ((RadioButton) view).isChecked();

    switch(view.getId()) {
        case R.id.radio0:
            if (checked)
                F = true;
            break;
        case R.id.radio1:
            if (checked)
                F = false;
            break;
    }
}
}

LogCat when the button is clicked

04-13 20:19:50.423: E/AndroidRuntime(25200): FATAL EXCEPTION: main
04-13 20:19:50.423: E/AndroidRuntime(25200): java.lang.IllegalStateException: Could not execute method of the activity
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.view.View$1.onClick(View.java:3674)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.view.View.performClick(View.java:4198)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.view.View$PerformClick.run(View.java:17158)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.os.Handler.handleCallback(Handler.java:615)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.os.Looper.loop(Looper.java:137)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.app.ActivityThread.main(ActivityThread.java:4918)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at java.lang.reflect.Method.invokeNative(Native Method)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at java.lang.reflect.Method.invoke(Method.java:511)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at dalvik.system.NativeStart.main(Native Method)
04-13 20:19:50.423: E/AndroidRuntime(25200): Caused by: java.lang.reflect.InvocationTargetException
04-13 20:19:50.423: E/AndroidRuntime(25200):    at java.lang.reflect.Method.invokeNative(Native Method)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at java.lang.reflect.Method.invoke(Method.java:511)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.view.View$1.onClick(View.java:3669)
04-13 20:19:50.423: E/AndroidRuntime(25200):    ... 11 more
04-13 20:19:50.423: E/AndroidRuntime(25200): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x59
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.content.res.Resources.getText(Resources.java:242)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at android.widget.TextView.setText(TextView.java:3773)
04-13 20:19:50.423: E/AndroidRuntime(25200):    at  com.example.myfirstapp.MainActivity.onBtnClicked(MainActivity.java:43)
04-13 20:19:50.423: E/AndroidRuntime(25200):    ... 14 more
04-13 20:19:50.453: E/android.os.Debug(718): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error

And lastly for the xml of the button

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/radioGroup1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:onClick="onBtnClicked"
    android:text="Calculate" />

I'm not sure how to go about fixing this, so hopefully someone can help. Thanks.

Upvotes: 0

Views: 44279

Answers (5)

linkery
linkery

Reputation: 1

Try to add in file AndroidManifest.xml the following code:

<activity
        android:name=".SecondActivity"
        android:exported="true">
</activity>

Its helped me, i think its help you

Upvotes: 0

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

Initialize your Buttons first then set onclicklistener to them

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //initialize views here 
    EditText mEdit = (EditText) findViewById(R.id.editText1);
    TextView myTextView = (TextView) findViewById(R.id.label);
    Button yourButton = (Button) findViewByid(R.id.youridforbutton);
    //set onclicklistener for your button
    yourbutton.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number = mEdit.getText().toString();
                number2 = Integer.parseInt(number);

                if (F = true) {
                    output = number2 * 9 / 5 + 32;
                } else {
                    output = number2 - 32 * 5 / 9;
                }

                myTextView.setText("" + output);
            }
        });

}

Similarly set the other button also

Upvotes: 7

Ahmad
Ahmad

Reputation: 72533

You can not set Integers to TextViews. You have to convert them into Strings.

myTextView.setText(String.valueOf(output));

Upvotes: 1

Gustek
Gustek

Reputation: 3760

You need to cast output to String

myTextView.setText(String.valueOf(output));

setText method is overloaded and when You pass an integer to it it expects it to be an id of resource.

Upvotes: 3

Shobhit Puri
Shobhit Puri

Reputation: 26007

You need to tell the click listener to listen to a particular button. So, in other words set an OnItemClickListener on the button. Something as follows:

Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    // do the "on click" action here
  }
});

Hope this helps. If not, then please comment with further issue.

Upvotes: 0

Related Questions