Austin Green
Austin Green

Reputation: 37

setOnClickListener causes force close in Android

I am trying to create an onClickListener for a button. Everything shows up fine before I create the event listener, but force closes when I add it in.

Here is the code: package com.austin.mobile;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class TxtLingoActivity extends Activity {
/** Called when the activity is first created. */
Button convert;
EditText inputText, outputText;
String myCopy;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);

    convert = (Button) findViewById(R.id.inputTextBox);
    convert.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

}
}

Here is the xml:

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Input"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/inputTextBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>


<Button
    android:id="@+id/convert"
    android:layout_gravity="right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:text="Convert" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Output"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/outputTextBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

Log Cat is showing this error: 03-19 18:05:21.726: E/dalvikvm(662): Unable to open stack trace file'/data/anr/traces.txt': Permission denied

Any suggestions?

Upvotes: 0

Views: 784

Answers (2)

px3j
px3j

Reputation: 236

Looks like you are referring to the incorrect id when 'finding' the Button:

Try to change this:

convert = (Button) findViewById(R.id.inputTextBox);

to this:

convert = (Button) findViewById(R.id.convert);

Hope this helps...

Upvotes: 2

Samir Mangroliya
Samir Mangroliya

Reputation: 40416

its ClassCastException in this line convert = (Button) findViewById(R.id.inputTextBox);

Because R.id.inputTextBox is EditText and you use as Button .change it in R.id.convert

Upvotes: 1

Related Questions