2

Meu programa roda, mas quando clico no botão q deveria abrir uma nova activity, o programa fecha dizendo que ouve um erro.

FATAL EXCEPTION: main Process: com.example.raissa.calencon, PID: 2142
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.raissa.calencon/com.example.raissa.calencon.CriarEvento}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at com.example.raissa.calencon.CriarEvento.onCreate(CriarEvento.java:12) at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

O que pode ser? Meu código:

import android.content.Intent; 
import android.net.Uri;
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ListView;
import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

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

        ArrayList<evento_onClick> lista = new ArrayList<evento_onClick>();

        evento_onClick a = new evento_onClick("item a", R.drawable.fireworks, "22/03");
        evento_onClick b = new evento_onClick("item b", R.drawable.fireworks, "22/03");
        evento_onClick c = new evento_onClick("item c", R.drawable.fireworks, "22/03");
        evento_onClick d = new evento_onClick("item d", R.drawable.fireworks, "22/03");
        evento_onClick e = new evento_onClick("item e", R.drawable.fireworks, "22/03");
        evento_onClick f = new evento_onClick("item f", R.drawable.fireworks, "22/03");
        evento_onClick g = new evento_onClick("item g", R.drawable.fireworks, "22/03");
        evento_onClick h = new evento_onClick("item h", R.drawable.fireworks, "22/03");

        lista.add(a);
        lista.add(b);
        lista.add(c);
        lista.add(d);
        lista.add(e);
        lista.add(f);
        lista.add(g);
        lista.add(h);

        ListaAdapterEvento AdapterEvento = new ListaAdapterEvento(this, lista);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(AdapterEvento);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    public void acionaBotao(View view) {
        Intent novoEvento = new Intent(this, CriarEvento.class);
        startActivity(novoEvento);
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.raissa.calencon/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.raissa.calencon/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    } }

Arquivo XML:

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mais"
    android:id="@+id/button"
    android:textSize="25sp"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/listView"
    android:layout_alignEnd="@+id/listView"
    android:layout_alignParentEnd="false"
    android:background="#3ed2cb"
    android:textAlignment="center"
    android:nestedScrollingEnabled="false"
    android:onClick="acionaBotao" />

Já tentei alguns tutoriais da internet e algumas respostas de outros posts daqui mas não adiantaram. Quando inicio o programa:

W/EGL_emulation: eglSurfaceAttrib not implemented 01-07 18:45:54.989 2071-2086/com.example.raissa.calencon W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xeae349c0, error=EGL_SUCCESS

8
  • 1
    Coloca o código que você tem, facilita a dar uma resposta.
    – Ricardo
    6/01/2016 às 19:09
  • 1
    Bem-vinda Raissa. Você terá que incluir o seu código para o pessoal poder te ajudar. Se possível forneça um MCVE
    – Franchesco
    6/01/2016 às 19:10
  • 1
    Seja bem vinda ao Stack Overflow! Para que a comunidade possa te ajudar, é importante que se explique detalhadamente o seu problema e mostre o código que você fez até o momento! Sugiro que leia os artigos: Tour e como realizar uma pergunta. 6/01/2016 às 19:11
  • O que você faz com o ActionBar na classe CriarEvento??? 6/01/2016 às 19:32
  • A classe CriarEvento meio q nao faz nada ainda, apenas recebe alguns textos. 6/01/2016 às 19:38

2 Respostas 2

2

Há duas possibilidades:

  1. É provável que seu Theme não de suporte a ActionBar. Tente alterar ele no arquivo styles.xml para este:

    <style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    
  2. Support:

Ao invés de usar getActionBar , tente getSupportActionBar().

3
2

Se caso você esteja usando Theme.Holo.Light e não queira utilizar um ActionBar, então você pode desabilitar alterando seu style.xml modificando a propriedade windowActionBar:

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
</style> 

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .