I am new to Android development. This compiles in Eclipse without any errors, but when I run it in the emulator, it crashes, why?

package com.buttontest;

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

public class ButtonTest extends Activity {
	int timesPressed = 0;
	String timesPressedText = "";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final Button Button = (Button) findViewById(R.id.Button);
        final TextView Textview = (TextView) findViewById(R.id.ButtonPressedText);
        Button.setOnClickListener(new View.OnClickListener() {
			

			@Override
			public void onClick(View v) {
				timesPressed++;
				timesPressedText = getString(timesPressed);
				Textview.setText(timesPressedText);
			}
		});
        
    }
}

try this...

@Override
public void onClick(View v) {
    timesPressed++;
    timesPressedText = timesPressed.toString();
    Textview.setText(timesPressedText);
}

Edited 6 Years Ago by Neosmithk: n/a

I tried that, it tells me "cannot perform toString() on primitive type int"

You can try timesPressedText = (Integer) timesPressed.toString(); but I see really no reason why above shouldn't work. Besides, why you are converting to different type since since TextView is able to take int too setText(int resid)

It still won't let me run it. It says "cannot convert from int to string"

The only way it will let me run it is if I put timesPressed into setText(). Then when I run it in the emulator and press the button, it crashes.

I have it set on Android 1.1, could a higher version of Android possibly help?

Jesus man, you should said so. Nobody is using SDK 1.1 any more as it was developed before first real devices been available over 2 years ago. Get your self newer version. There are still some people working on SDK 1.5, but most are on 1.6
Also you can go extreme and use SDK 2.1

Thanks, I'll try it. I was just going for max compatibility.

Uggg... My AVD is running 2.1, and the project is set to 2.0. Now it crashes as soon as the app starts... Eclipse says there's nothing wrong with it, why is it crashing?

package app.button;

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

public class button extends Activity {
	int pressed = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final Button press_me = (Button) findViewById(R.id.button);
        final TextView num_pressed = (TextView) findViewById(R.id.num_pressed);
        
        press_me.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				pressed++;
				num_pressed.setText(pressed);
			}
		}); {
        	
        }
    }
}

The button is simply and image button and num_pressed is a normal TextView. My directories are correct.

This article has been dead for over six months. Start a new discussion instead.