ImageView提供空返回崩溃程序

发布于 2025-01-10 03:34:56 字数 2652 浏览 3 评论 0原文

Android Studio 新手。在这里阅读了很多关于 ImageView 的 null 返回的回复,但没有一个能解决我的问题。我得到一个空返回,结果该程序崩溃了。感谢任何帮助

我不确定是否是因为在函数内调用图像视图并且需要其自己的函数具有不同的覆盖?我尝试这样做,但它不起作用

MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    public boolean timeDelt = false;
    public long timeStart = 99999;
    public long timeEnd = 99999;
    public int buttonCnt = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            long buttonTime = System.currentTimeMillis();
            buttonCnt = buttonCnt + 1;
            if (timeDelt == false) {
                timeStart = System.currentTimeMillis();
                timeDelt = true;
            }
            if (buttonTime - timeStart < 5000) {
                    buttonCnt = 0; // reset count
                    ImageView firstImage = (ImageView) findViewById(R.id.firstimage);
                    firstImage.setImageResource(R.drawable.norway);
                }
            long a = System.currentTimeMillis();
            Log.d("ADebugTag", "Value: " + Long.toString(a));
        }
        return true;
    }

}

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:id="@+id/firstimage"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
    
        tools:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.388"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.353" />

</androidx.constraintlayout.widget.ConstraintLayout>

New to android studio. Read a lot of responses on here regarding null returns for ImageView but none have solved my issue. I am getting a null return and that program is crashing as a result. Any help is appreciated

I am not sure if it is because the imageview is being called within a function and needs its own function with a different override? I attempted that and it did not work either

MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    public boolean timeDelt = false;
    public long timeStart = 99999;
    public long timeEnd = 99999;
    public int buttonCnt = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            long buttonTime = System.currentTimeMillis();
            buttonCnt = buttonCnt + 1;
            if (timeDelt == false) {
                timeStart = System.currentTimeMillis();
                timeDelt = true;
            }
            if (buttonTime - timeStart < 5000) {
                    buttonCnt = 0; // reset count
                    ImageView firstImage = (ImageView) findViewById(R.id.firstimage);
                    firstImage.setImageResource(R.drawable.norway);
                }
            long a = System.currentTimeMillis();
            Log.d("ADebugTag", "Value: " + Long.toString(a));
        }
        return true;
    }

}

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:id="@+id/firstimage"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
    
        tools:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.388"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.353" />

</androidx.constraintlayout.widget.ConstraintLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙气飘飘 2025-01-17 03:34:56

用这个替换你的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="250dp"
    android:layout_height="250dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"

    tools:context=".MainActivity">

<ImageView
    android:id="@+id/firstimage"
    android:layout_width="50dp"
    android:layout_height="50dp"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.423"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.642" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.388"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.353" />

</androidx.constraintlayout.widget.ConstraintLayout>

replace you xml with this

<?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="250dp"
    android:layout_height="250dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"

    tools:context=".MainActivity">

<ImageView
    android:id="@+id/firstimage"
    android:layout_width="50dp"
    android:layout_height="50dp"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.423"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.642" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.388"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.353" />

</androidx.constraintlayout.widget.ConstraintLayout>
你是我的挚爱i 2025-01-17 03:34:56

在 XML 上创建一个 Imageview 并为该视图设置 id 并在关联的活动或片段中调用该视图

create an Imageview on your XML and set id for the view and call the view in associated activity or fragment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文