Android的 - 如何通过点击按钮打开活动[关闭](Android - How to open

2019-08-05 02:26发布

How do I open an Activity(GameProcess) by clicking on a button(ButStart)?

I already edited it and take to manifest. What mistakes do you see? It crashes when I tap the button(ButStart).

package com.makeandroid.klikomania;

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(KlikomaniaActivity.this,GameProcess.class); 
             startActivity(intent);
        }
    });
}

}

And Here is GameProgress Activity:

package com.makeandroid.klikomania;


public class GameProcess extends Activity {



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

        }
 }

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.makeandroid.klikomania"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity android:name="com.makeandroid.klikomania.GameProcess"></activity>
    <activity
        android:name="com.makeandroid.klikomania.KlikomaniaActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

</manifest>

Answer 1:

也许你的button Butstartnull 。 在您检查“butstart” ID main.xml



文章来源: Android - How to open Activity by clicking button [closed]