0

It is not a clean and rebuild issue AT ALL! Make module will not resolve this issue either. This is the only FlotingActionButton I'm using in my code. One solution said this only occurs with an anchor parameter but as you can see I am not using one so I dont know what to make of it.

    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    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:id="@+id/activity_main"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/coverpic"
    tools:context="com.android.nohiccupsbeta.MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/effects_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="top|end"
            android:layout_marginTop="400dp"
            android:layout_marginRight="15dp"
            android:src="@drawable/ic_book"
            app:backgroundTint="@color/primary_color" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/take_test"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:backgroundTint="@color/primary_color"
            android:src="@drawable/ic_handcuffs" />

    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

This is the JAVA code. I tried importing the Coordinator Layout but that is not possible. I also tried declaring the fab var differently just to get the same error :

    package com.android.nohiccupsbeta;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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


        FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, pageone.class));
            }
        });


        FloatingActionButton fab2 = (FloatingActionButton)findViewById(R.id.fab2);
        fab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, BacTest.class));
            }
        });
    }

}

1 Answer 1

2

Your second FloatingActionButton does not have an id, and the one you are trying to cast into a FAB is the CoordinatorLayout.

So, you should set an id in your layout for your fabs like this

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    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:id="@+id/activity_main"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/coverpic">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/effects_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="top|end"
            android:layout_marginTop="400dp"
            android:layout_marginRight="15dp"
            android:src="@drawable/ic_book"
            app:backgroundTint="@color/primary_color" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/take_test"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:backgroundTint="@color/primary_color"
            android:src="@drawable/ic_handcuffs" />

    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

and your fabs to match the new ids like this

FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
FloatingActionButton fab2 = (FloatingActionButton)findViewById(R.id.fab2);
Sign up to request clarification or add additional context in comments.

8 Comments

It has an i.d . I gave it a try anyway and put the id under the floating action button and gave the coordinator layout a different id. I got the same error
You have the same issue with the first fab. Updating my answer with working code
OK I changed the code now I'm getting a different error message in the log java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.android.nohiccupsbeta.MainActivity.onCreate(MainActivity.java:18)
do you mind updating your code on the question? for both the activity and layout
So, your new code is working for me . As I can see there should not be problems. Are you sure you are referencing the correct layout file ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.