无法在父级或祖先上下文中找到方法
Could not find method in a parent or ancestor Context
问题
我得到 java.lang.IllegalStateException: Could not find method secondOne(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton
我做了什么?
在清单中,我有活动:
<activity
android:name=".MainActivity"
android:label="@string/app_name">
</activity>
<activity
android:name=".LeadActivity"
android:label="@string/app_name">
</activity>
<activity
android:name=".MainMenu"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
在game_menu.xml中,我有两个按钮。第一个有android:onClick="firstOne"
属性,第二个有android:onClick="secondOne"
属性.
在class MainMenu
里面extends AppCompatActivity
我有两个方法:
public void firstOne(View view) {
Intent intent = new Intent(MainMenu.this, MainActivity.class);
startActivity(intent);
}
public void secondOne(View view) {
Intent intent = new Intent(MainMenu.this, LeadActivity.class);
startActivity(intent);
}
当我点击第一个按钮时,我的 MainActivity.class 会正常运行。
但是当我点击第二个按钮时出现这个错误。 java.lang.IllegalStateException: Could not find method secondOne(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton
请帮忙。我做错了什么?
UPD
完整主菜单class代码:
public class MainMenu extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_menu);
//Hide the status bar and the action bar
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
}
public void firstOne(View view) {
Intent intent = new Intent(MainMenu.this, MainActivity.class);
startActivity(intent);
}
public void secondOne(View view) {
Intent intent = new Intent(MainMenu.this, LeadActivity.class);
startActivity(intent);
}}
UPD2
game_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/sky"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="-16dp" />
<Button
android:id="@+id/button_start"
android:layout_width="175dp"
android:layout_height="76dp"
android:layout_marginTop="200dp"
android:background="@color/black"
android:onClick="firstOne"
android:text="@string/playButtonText"
android:textColor="@color/white"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_lead"
android:layout_width="175dp"
android:layout_height="76dp"
android:layout_marginTop="312dp"
android:background="@color/black"
android:onClick="secondOne"
android:text="@string/leaderboardButtonText"
android:textColor="@color/white"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
那太糟糕了。我在这上面花了很多时间,但答案很简单。始终关心 onCreate
方法,实际上 setContentView(R.layout.);
行。我一直在复制粘贴并尝试创建具有相同布局的 类。
问题
我得到 java.lang.IllegalStateException: Could not find method secondOne(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton
我做了什么?
在清单中,我有活动:
<activity
android:name=".MainActivity"
android:label="@string/app_name">
</activity>
<activity
android:name=".LeadActivity"
android:label="@string/app_name">
</activity>
<activity
android:name=".MainMenu"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
在game_menu.xml中,我有两个按钮。第一个有android:onClick="firstOne"
属性,第二个有android:onClick="secondOne"
属性.
在class MainMenu
里面extends AppCompatActivity
我有两个方法:
public void firstOne(View view) {
Intent intent = new Intent(MainMenu.this, MainActivity.class);
startActivity(intent);
}
public void secondOne(View view) {
Intent intent = new Intent(MainMenu.this, LeadActivity.class);
startActivity(intent);
}
当我点击第一个按钮时,我的 MainActivity.class 会正常运行。
但是当我点击第二个按钮时出现这个错误。 java.lang.IllegalStateException: Could not find method secondOne(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton
请帮忙。我做错了什么?
UPD
完整主菜单class代码:
public class MainMenu extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_menu);
//Hide the status bar and the action bar
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
}
public void firstOne(View view) {
Intent intent = new Intent(MainMenu.this, MainActivity.class);
startActivity(intent);
}
public void secondOne(View view) {
Intent intent = new Intent(MainMenu.this, LeadActivity.class);
startActivity(intent);
}}
UPD2
game_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/sky"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="-16dp" />
<Button
android:id="@+id/button_start"
android:layout_width="175dp"
android:layout_height="76dp"
android:layout_marginTop="200dp"
android:background="@color/black"
android:onClick="firstOne"
android:text="@string/playButtonText"
android:textColor="@color/white"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_lead"
android:layout_width="175dp"
android:layout_height="76dp"
android:layout_marginTop="312dp"
android:background="@color/black"
android:onClick="secondOne"
android:text="@string/leaderboardButtonText"
android:textColor="@color/white"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
那太糟糕了。我在这上面花了很多时间,但答案很简单。始终关心 onCreate
方法,实际上 setContentView(R.layout.);
行。我一直在复制粘贴并尝试创建具有相同布局的 类。