OnClick导致活动关闭



活动的开始导致我的应用程序崩溃。这不是应用程序上的第一个OnClick。

不工作

public void  start_motora(View view){
Intent intent = new Intent(getApplicationContext(), MotorActivity.class);
startActivity(intent);
}

public void  start_app(View view){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);  
startActivity(intent);
}

不工作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"
android:orientation="vertical"
android:background="@android:color/white">
<ImageButton
android:onClick="start_motora"
android:id="@+id/motor"
android:layout_width="0dp"
android:layout_height="54dp"
android:layout_marginStart="44dp"
android:layout_marginTop="32dp"
android:background="@drawable/purplebutton"
android:src="@drawable/ic_face_white"
app:layout_constraintEnd_toStartOf="@+id/alfabeto"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text11" />
</androidx.constraintlayout.widget.ConstraintLayout>

使用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"
tools:context=".HomeActivity"
android:background="#ffd500">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/logo_sam"
android:onClick="start_app"
android:outlineAmbientShadowColor="@color/yellow"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="?android:attr/borderlessButtonStyle"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<<p>

控制台错误/strong>错误。我已经改变了onClick名称和活动名称,但这是同样的问题。

2021-11-03 14:34:03.659 29506-29506/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 29506
java.lang.IllegalStateException: Could not find method teste(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'button2'
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:447)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:405)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

请帮助。错误没有链接到我的代码中的一些错误,我很困惑

您必须在您的清单中注册您的MotorActivity活动。

<activity android:name=".MotorActivity" />
  1. 我可以看到上面的代码,如果你正在使用活动,那么你不需要需要使用getApplicationContext()来代替你应该用这个。ActivityName或只有这个因为上下文是为特定的原因定义的,比如例如,如果你正在使用片段,那么你可以使用上下文从onAttach和Apdater,你可以使用应用程序上下文。

  2. 请分享你的错误没有错误,我不知道什么是你的应用程序崩溃的原因。

作为参考检查这个答案:Android按钮onClick改变活动导致应用程序崩溃

和上下文描述:- https://medium.com/@banmarkovic/what-iscontext-inandroidand -which-one- shoul-youruse -e1a8c6529652

最新更新