gpt4 book ai didi

java - ImageView 中的背景图片导致崩溃

转载 作者:行者123 更新时间:2023-11-30 02:12:16 25 4
gpt4 key购买 nike

我在 drawables 文件夹中有一个名为 down.png 的图像,但每次运行该应用程序时,它都会在创建时崩溃。

这是我的 MainActivity 代码:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView sss = (ImageView) findViewById(R.id.sss);
Button ddd = (Button) findViewById(R.id.button);
ddd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//sss.setImageResource(R.drawable.down);
sss.setBackgroundResource(R.drawable.down);
//sss.setImageDrawable(getResources().getDrawable(R.drawable.down));

}
});


}

这是我正在使用的 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/activity_main_layout"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ImageView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="@+id/sss"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_below="@+id/sss"
android:layout_alignParentStart="true"
android:layout_marginTop="111dp" />
</RelativeLayout>

还有我的 Logcat 输出:

04-28 07:11:09.637    1359-1359/prosis.teste D/AndroidRuntime﹕ Shutting down VM
04-28 07:11:09.637 1359-1359/prosis.teste W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3a99ba8)
04-28 07:11:09.687 1359-1359/prosis.teste E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: prosis.teste, PID: 1359
java.lang.RuntimeException: Unable to start activity ComponentInfo{prosis.teste/prosis.teste.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at prosis.teste.MainActivity.onCreate(MainActivity.java:35)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
04-28 07:11:14.327 1359-1359/prosis.teste I/Process﹕ Sending signal. PID: 1359 SIG: 9

评论中的所有命令都是我尝试过但失败的地方。 line 35(出现在 logcat 中的那一行)是进行更改的代码所在的行;为什么该代码会导致崩溃?

最佳答案

你应该使用

  1. setImageResource(R.drawable.down);

  2. setImageDrawable(getResources().getDrawable(R.drawable.down));

代替 setBackgroundResource(R.drawable.img1);

关于java - ImageView 中的背景图片导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29918006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com