There was an error during development, but I don't know how to handle it, so I'm asking you a question. I'm developing a simple game This error occurs when I pressed the game start button on the first screen. What should I do to flip the screen? The emulator keeps turning off the app. I have registered for manifest.
Error: E/Android Runtime: FATAL EXCEPTION: main Process: com.example.pc1.myapplication, PID: 3363 java.lang.IllegalStateException: Could not find method GameStart(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'StartButton'
Code (Main Java)
public void GameStart(View view){
Intent intent = new Intent(getApplication(), GameActivity.class);
startActivity(intent);
}
(Main Activity)
(main activity)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GAME START"
android:textSize="39dp"
android:layout_marginBottom="14dp"
android:id="@+id/StartButton"
android:onClick="GameStart"
android:layout_above="@+id/SaveButton"
android:layout_centerHorizontal="true" />
MainActivity.java
public class MainActivity extensions AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void GameStart(View view) {
Intent intent = new Intent(getApplication(), GameActivity.class);
startActivity(intent);
finish(); // Remove current activity and display other activity.
}
}
Please check if GameActivity.java and activity_game.xml are also registered in Manifests and give us feedback if there is a continuous error.
I think it's an error because I couldn't find the context new Intent(getApplication(), GameActivity.class);
Check out getApplication() here
© 2024 OneMinuteCode. All rights reserved.