ceryle / RadioRealButton

A custom radio button for Android API 12+
Apache License 2.0
259 stars 73 forks source link

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference #52

Open Ashley-Deans opened 4 years ago

Ashley-Deans commented 4 years ago

I tried using the example code at the bottom of the page to test it out. But I'm getting this error whenever I put the java code in. Otherwise the XML part works fine. But the minute I put the java code the function setOnClickedButtonListener and OnPositionChangedListener just causes the app to crash.

Main Activity

package com.masslabs.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toast;

import co.ceryle.radiorealbutton.RadioRealButton;
import co.ceryle.radiorealbutton.RadioRealButtonGroup;

public class MainActivity extends AppCompatActivity {

    final RadioRealButton button1 = (RadioRealButton) findViewById(R.id.button1);
    final RadioRealButton button2 = (RadioRealButton) findViewById(R.id.button2);

    RadioRealButtonGroup group = (RadioRealButtonGroup) findViewById(R.id.group);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

// onClickButton listener detects any click performed on buttons by touch
        group.setOnClickedButtonListener(new RadioRealButtonGroup.OnClickedButtonListener() {
            @Override
            public void onClickedButton(RadioRealButton button, int position) {
                Toast.makeText(MainActivity.this, "Clicked! Butten: " + button.getText(), Toast.LENGTH_SHORT).show();
            }
        });

        // onPositionChanged listener detects if there is any change in position
        group.setOnPositionChangedListener(new RadioRealButtonGroup.OnPositionChangedListener() {
            @Override
            public void onPositionChanged(RadioRealButton button, int currentPosition, int lastPosition) {
                Toast.makeText(MainActivity.this, "Button Changed! Text: " + button.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

XML File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <co.ceryle.radiorealbutton.RadioRealButtonGroup
        android:id="@+id/group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:rrbg_animateDrawables_enter="overshoot"
        app:rrbg_animateTexts_enter="overshoot"
        app:rrbg_dividerColor="@android:color/black"
        app:rrbg_dividerSize="1dp"
        app:rrbg_radius="10dp"
        app:rrbg_selectorColor="@android:color/holo_red_dark"
        app:rrbg_selectorSize="6dp">

        <co.ceryle.radiorealbutton.RadioRealButton
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:rrb_drawable="@mipmap/ic_launcher"
            app:rrb_drawableHeight="36dp"
            app:rrb_drawablePadding="8dp"
            app:rrb_drawableWidth="36dp"
            app:rrb_ripple="true"
            app:rrb_rippleColor="@android:color/black"
            app:rrb_text="Button 1"
            app:rrb_textColor="@android:color/black"/>

        <co.ceryle.radiorealbutton.RadioRealButton
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:rrb_drawable="@mipmap/ic_launcher"
            app:rrb_drawableGravity="right"
            app:rrb_drawableHeight="36dp"
            app:rrb_drawablePadding="8dp"
            app:rrb_drawableWidth="36dp"
            app:rrb_ripple="true"
            app:rrb_rippleColor="@android:color/black"
            app:rrb_text="Button 2"
            app:rrb_textColor="@android:color/black"/>
    </co.ceryle.radiorealbutton.RadioRealButtonGroup>

</LinearLayout>