AndroidStudio-两个相同(相同的源代码)应用程序-一个有效,第二个无效-应用程序启动后关闭

问题描述 投票:-1回答:2

设置了新的开发环境后,我只是想开始使用,我有两个简单的Android应用程序,只有一个简单,普通的ImageView。它们都有相同的源代码(标题不同),而且令人惊奇的是第一个有效,第二个却没有。

蜂鸣开始后,第二个应用程序立即关闭。第一个工作并显示图像。作为问题,我可以识别ImageView,如果删除该元素,则该应用程序可以正常启动。

也许有人可以给我小费或可以帮忙。非常感谢!

AndroidStudio 3.6-测试设备:华为Android 6.0

MainActivity.java:


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    public void fade (View view){
        ImageView imageView = findViewById(R.id.imageView);
        imageView.animate().alpha(0).setDuration(2000);
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml:

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

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:onClick="fade"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/bart" />
</androidx.constraintlayout.widget.ConstraintLayout>

Logcat:

05-12 23:29:44.274 14433-14433/? I/art: Late-enabling -Xcheck:jni
05-12 23:29:44.356 14433-14450/com.example.testdrei E/HAL: load: id=gralloc != hmi->id=gralloc
05-12 23:29:44.373 14433-14433/com.example.testdrei W/System: ClassLoader referenced unknown path: /data/app/com.example.testdrei-2/lib/arm64
05-12 23:29:44.401 14433-14433/com.example.testdrei I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl
05-12 23:29:44.412 14433-14433/com.example.testdrei W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-12 23:29:44.477 14433-14433/com.example.testdrei I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
05-12 23:29:44.477 14433-14433/com.example.testdrei I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
05-12 23:29:44.559 14433-14433/com.example.testdrei W/ResourceType: Failure getting entry for 0x7f060061 (t=5 e=97) (error -75)
05-12 23:29:44.559 14433-14433/com.example.testdrei W/ResourceType: Failure getting entry for 0x7f060061 (t=5 e=97) (error -75)
05-12 23:29:44.592 14433-14433/com.example.testdrei I/Process: Sending signal. PID: 14433 SIG: 9

java android android-studio imageview
2个回答
0
投票

ID为R.id.imageView3的ImageView不是您的布局的一部分。因此,以下行将导致异常

ImageView imageView = findViewById(R.id.imageView3);

使用正确的ID进行更改

ImageView imageView = findViewById(R.id.imageView); 

0
投票

ImageView imageView = findViewById(R.id.imageView3);//错误,因为main.xml文件中imageView的ID是'imageView'而不是'imageView3'

© www.soinside.com 2019 - 2024. All rights reserved.