0

when i try to go to the activity where the animation is, it always crash. and my log cat is here and my other files are here.

Im trying to creating an animation through DrawableAnimation here but i don't know why there is an error when i set the backround resource

LogCat

02-03 18:52:30.323 4831-4831/com.secsys.gagacamaso.gagacamaso E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.secsys.gagacamaso.gagacamaso, PID: 4831
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.secsys.gagacamaso.gagacamaso/com.secsys.gagacamaso.gagacamaso.Menu}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference
                                                                                at com.secsys.gagacamaso.gagacamaso.Menu.onCreate(Menu.java:31)
                                                                                at android.app.Activity.performCreate(Activity.java:6237)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417) 

Menu.java

public class Menu extends Activity {

ImageButton enable;
ImageView rm1;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    setViews();
    enable = (ImageButton)findViewById(R.id.enable);
    lock();


    rm1.setBackgroundResource(R.drawable.rm_animation);

}

public void setViews(){
    ImageView rm1 = (ImageView)findViewById(R.id.rm1);
}

private void lock(){
    enable.setTag(1);
    enable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final int state =(Integer) v.getTag();
            if(state == 1) {
                Toast.makeText(Menu.this, "ENABLED!", Toast.LENGTH_SHORT).show();
                enable.setBackgroundResource(R.drawable.disable);
                AnimationDrawable rmanim = (AnimationDrawable) rm1.getBackground();
                rmanim.start();
                v.setTag(0);
            } else {
                Toast.makeText(Menu.this, "DISABLED!", Toast.LENGTH_SHORT).show();
                enable.setBackgroundResource(R.drawable.enable);
                v.setTag(1); //pause
            }
        }
    });
}

rm_animation.xml (inside drawable folder)

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/rm1" android:duration="150"/>
    <item android:drawable="@drawable/rm1glow1" android:duration="150"/>
    <item android:drawable="@drawable/rm1glow2" android:duration="150"/>
    <item android:drawable="@drawable/rm1glow3" android:duration="150"/>

</animation-list>

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<GridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="100dp"
    android:layout_centerHorizontal="true"
    android:id="@+id/gridLayout2">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/rm1"
        android:layout_row="0"
        android:layout_column="0"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/rm4"
        android:layout_row="0"
        android:layout_column="2"
        android:background="@drawable/rm1"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/rm2"
        android:layout_row="1"
        android:layout_column="0"
        android:background="@drawable/rm1"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/rm5"
        android:layout_row="1"
        android:layout_column="2"
        android:background="@drawable/rm1"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/rm3"
        android:layout_row="2"
        android:layout_column="0"
        android:background="@drawable/rm1"/>
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/lrm1"
        android:layout_row="0"
        android:layout_column="1"
        android:layout_gravity="fill_vertical"
        android:layout_rowSpan="3"
        android:background="@drawable/rm2"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/lrm2"
        android:layout_row="2"
        android:layout_column="2"
        android:background="@drawable/rm2a"/>

</GridLayout>

<ImageButton
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:id="@+id/enable"
    android:layout_below="@+id/gridLayout2"
    android:layout_centerHorizontal="true"
    android:background="@drawable/enable"/>
</RelativeLayout>
Ralph
  • 435
  • 1
  • 5
  • 13
  • 3
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – David Medenjak Feb 03 '16 at 19:13
  • i can't see the connection – Ralph Feb 03 '16 at 19:19
  • Animation files are not under drawable. They are in anim folder.And you have to create it. And also when you have created rm1 imageview variable already then why are you creating it again in setView method – Vivek Mishra Feb 03 '16 at 19:27
  • @VivekMishra , I created an anim folder and now when i set the backgroudReasource of the image view rm1, it says "Expected of resource type drawable".. yea i spotted thaat error sorry and thank you! – Ralph Feb 03 '16 at 19:47
  • You can't set animation as background – Vivek Mishra Feb 03 '16 at 19:48
  • it's what i saw when i searched on how to use android drawable, and i have seen examples too that they setBackgroundResource to image views – Ralph Feb 03 '16 at 19:52

3 Answers3

0

boss this null pointer exception is because of

  public void setViews(){
    ImageView rm1 = (ImageView)findViewById(R.id.rm1);
  }

in this case the reference to rm1 used for setting background will be always null.

try doing

public void setViews(){
     rm1 = (ImageView)findViewById(R.id.rm1);
 }
0

Remove ImageView from setViews() method as it will create a local variable and initialize it not the top level rm1.

public void setViews(){
     rm1 = (ImageView)findViewById(R.id.rm1);
}
Rohit Sharma
  • 2,017
  • 1
  • 20
  • 22
0

You should first create Animation from drawable xml file and then call it to your ImageView.

Animation animation = AnimationUtils.loadAnimation(this, R.anim.rm_animation);
imageView.startAnimation(animation);

Just create a new anim folder and create there new anim file, and copy everything from rm_animation.xml you have in drawable folder.

And remove from setView() ImageView. You should have only imageView = (ImageView) findViewById(R.id.imageView); if you have already initialize variable outside the method.

Dusan Dimitrijevic
  • 3,169
  • 6
  • 22
  • 46