Carlton Jr. Carlton Jr. - 3 months ago 14
Java Question

Use animation on button click?

I am trying to use this cool animation called SmallBang
on my Image button. Here is how my code looks for now:

import xyz.hanks.library.SmallBang;

public class MainActivity extends AppCompatActivity {

private SmallBang smallBang;
private ImageButton imageButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smallBang = SmallBang.attach2Window(this);
smallBang.bang((ImageButton) findViewById(R.id.imageButton));
}

public void onSettingsButtonClick(View view) {
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
startActivity(intent);
finish();

}
}


I want to use that animation and then change to another activity. When i run this code I get this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference


If i remove the smallbang animation, the button and activity change works just fine. What could I do to get the animation working? I've added the library to the Gradle file.

Answer

Working Code

public class MainActivity extends Activity {

    private SmallBang mSmallBang;
    private ImageView ImageButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSmallBang = SmallBang.attach2Window(this);

        ImageButton = (ImageView) findViewById(R.id.imageButton);
        ImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mSmallBang.bang(v);
                mSmallBang.setmListener(new SmallBangListener() {
                    @Override
                    public void onAnimationStart() {
                    }

                    @Override
                    public void onAnimationEnd() {

                    }
                });
            }
        });
    }
}

activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/image_1">


    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@mipmap/ic_launcher" />
</RelativeLayout>

Update your code modification:

public class MainActivity extends Activity {

    private SmallBang mSmallBang;
    private ImageView ImageButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSmallBang = SmallBang.attach2Window(this);
        ImageButton = (ImageView) findViewById(R.id.imageButton);
    }

    public void onSettingsButtonClick(View view) {
        mSmallBang.bang(view);
        mSmallBang.setmListener(new SmallBangListener() {
            @Override
            public void onAnimationStart() {
            }

            @Override
            public void onAnimationEnd() {
                Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}
Comments