1

I have an image called down.png in the drawables folder, but every time I run the app, it crashes on creation.

This is my MainActivity code:

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

        }
    });


}

This is the XML I am using:

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

And my Logcat output:

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

All the commands that are in comments are where I tried and failed. line 35 (the one that appears in logcat) is the line where the code to make the change is; why is that code causing a crash?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Luis
  • 668
  • 1
  • 6
  • 16
  • need to post to logcat to reach the exact problem – Balvinder Singh Apr 28 '15 at 11:25
  • 1
    please specify line `35` in `Mainactivity` as per `prosis.teste.MainActivity.onCreate(MainActivity.java:35)` – EpicPandaForce Apr 28 '15 at 11:39
  • post your activity's oncreate method – nvinayshetty Apr 28 '15 at 11:45
  • Look at here is posted answer – Amitsharma Apr 28 '15 at 12:06
  • Post ur entire MainActivity code including line numbers. Also there is no problem with ur code, only thing i found in xml would be adding this line **android:layout_alignParentRight="true"** for image view if ur using API 15 and also this **android:layout_alignParentLeft="true"** for button thats all.. The code above worked like a charm for me. – DJphy Apr 28 '15 at 12:47

4 Answers4

1

you should be using

  1. setImageResource(R.drawable.down);

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

instead of setBackgroundResource(R.drawable.img1);.

n1nsa1d00
  • 856
  • 9
  • 23
Anshuman
  • 308
  • 2
  • 6
  • @NobodyNemo ..try these apis ,may be it helps – Anshuman Apr 28 '15 at 11:29
  • @Anshuman To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – n1nsa1d00 Apr 28 '15 at 11:40
0

firstly put your image inside image view,then try to make your button,textView,editText etc transparent(No need to set it dynamically,you can make those changes through your xml).

and go through Android Transparent TextView?

Community
  • 1
  • 1
Miller
  • 744
  • 3
  • 15
  • 39
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);
            }
        });

    }

}

use this code ....I think you forgot to add setContentView() method...

Abdul Rizwan
  • 3,904
  • 32
  • 31
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);
Amitsharma
  • 1,577
  • 1
  • 17
  • 29