Solucionado (ver solução)
Solucionado
(ver solução)
5
respostas

Erro: java.lang.NullPointerException

Estou com um problema, criei um Circular Reveal Menu, porém não estou conseguindo mostrá-lo! Segue meu código java:

package com.example.myclinic.myclinic;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import io.codetail.animation.SupportAnimator;
import io.codetail.animation.ViewAnimationUtils;

public class PrincipalActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

    private FrameLayout mRevealView;
    private boolean hidden = true;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        mRevealView = (FrameLayout) findViewById(R.id.reveal_items);

    }

    @Override
    public void onClick(View v) {
        hideRevealView();
        switch (v.getId()) {

            case R.id.gallery:

                break;
            case R.id.audio:

                break;
            case R.id.video:

                break;
            case R.id.camera:

                break;
            case R.id.location:

                break;
            case R.id.contacts:

                break;
        }
    }

    private void hideRevealView() {
        if (mRevealView.getVisibility() == View.VISIBLE) {
            mRevealView.setVisibility(View.GONE);
            hidden = true;
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.principal, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.menu_rapido:

                mRevealView.setVisibility(View.INVISIBLE);
                int cx = (mRevealView.getLeft() + mRevealView.getRight());
                int cy = mRevealView.getTop();
                int radius = Math.max(mRevealView.getWidth(), mRevealView.getHeight());

                //Below Android LOLIPOP Version
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    SupportAnimator animator =
                            ViewAnimationUtils.createCircularReveal(mRevealView, cx, cy, 0, radius);
                    animator.setInterpolator(new AccelerateDecelerateInterpolator());
                    animator.setDuration(700);

                    SupportAnimator animator_reverse = animator.reverse();

                    if (hidden) {
                        mRevealView.setVisibility(View.VISIBLE);
                        animator.start();
                        hidden = false;
                    } else {
                        animator_reverse.addListener(new SupportAnimator.AnimatorListener() {
                            @Override
                            public void onAnimationStart() {

                            }

                            @Override
                            public void onAnimationEnd() {
                                mRevealView.setVisibility(View.INVISIBLE);
                                hidden = true;

                            }

                            @Override
                            public void onAnimationCancel() {

                            }

                            @Override
                            public void onAnimationRepeat() {

                            }
                        });
                        animator_reverse.start();
                    }
                }
                // Android LOLIPOP And ABOVE Version
                else {
                    if (hidden) {
                        Animator anim = android.view.ViewAnimationUtils.createCircularReveal(mRevealView, cx, cy, 0, radius);
                        mRevealView.setVisibility(View.VISIBLE);
                        anim.start();
                        hidden = false;
                    } else {
                        Animator anim = android.view.ViewAnimationUtils.createCircularReveal(mRevealView, cx, cy, radius, 0);
                        anim.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                super.onAnimationEnd(animation);
                                mRevealView.setVisibility(View.INVISIBLE);
                                hidden = true;
                            }
                        });
                        anim.start();
                    }
                }
                return true;

            case android.R.id.home:
                supportFinishAfterTransition();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.meus_locais) {
            Intent ml = new Intent(PrincipalActivity.this, MeusLocaisActivity.class);
            startActivity(ml);
        } else if (id == R.id.minhas_notas) {
            Intent mn = new Intent(PrincipalActivity.this, FormularioNotasActivity.class);
            startActivity(mn);
        } else if (id == R.id.agenda) {
            Intent lo = new Intent(PrincipalActivity.this, LocalizacoesActivity.class);
            startActivity(lo);
        } else if (id == R.id.preferencias) {
            Intent pref = new Intent(PrincipalActivity.this, PreferenciasActivity.class);
            startActivity(pref);
        } else if (id == R.id.contato) {

        } else if (id == R.id.ajuda) {

        } else if (id == R.id.sobre) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Segue o LOG do erro:

example.myclinic.myclinic E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               java.lang.NullPointerException
                                                                                   at com.example.myclinic.myclinic.PrincipalActivity.onOptionsItemSelected(PrincipalActivity.java:109)
                                                                                   at android.app.Activity.onMenuItemSelected(Activity.java:2640)
                                                                                   at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
                                                                                   at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)
                                                                                   at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
                                                                                   at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
                                                                                   at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
                                                                                   at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:169)
                                                                                   at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
                                                                                   at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
                                                                                   at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                                                                                   at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
                                                                                   at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
                                                                                   at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
                                                                                   at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
                                                                                   at android.view.View.performClick(View.java:4475)
                                                                                   at android.view.View$PerformClick.run(View.java:18786)
                                                                                   at android.os.Handler.handleCallback(Handler.java:730)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                   at android.os.Looper.loop(Looper.java:176)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                                   at dalvik.system.NativeStart.main(Native Method)
08-16 10:00:24.159 13657-13657/com.example.myclinic.myclinic I/Process: Sending signal. PID: 13657 SIG: 9

Se puderem me ajudar, ficarei muito grato!

5 respostas

Olá Willian,

Aparentemente o atributo mRevealView está nulo quando a linha 109 está sendo executada. Isso pode estar acontecendo porque esta busca não está encontrando o seu FrameLayout:

mRevealView = (FrameLayout) findViewById(R.id.reveal_items);

Eu começaria verificando se no layout activity_principal.xml tem um FrameLayout com id reveal_items. Dá uma olhadinha nesse arquivo, aproveita e posta aqui prá gente poder ajudar também.

Olá Jeferson, alterei o FrameLayout colocando o id dele como "reveal_items" como você explicou, ele deu outro erro! Segue o activity_principal.xml e o log do erro:

activity_principal.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    tools:context=".PrincipalActivity">

    <include
        layout="@layout/app_bar_principal"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:id="@+id/reveal_items">

        <io.codetail.widget.RevealFrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include layout="@layout/menu_attach" />

        </io.codetail.widget.RevealFrameLayout>

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_principal"
        app:menu="@menu/activity_principal_drawer"
        android:background="#484848"
        app:itemTextColor="#f1bd14"
        app:itemIconTint="#FFFFFF">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Suporte"
            android:textColor="#FFFFFF"
            android:layout_marginTop="335dp"
            android:layout_marginLeft="16dp"
            android:textSize="17sp"/>

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

</android.support.v4.widget.DrawerLayout>

menu_attach.xml para abrir o circular reveal layout

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

log do erro

08-17 13:24:53.388 5975-5975/com.example.myclinic.myclinic E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             java.lang.IllegalArgumentException: View must be inside RevealFrameLayout or RevealLinearLayout.
                                                                                 at io.codetail.animation.ViewAnimationUtils.createCircularReveal(ViewAnimationUtils.java:75)
                                                                                 at io.codetail.animation.ViewAnimationUtils.createCircularReveal(ViewAnimationUtils.java:46)
                                                                                 at com.example.myclinic.myclinic.PrincipalActivity.onOptionsItemSelected(PrincipalActivity.java:93)
                                                                                 at android.app.Activity.onMenuItemSelected(Activity.java:2640)
                                                                                 at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
                                                                                 at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)
                                                                                 at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
                                                                                 at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
                                                                                 at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
                                                                                 at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:169)
                                                                                 at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
                                                                                 at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
                                                                                 at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                                                                                 at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
                                                                                 at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
                                                                                 at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
                                                                                 at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
                                                                                 at android.view.View.performClick(View.java:4475)
                                                                                 at android.view.View$PerformClick.run(View.java:18786)
                                                                                 at android.os.Handler.handleCallback(Handler.java:730)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                 at android.os.Looper.loop(Looper.java:176)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                                 at dalvik.system.NativeStart.main(Native Method)

Não sei se no menu_attach.xml eu devo trocar de RevealFrameLayout para apenas FrameLayout...

Aguardo resposta! =D

Esqueci de colocar o menu_attach.xml

menu_attach.xml

<?xml version="1.0" encoding="utf-8"?>
<io.codetail.widget.RevealFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:elevation="10dp"
    android:stateListAnimator="@null"
    android:orientation="horizontal"
    android:background="#9f1313"/>
solução!

Olá Willian,

Pelo que diz o erro só podemos realizar essa operação em uma View localizada dentro de um RevealFrameLayout.

Vamos tentar então tirar o id="@+id/reveal_items" do FrameLayout e colocá-lo no RevealFrameLayout do menu_attach.xml.

Olá Jeferson, problema solucionado! Só preciso agora tratar os eventos (caso o usuário cancele, repita ou segure o botão do menu que aciona o circular reveal). Muito obrigado cara! Me ajudou muitão!

Abraços! :D