Error trying to start another activity through a button

2

My program runs, but when I click the q button it should open a new activity, the program closes saying that it hears an error.

  

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.CriarEvent}:   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 (BuildEvent.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)

What can it be? My code:

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();
    } }

XML file:

<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" />

I have tried some internet tutorials and some answers from other posts here but they did not work. When I start the program:

  

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

    
asked by anonymous 06.01.2016 / 20:08

2 answers

1

There are two possibilities:

  • It is likely that your Theme does not support ActionBar. Try changing it in the styles.xml file to this:

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

  • Instead of using getActionBar , try getSupportActionBar () .

        
    07.01.2016 / 11:47
    1

    If you are using Theme.Holo.Light and do not want to use ActionBar , then you can disable it by changing its style.xml by modifying the windowActionBar property:

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