imageview中的背景图像导致崩溃

gcuhipw9  于 2021-07-13  发布在  Java
关注(0)|答案(4)|浏览(422)

我有一个图像叫做 down.png 在drawables文件夹中,但每次我运行应用程序时,它都会在创建时崩溃。
这是我的 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中出现的一个)是进行更改的代码所在的行;为什么代码会导致崩溃?

uqxowvwt

uqxowvwt1#

public class MainActivity extends Activity {

    @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.setBackgroundResource(R.drawable.ic_launcher);
            }
        });

    }

}

使用下面的代码…我想你忘了添加setcontentview()方法。。。

w80xi6nr

w80xi6nr2#

你应该使用
setImageResource(R.drawable.down); setImageDrawable(getResources().getDrawable(R.drawable.down)); 而不是 setBackgroundResource(R.drawable.img1); .

a1o7rhls

a1o7rhls3#

首先将图像放在图像视图中,然后尝试使按钮、文本视图、编辑文本等透明(无需动态设置,您可以通过xml进行更改)。
通过android透明文本视图?

ghhkc1vu

ghhkc1vu4#

Use this simple code to draw in background image at ImageView 

 <ImageView 
        android:id="@+id/img"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/ic_launcher"
        />

or 
<ImageView 
          android:id="@+id/img"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_launcher"
        />

or 
 programmatically
 img..setBackgroundResource(R.drawable.ic_launcher);

相关问题