我在 Android Studio 中使用导航 fragment 模板。我在 fragment_main.xml 中添加了一个 ImageView,如下所示
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/at"
android:layout_below="@+id/section_label"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="80dp" />
而java代码如下 在 OnCreate 方法中。
img = (ImageView)findViewById(R.id.imageView);
在章节附加方法中。
img.setImageResource(R.drawable.at);
每次我运行这段代码时它都会崩溃。如果图像源是用 XML 设置的,它工作得非常好,但是当我使用 java 文件来更改图像源时。它崩溃说不幸的应用程序已停止。 必要时的图像细节 图片大小:32.3 kb 尺寸:450*432像素
最佳答案
查看android Console Log,是否可以在那里找到错误源
崩溃报告是否来自某些版本的 android,并且您只有合格文件夹中的某些资源,即“drawable-mdpi”而不仅仅是“drawable”?阅读此 http://developer.android.com/guide/topics/resources/providing-resources.html
如果您使用的是 Eclipse,您是否尝试过清理和重建您的项目?这解决了我遇到的类似问题。
关于java - 图像使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31351100/