-1

Possible Duplicate:
Android - How to open Activity by clicking button

i want to open another Activity(GameProcess) from this one (KlikomaniaActivity) through button, but when i tap the button the program crashes. i don't a proffesional android programmer, please say what mistakes i have:

packagecom.makeandroid.klikomania;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class KlikomaniaActivity extends Activity {

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

    final Button Butstart = (Button)findViewById(R.id.butstart);
    final Button Butrez = (Button)findViewById(R.id.butrez);

    Butstart.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent("com.makeandroid.klikomania.GameProcess");
             // эапускаем деятельнсть 
             startService(intent);
        }
    });
}

}

and here GameProcess Acticity:

public class GameProcess extends KlikomaniaActivity {

private static int rezult = 0;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gameprocess);

        final Button Butklik = (Button)findViewById(R.id.klik);
        final TextView TextTime = (TextView)findViewById(R.id.texttime);
        final TextView TextKolvo = (TextView)findViewById(R.id.kolvo);

        Butklik.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            rezult=rezult+1;    
            TextKolvo.setText(rezult);
            }
        });
 }
}
0

1 Answer 1

0

First make sure that you added your activity to the manifest file :

<activity android:name="com.makeandroid.klikomania.GameProcess"></activity>

second, to start the activity use this code:

final Intent gameProcessIntent= new Intent(this, GameProcess.class);
Butstart.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        startActivity(gameProcessIntent);
    }
});

this should work normaly

5
  • Eclipse think that it is an error and ask to "remove arguments to match 'Intent()'" Dec 30, 2012 at 20:09
  • The problem is with you using this within the OnClickListener(). When you call new Intent(this, GameProcess.class), this is actually the OnClickListener instance not that Activity instance since you are inside of a new View.OnClickListener class. Please check the edit in the answer
    – Ahmad
    Dec 30, 2012 at 20:19
  • can you put the error message you get in eclipse when the app crashes ??
    – Ahmad
    Dec 30, 2012 at 20:39
  • no error, black screen and then home screen Dec 31, 2012 at 7:26
  • Now I see where is the problem.. the GameProcess class should extends "Activity" inorder for it to work as activity. so you should change the extends KlikomaniaActivity to GameProcess extends Activity
    – Ahmad
    Dec 31, 2012 at 7:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.