javaandroiduncaught-exceptionandroid-actionbaractivityappcompatactivity

Extending AppCompActivity gives uncaught exception


From my MainActivity, when I press a button, I have to open another activity that extends AppCompacActivity. The problem is that I get my application to crush because of an unhandled exception. Here is the code

MainProgram code:

Button NextButton = (Button)findViewById(R.id.next_button);
    NextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), GeofenceMainActivity.class);
            startActivity(intent);
        }
    });

GeofenceMainActivity code:

package com.clb.apokalos.todolistas;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.GeofencingApi;
import com.google.android.gms.maps.model.LatLng;

import java.util.ArrayList;
import java.util.Map;

/**
 * Demonstrates how to create and remove geofences using the GeofencingApi. Uses an IntentService
 * to monitor geofence transitions and creates notifications whenever a device enters or exits
 * a geofence.
 *
 * This sample requires a device's Location settings to be turned on. It also requires
 * the ACCESS_FINE_LOCATION permission, as specified in AndroidManifest.xml.
 *
 * Note that this Activity implements ResultCallback<Status>, requiring that
 * {@code onResult} must be defined. The {@code onResult} runs when the result of calling
 * {@link GeofencingApi#addGeofences(GoogleApiClient, GeofencingRequest, PendingIntent)}  addGeofences()} or
 * {@link com.google.android.gms.location.GeofencingApi#removeGeofences(GoogleApiClient, java.util.List)}  removeGeofences()}
 * becomes available.
 */
public class GeofenceMainActivity extends AppCompatActivity implements
        ConnectionCallbacks, OnConnectionFailedListener, ResultCallback<Status> {

    //variables here.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }

    protected synchronized void buildGoogleApiClient() {...}

    @Override
    protected void onStart() {...}

    @Override
    protected void onStop() {...}

    @Override
    public void onConnected(Bundle connectionHint) {...}

    @Override
    public void onConnectionFailed(ConnectionResult result) {...}

    @Override
    public void onConnectionSuspended(int cause) {...}

    private GeofencingRequest getGeofencingRequest() {...}

    public void addGeofencesButtonHandler(View view) {...}

    public void removeGeofencesButtonHandler(View view) {...}

    private void logSecurityException(SecurityException securityException) {...}

    public void onResult(Status status) {...}

    private PendingIntent getGeofencePendingIntent() {...}

    public void populateGeofenceList() {...}

    private void setButtonsEnabledState() {...}
}

This code has been modified since in it's original implementation, it was extending ActionBarActivity (now deprecated), so I modified the code by substituting ActionBarActivity with AppCompatActivity.

The error code is just: threadid=1: thread exiting with uncaught exception (group=0x41da6438)

Could you help me? Try blocks doesn't seem to help me to find a solution.

EDIT: I've got the stacktrace

01-16 16:45:11.597 31258-31258/com.clb.apokalos.todolistas W/System.err: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
01-16 16:45:11.597 31258-31258/com.clb.apokalos.todolistas W/System.err:     at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:112)
01-16 16:45:11.597 31258-31258/com.clb.apokalos.todolistas W/System.err:     at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:148)
01-16 16:45:11.597 31258-31258/com.clb.apokalos.todolistas W/System.err:     at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:60)
01-16 16:45:11.597 31258-31258/com.clb.apokalos.todolistas W/System.err:     at com.clb.apokalos.todolistas.GeofenceMainActivity.onCreate(GeofenceMainActivity.java:81)
01-16 16:45:11.597 31258-31258/com.clb.apokalos.todolistas W/System.err:     at android.app.Activity.performCreate(Activity.java:5024)
01-16 16:45:11.597 31258-31258/com.clb.apokalos.todolistas W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2042)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2103)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/System.err:     at android.app.ActivityThread.access$600(ActivityThread.java:137)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/System.err:     at android.os.Looper.loop(Looper.java:137)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:4842)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/System.err:     at dalvik.system.NativeStart.main(Native Method)
01-16 16:45:11.607 31258-31258/com.clb.apokalos.todolistas W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41da6438)

Solution

  • try adding this theme in your manifest:

    android:theme="@style/Theme.AppCompat" >
    

    in activity tag

     <activity
            android:name=".GeofenceMainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat" />