• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Simple Android Application help

RaulGogo

Lurker
I'm new to Android and I started doing some tutorials (HelloWorld and stuff). I found some Hello World tutorials based on views which are pretty neat.

But, for this particular one I wanted to see some user-interaction, so I wanted to implement a method that clears the text entry when Cancel button is pushed.

main.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Type here:"/>
    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/label"/>
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     	android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:text="OK" />
        
    <Button
    	android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_below="@id/entry"
        android:text="Cancel" />
</RelativeLayout>

Java file:
Code:
package ro.examples.hellorelativelayout;

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


public class HelloRelativeLayout extends Activity {
	private Button cancelButton;
	private EditText editText;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        editText = (EditText)this.findViewById(R.id.entry);
        cancelButton = (Button)this.findViewById(R.id.cancel);
        cancelButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
				editText.setText("");
			}
		});
    }
}

When starting Android it crashes ("The application Hello, Relative Layout has stopped unexpectedly. Please try again").

Help please for the newbie in me :D.
 
I believe the editText variable needs to be final when the listener is given in a anonymous inner class.

Code:
package ro.examples.hellorelativelayout;

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


public class HelloRelativeLayout extends Activity {
	private Button cancelButton;
	private final EditText editText;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        editText = (EditText)this.findViewById(R.id.entry);
        cancelButton = (Button)this.findViewById(R.id.cancel);
        cancelButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
				editText.setText("");
			}
		});
    }
}
 
Replace

editText = (EditText)this.findViewById(R.id.entry);
cancelButton = (Button)this.findViewById(R.id.cancel);

With
editText = (EditText)findViewById(R.id.entry);
cancelButton = (Button)findViewById(R.id.cancel);
 
Oh, I sort of forgot about this thread.

Thank you, that worked fine ! I would probably have never figured that out by myself.
 
Back
Top Bottom