Error while trying to start another Activity via a button

Asked

Viewed 675 times

2

My program runs, but when I click on the q button I should open a new Activity, the program closes saying there is 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.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.Criar evento.onCreate(Criar evento.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 could 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’ve tried some internet tutorials and some responses from other posts here but they didn’t help. 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

  • 1

    Puts the code you have, makes it easy to give an answer.

  • 1

    Welcome Raissa. You will need to include your code so the staff can help you. If possible provide a MCVE

  • 1

    Welcome to Stack Overflow! In order for the community to help you, it is important that you explain in detail your problem and show the code you have done so far! I suggest you read the articles: Tour and how to ask a question.

  • What do you do with the Actionbar in class Criaevent???

  • The Create Event class does nothing yet, only takes a few texts.

  • What you can understand from his mistake is that he couldn’t find Actionbar: on a null Object Reference at com.example.Raissa.calencon.Create event.onCreate(Createvent.java:12) . What makes line 12? This seems to me something related to the Theme of the Criaevent

  • line 12: getActionBar(). setDisplayHomeAsUpEnabled(true);

  • do not create any action bar, need to modify something?

Show 3 more comments

2 answers

2

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

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

2

There are two possibilities:

  1. It is likely that your Theme not of Actionbar support. Try to change it in the file styles.xml for this:

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

Instead of using getActionBar , try getSupportActionBar().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.