Home > Net >  ImageView providing Null Returning Crashing Program
ImageView providing Null Returning Crashing Program

Time:02-27

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>

CodePudding user response:

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>

CodePudding user response:

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

  • Related