Android Activities startactivity [duplicate]

2019-09-22 04:03发布

问题:

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);
            }
        });
 }
}

回答1:

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