Spotlight
Spotlight

Reputation: 482

Android ToggleButton provides null object reference

I'm developing an Android Wear app, and I have a ToggleButton named fitnessDebugToggle. It's defined in my manifests and can be referenced by findViewById. However, whenever I access it any other way my application crashes.

I'm currently debugging my app on a square Wear device ("Wear Square"), so the following manifest is for a square device.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:orientation="vertical"
    tools:context="me.package.etc.MainActivity"
    tools:deviceIds="wear_square">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dp">

        <TextView
            android:id="@+id/fitnessDebugText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="@string/fitnessDebugText"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <ToggleButton
            android:id="@+id/fitnessDebugToggle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textOff="@string/off"
            android:textOn="@string/on"
            android:layout_below="@+id/fitnessDebugText"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>

</ScrollView>

In my MainActivity class, I'm able to write the following:

public class MainActivity extends Activity {
    ToggleButton fitnessDebugToggle;
    MainActivity mainActivity;

    public MainActivity() {
        mainActivity = this;
    }

    @Override
    public void onCreate(Bundle bundle) {
        // Show view
        super.onCreate(bundle);
        setContentView(R.layout.layout_main);

        // Setup
        fitnessDebugToggle = (ToggleButton) findViewById(R.id.fitnessDebugToggle);
    }
}

However right below the findViewById call, if I put

fitnessDebugToggle.setTransformationMethod(null);

it crashes with part of the stack trace being

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ToggleButton.setTransformationMethod(android.text.method.TransformationMethod)' on a null object reference

at the line I added the setTransformationMethod call.

I have looked at the question Android studio “ Attempt to invoke virtual method on a null object reference” however that deals with a NullPointerException on the findViewById call, not uponfurther calls. My package name has also been changed in this example.


Regarding comments:

Upvotes: 0

Views: 896

Answers (3)

pehg
pehg

Reputation: 1

I experienced a problem with findViewById returning null for all the elements of my layout. In the end, it was solved by just rebuilding the project. Very annoying problem.

Upvotes: 0

Dharvik shah
Dharvik shah

Reputation: 1989

if you rename the xml file the try this option of android studio :

File -> Invalidate Catches/Restart

Then even problem occures the change ToggleButton id in xml and check is there java file gives error ? if yes then the id is perfectly integrated and updated in R.java.

Then change onCreate methos as :

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
    }

Upvotes: 0

Charuka Silva
Charuka Silva

Reputation: 13153

you can use null only in one scenario like this try if else

if(checked){
            button.setTransformationMethod(null);
        }else {
            button.setTransformationMethod(new PasswordTransformationMethod());

        }

Upvotes: 1

Related Questions