当我尝试使用图像按钮进入第二个活动时应用程序崩溃
Posted
技术标签:
【中文标题】当我尝试使用图像按钮进入第二个活动时应用程序崩溃【英文标题】:App crash when i tried to go to the second activity with a image button 【发布时间】:2019-09-24 10:23:49 【问题描述】:有一个简单的课程,当我尝试参加活动 ColgarAWalter 时,我的应用程序崩溃并出现 FATAL EXCEPTION MAIN。我看到了其他用户的一些解决方案并修改了一些东西。 我应该在 XML 中调用 onClickListener,因为我觉得它对我来说更舒服,不需要在主类上写太多代码
主要活动 在主要活动中,我使用 Intent vista.getContext(),但我认为两者相同
package com.example.practica_4_walter_white;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void llamando(View vista)
Intent intent=new Intent(MainActivity.this,ColgarAWalter.class);
startActivity(intent);
COLGAR A WALTER
package com.example.practica_4_walter_white;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class ColgarAWalter extends AppCompatActivity
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.llamando_al_peligro);
public void colgar(View vista)
Intent intent=new Intent(ColgarAWalter.this,MainActivity.class);
startActivity(intent);
activity_main.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_
android:layout_
tools:context=".MainActivity">
<TextView
android:id="@+id/encabezado"
android:layout_
android:layout_
android:text="Puedes llamar a walter white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.096" />
<ImageView
android:id="@+id/walter"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.779"
app:srcCompat="@drawable/walterwhite" />
<ImageButton
android:src="@drawable/llamar"
android:layout_
android:layout_
android:onClick="llamando"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.265"
app:srcCompat="@drawable/llamar" />
</androidx.constraintlayout.widget.ConstraintLayout>
在 XML 上,我得到了 +id 以供参考 ImageButtons,但我看到有人说最好放 src 和 @drawable/llamar 或类似的东西 llamando_al_peligro.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_
android:layout_
tools:context=".ColgarAWalter">
<ImageView
android:id="@+id/walter_2"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.801"
app:srcCompat="@drawable/walterwhite2" />
<ImageButton
android:src="@drawable/colgar"
android:layout_
android:layout_
android:layout_marginTop="220dp"
android:onClick="colgar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/colgar" />
<TextView
android:id="@+id/encabezado2"
android:layout_
android:layout_
android:text="Calling the Danger Skyler..."
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"
app:layout_constraintVertical_bias="0.072" />
</androidx.constraintlayout.widget.ConstraintLayout>
调试 EROOR
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.practica_4_walter_white, PID: 9015
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6608)
at android.view.View.performClickInternal(View.java:6585)
at android.view.View.access$3100(View.java:785)
at android.view.View$PerformClick.run(View.java:25921)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6608)
at android.view.View.performClickInternal(View.java:6585)
at android.view.View.access$3100(View.java:785)
at android.view.View$PerformClick.run(View.java:25921)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class com.example.practica_4_walter_white/com.example.practica_4_walter_white.ColgarAWalter; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2014)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1677)
at android.app.Activity.startActivityForResult(Activity.java:4651)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
at android.app.Activity.startActivityForResult(Activity.java:4609)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
at android.app.Activity.startActivity(Activity.java:4970)
at android.app.Activity.startActivity(Activity.java:4938)
at com.example.practica_4_walter_white.MainActivity.llamando(MainActivity.java:19)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6608)
at android.view.View.performClickInternal(View.java:6585)
at android.view.View.access$3100(View.java:785)
at android.view.View$PerformClick.run(View.java:25921)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
我第一次知道它存在所以我把清单放在这里所以我不编辑任何东西
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practica_4_walter_white">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
【问题讨论】:
have you declared this activity in your AndroidManifest
??
嗯,我不知道 android manifest 是什么,我从来没用过。那不是我发布的代码吗? @IntelliJAmiya
初读developer.android.com/guide/topics/manifest/manifest-intro
@IntelliJAmiya 好的,我读了,我明白了,但我不知道我应该在这个文件上改变什么。我在代码上面介绍清单
好的,现在可以使用了,谢谢大家的真实@IntelliJAmiya 我不能说出你的名字,但也谢谢你 Makarand 和 inlacou 现在我永远不会忘记修改清单 XDD
【参考方案1】:
在我的情况下,解决方案是我不编辑 Manifest 文件,因此系统无法识别我的第二个活动,添加它有效。
<activity android:name=".ColgarAWalter"></activity>
我现在正确放置了所有清单代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practica_4_walter_white">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ColgarAWalter"></activity>
</application>
</manifest>
代表提问者添加。
【讨论】:
以上是关于当我尝试使用图像按钮进入第二个活动时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章