Chris
Chris

Reputation: 171

Crashing when onClickListener is added

Probably a fairly simple question but I can't seem to make sense of why any onClickListeners I'm adding. (I also seem to get a similar crash when adding a text changed listener to an EditText box, which makes me think I'm consistently setting something up wrong?)

I followed the basic Android Developers stuff which creates an onClickListener as follows...

        mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

However, I was wanting to create the Listener then have an onCreate method to execute the code.

Here is my current setup which is crashing as soon as I run it

package com.chris.formStuff;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class FormStuffActivity extends Activity implements OnClickListener {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    // Create the view
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Load all views.
    //EditText edtText1;
    Button btnChange1;

    // And find all views in the relevant layout file
    //edtText1=(EditText) findViewById(R.layout.main);
    btnChange1 = (Button) this.findViewById(R.layout.main);

    btnChange1.setOnClickListener((OnClickListener) this);
}
@Override
public void onClick(View v)
{
    Context context = getApplicationContext();
    CharSequence text = "Hello toast!";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
}
}

And the logcat output...

07-25 13:19:10.593: ERROR/AndroidRuntime(22861): FATAL EXCEPTION: main
07-25 13:19:10.593: ERROR/AndroidRuntime(22861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chris.formStuff/com.chris.formStuff.FormStuffActivity}: java.lang.ClassCastException: com.chris.formStuff.FormStuffActivity
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at android.os.Looper.loop(Looper.java:144)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at android.app.ActivityThread.main(ActivityThread.java:4937)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at java.lang.reflect.Method.invokeNative(Native Method)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at java.lang.reflect.Method.invoke(Method.java:521)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at dalvik.system.NativeStart.main(Native Method)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861): Caused by: java.lang.ClassCastException: com.chris.formStuff.FormStuffActivity
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at com.chris.formStuff.FormStuffActivity.onCreate(FormStuffActivity.java:25)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
07-25 13:19:10.593: ERROR/AndroidRuntime(22861):     ... 11 more

Upvotes: 0

Views: 1450

Answers (5)

theisenp
theisenp

Reputation: 8719

Everyone has been quick to point out that your problem is the line

btnChange1 = (Button) this.findViewById(R.id.buttonchange_id);

They are right, and that will fix this problem for you. But if you want to avoid future problems it's best for you to understand why.

The logcat usually makes fixing errors like this incredibly simple. When you get an error, the logcat output typically looks like this

07-25 13:19:10.593: ERROR/AndroidRuntime(22861): FATAL EXCEPTION: main
ERROR/AndroidRuntime(22861): java.lang.RuntimeException: /some sort of error/
    .../A lot of description/
    ...
07-25 13:19:10.593: ERROR/AndroidRuntime(22861): Caused by: /Whatever the specific error was/
    07-25 13:19:10.593: ERROR/AndroidRuntime(22861):at com.chris.formStuff.FormStuffActivity.onCreate(FormStuffActivity.java:25)
    ...
    ...

When you get errors like this you want to look at the "Caused by..." line, and the first few after that. This will describe the error and give you the exact line it occurred at. In your case the error was a "ClassCastException" at line 25 in your FormStuffActivity. Then when you look at line 25 you can see that you must be casting something to a Button, that is not in fact, a Button (as everyone else has said).

If the error had been a NullPointerException at line 406, you would know to look for a variable on that line that had not been properly initialized. The logcat is a pretty powerful debugging tool and if you learn how to use it you can fix small syntax-type bugs like this in a few seconds.

Upvotes: 2

MrJre
MrJre

Reputation: 7161

Your error is in this line according to your stacktrace: (Button) this.findViewById(R.layout.main); Apparently R.layout.main isnt a button.

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33248

you are going wrong on this line

btnChange1 = (Button) this.findViewById(R.layout.main);

means you need to find id instead of layout

same as this line

btnChange1 = (Button) this.findViewById(R.id.button);

Upvotes: 1

Nikola Despotoski
Nikola Despotoski

Reputation: 50538

Your button must be found by it ID inside the main.xml, there for R.layout.main is not his id.

btnChange1 = (Button) this.findViewById(R.layout.nameofthebuttoninsidethexml);

Upvotes: 1

Selvin
Selvin

Reputation: 6797

change btnChange1 = (Button) this.findViewById(R.layout.main);

to

btnChange1 = (Button) this.findViewById(R.id.buttonchange_id);

where buttonchange_id is id of btnChange1 in main layout

Upvotes: 3

Related Questions