ImageView提供空返回崩溃程序
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用这个替换你的xml
replace you xml with this
在 XML 上创建一个 Imageview 并为该视图设置 id 并在关联的活动或片段中调用该视图
create an Imageview on your XML and set id for the view and call the view in associated activity or fragment