Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我尝试使用图像按钮转到第二个活动时,应用程序崩溃_Java_Android_View_Imagebutton - Fatal编程技术网

Java 当我尝试使用图像按钮转到第二个活动时,应用程序崩溃

Java 当我尝试使用图像按钮转到第二个活动时,应用程序崩溃,java,android,view,imagebutton,Java,Android,View,Imagebutton,有一个简单的类,当我试图进入活动ColgarAWalter时,我的应用程序崩溃,出现了一个致命的异常MAIN。我看到了其他用户的一些解决方案,并修改了一些东西。 我应该在XML中调用onClickListener,因为我觉得它对我来说更方便,并且不需要在主类上编写太多代码 主要活动 这里是我在Intent vista.getContext()上看到的主要活动,但我认为这两件事是一样的 package com.example.practica_4_walter_white; import and

有一个简单的类,当我试图进入活动ColgarAWalter时,我的应用程序崩溃,出现了一个致命的异常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);
    }
}

科尔加·沃尔特

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);
    }
}


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) 

活动\u 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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/encabezado"
        android:layout_width="207dp"
        android:layout_height="47dp"
        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_width="213dp"
        android:layout_height="211dp"
        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_width="106dp"
        android:layout_height="116dp"
        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 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=".ColgarAWalter">

    <ImageView
        android:id="@+id/walter_2"
        android:layout_width="207dp"
        android:layout_height="192dp"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="322dp"
        android:layout_height="84dp"
        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>
我把清单放在这里,这是我第一次知道它存在,所以我不编辑任何东西

清单

<?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>

代表OP发布

在我的例子中,解决方法是我不编辑清单文件,这样系统就不会识别我的第二个活动,添加它是有效的

<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>


您是否在AndroidManifest中声明了此活动?嗯,我不知道什么是AndroidManifest,我从未使用过它。这不在我发布的代码上吗@IntelliJAmiyaread起初@IntelliJAmiya好的,我读了,我明白了,但我不知道我应该在这个文件上更改什么。我在代码上介绍了清单Upsidek现在起作用了感谢所有人的真实@IntelliJAmiya我不能说出你的名字,但也谢谢你Makarand和inlacou现在我永远不会忘记修改清单XDD