ImageView中的背景图像导致崩溃

时间:2015-04-28 11:18:54

标签: java android android-studio imageview

我在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中的那个)是进行更改的代码所在的行; 为什么那段代码导致崩溃?

4 个答案:

答案 0 :(得分:1)

你应该使用

  1. setImageResource(R.drawable.down);

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

  3. 而不是setBackgroundResource(R.drawable.img1);

答案 1 :(得分:0)

首先将图像放入图像视图中,然后尝试使按钮,textView,editText等透明(无需动态设置,可以通过xml进行更改)。

然后通过 Android Transparent TextView?

答案 2 :(得分:0)

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()方法......

答案 3 :(得分:0)

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);