简体   繁体   中英

Android App is crashing in Super.onCreate(savedInstanceState) at the very begining

My app crashes at the very begining, in the first line of code of the protected void onCreate(Bundle savedInstanceState) method, the line is super.onCreate(savedInstanceState); , and when I debugged it has pointed me to a file ( ActivityThread.java ) full of errors. There is a lot of import statement not working and lot of unresolved symbols. I never had problems with this file and I wonder if I have changed something important in manifest or build.graddle, because I'm really stuck in android studio and I'm following google tutorials but I've also heard they are deprecated so I'm also following this code . Besides, I've updated today the android studio software and my project weren't there when I've opened, so I had to import it again and I don't know if it can be the cause of the problem. I haven't posted the logcat because it's allmost infinite, but I'll do an edit if necesary. Any help would be appreciated.

This is a screenshot to ( ActivityThread.java )

在此处输入图片说明

This is my manifest:

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

<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="22"/>


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.alfredo.webapp.permission.C2D_MESSAGE"
    android:protectionLevel="signature"/>
<uses-permission android:name="com.example.alfredo.webapp.permission.C2D_MESSAGE"/>
<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">
    <!-- Required for applications which use Google Play Services. -->
   <!-- <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> -->
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
    <receiver
        android:name=".MainActivity$GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <!--android:name=".MainActivity$GcmBroadcastReceiver" -->

        <intent-filter>
            <!--
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="com.example.alfredo.webapp" />
            -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.google.android.gcm.demo.app" />
        </intent-filter>
    </receiver>
    <!--
<service android:name=".MainActivity$GcmIntentService" />
    -->
    <service android:name=".MainActivity$GcmIntentService" />
</application>

build.gradle:

    apply plugin: 'com.android.application'
android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.alfredo.webapp"
        minSdkVersion 10
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.google.android.gms:play-services:7.0.0'
}

And main activity:

    package com.example.alfredo.webapp;


import android.app.Activity;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.atomic.AtomicInteger;

public class MainActivity extends ActionBarActivity {

    public static final String PROPERTY_REG_ID = "registration_id";
    private static final String PROPERTY_APP_VERSION = "appVersion";
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;

    String SENDER_ID = "46075xxxx056";

    /**
     * Tag used on log messages.
     */
    static final String TAG = "GCMDemo";
    private WebView myWebView = null;

    TextView mDisplay;
    GoogleCloudMessaging gcm;
    AtomicInteger msgId = new AtomicInteger();
    SharedPreferences prefs;
    Context context;
    String regid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Log.d(TAG, "hola coño");
        setContentView(R.layout.activity_main);

        //initialize variables
        context = getApplicationContext();
        gcm = GoogleCloudMessaging.getInstance(this);
        prefs = getPreferences(0);
        mDisplay = new TextView(getApplicationContext());

        // web view
        this.myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://mongini.net/guiasdelsur");

        //remove shared prefs
        /*
        SharedPreferences prefs = getGCMPreferences(context);
        SharedPreferences.Editor edit = prefs.edit();
        edit.clear();
        edit.commit();
        */
        /*
        SharedPreferences clear_cache = getSharedPreferences("registration_id", MODE_PRIVATE);
        SharedPreferences.Editor edit = clear_cache.edit();
        edit.clear();
        edit.commit();
        */


        // Check device for Play Services APK. If check succeeds, proceed with
        //  GCM registration.
        if (checkPlayServices()) {
            gcm = GoogleCloudMessaging.getInstance(this);
            regid = getRegistrationId(context);

            if (regid.isEmpty()) {
                registerInBackground();
                Log.i(TAG,"ok");
            }
            } else {
            Log.i(TAG, "No valid Google Play Services APK found.");

        }
    }



    // You need to do the Play Services APK check here too.
    @Override
    protected void onResume() {
        super.onResume();
        checkPlayServices();
    }

    /**
     * Check the device to make sure it has the Google Play Services APK. If
     * it doesn't, display a dialog that allows users to download the APK from
     * the Google Play Store or enable it in the device's system settings.
     */
    private boolean checkPlayServices() {

        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                        PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Log.d(TAG, "This device is not supported.");

                finish();
            }
            return false;
        }
        return true;
    }
        /**
         * Gets the current registration ID for application on GCM service.
         * <p>
         * If result is empty, the app needs to register.
         *
         * @return registration ID, or empty string if there is no existing
         *         registration ID.
         */
    private String getRegistrationId(Context context) {
        final SharedPreferences prefs = getGCMPreferences(context);
        String registrationId = prefs.getString(PROPERTY_REG_ID, "");
        if (registrationId.isEmpty()) {
            Log.i(TAG, "Registration not found.");
            return "";
        }
        // Check if app was updated; if so, it must clear the registration ID
        // since the existing registration ID is not guaranteed to work with
        // the new app version.
        int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
        int currentVersion = getAppVersion(context);
        if (registeredVersion != currentVersion) {
            Log.i(TAG, "App version changed.");
            return "";
        }
        return registrationId;
    }

    /**
     * @return Application's {@code SharedPreferences}.
     */
    private SharedPreferences getGCMPreferences(Context context) {
        // This sample app persists the registration ID in shared preferences, but
        // how you store the registration ID in your app is up to you.
        return getSharedPreferences(MainActivity.class.getSimpleName(),
                Context.MODE_PRIVATE);
    }


    /**
     * @return Application's version code from the {@code PackageManager}.
     */
    private static int getAppVersion(Context context) {
        try {
            PackageInfo packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            // should never happen
            throw new RuntimeException("Could not get package name: " + e);
        }
    }

    /**
     * Registers the application with GCM servers asynchronously.
     * <p>
     * Stores the registration ID and app versionCode in the application's
     * shared preferences. com.example.alfredo.webapp.MainActivity
     */
    private void registerInBackground() {

        new AsyncTask<Void,Void,String>() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                try {

                    regid = gcm.register(SENDER_ID);


                    msg = "Device registered, registration id=" + regid;

                    // You should send the registration ID to your server over HTTP,
                    // so it can use GCM/HTTP or CCS to send messages to your app.
                    sendRegistrationIdToBackend();


                    // Save the regid for future use - no need to register again.
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putString(PROPERTY_REG_ID, regid);
                    editor.commit();
                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();
                }
                return msg;
            }
            // Once registration is done, display the registration status
            // string in the Activity's UI.
            @Override
            protected void onPostExecute(String msg) {
                mDisplay.append(msg + "\n");
            }
        }.execute();
    }



    private String readStream(InputStream is) {
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            int i = is.read();
            while(i != -1) {
                bo.write(i);
                i = is.read();
            }
            return bo.toString();
        } catch (IOException e) {
            return "";
        }
    }

    /**
     * Sends the registration ID to your server over HTTP, so it can use GCM/HTTP
     * or CCS to send messages to your app. Not needed for this demo since the
     * device sends upstream messages to a server that echoes back the message
     * using the 'from' address in the message.
     */
    private void sendRegistrationIdToBackend() {
        // Your implementation here.
        HttpURLConnection urlConnection = null;
        try {

            URL url = new URL("http://www.example.com/example.php?host=xxxx&dbname=xxx&user=mongini_webapp&pass=xxx&idPush="+regid);
            urlConnection = (HttpURLConnection) url.openConnection();

            /** Connecting to url */
            urlConnection.connect();

            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            readStream(in);
        }catch(Exception e){
            Log.d("Exception url ", e.toString());
        }finally {
            urlConnection.disconnect();
            }

    }


    /**
     * Stores the registration ID and app versionCode in the application's
     * {@code SharedPreferences}.
     *
     * @param context application's context.
     * @param regId registration ID
     */
    //aqui peta
    private void storeRegistrationId(Context context, String regId) {
        final SharedPreferences prefs = getGCMPreferences(context);
        int appVersion = getAppVersion(context);
        Log.i(TAG, "Saving regId on app version " + appVersion);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PROPERTY_REG_ID, regId);
        editor.putInt(PROPERTY_APP_VERSION, appVersion);
        editor.commit();
    }



    public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Explicitly specify that GcmIntentService will handle the intent.
            ComponentName comp = new ComponentName(context.getPackageName(),
                    GcmIntentService.class.getName());
            // Start the service, keeping the device awake while it is launching.
            startWakefulService(context, (intent.setComponent(comp)));
            setResultCode(Activity.RESULT_OK);
        }
    }


    public class GcmIntentService extends IntentService {
        public static final int NOTIFICATION_ID = 1;
        private NotificationManager mNotificationManager;
        NotificationCompat.Builder builder;

        public GcmIntentService() {
            super("GcmIntentService");

        }

        @Override
        protected void onHandleIntent(Intent intent) {
            Bundle extras = intent.getExtras();
            GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
            // The getMessageType() intent parameter must be the intent you received
            // in your BroadcastReceiver.
            String messageType = gcm.getMessageType(intent);

            if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */
                if (GoogleCloudMessaging.
                        MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                    sendNotification("Send error: " + extras.toString());
                } else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_DELETED.equals(messageType)) {
                    sendNotification("Deleted messages on server: " +
                            extras.toString());
                    // If it's a regular GCM message, do some work.
                } else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                    // This loop represents the service doing some work.
                    for (int i=0; i<5; i++) {
                        Log.i(TAG, "Working... " + (i+1)
                                + "/5 @ " + SystemClock.elapsedRealtime());
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                        }
                    }
                    Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                    // Post notification of received message.
                    sendNotification("Received: " + extras.toString());
                    Log.i(TAG, "Received: " + extras.toString());
                }
            }
            // Release the wake lock provided by the WakefulBroadcastReceiver.
            GcmBroadcastReceiver.completeWakefulIntent(intent);
        }

        // Put the message into a notification and post it.
        // This is just one simple example of what you might choose to do with
        // a GCM message.
        private void sendNotification(String msg) {
            mNotificationManager = (NotificationManager)
                    this.getSystemService(Context.NOTIFICATION_SERVICE);

            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    new Intent(this, MainActivity.class), 0);

            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            //.setSmallIcon(R.drawable.ic_stat_gcm)
                            .setContentTitle("GCM Notification")
                            .setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText(msg))
                            .setContentText(msg);

            mBuilder.setContentIntent(contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        }
    }



    //back device button
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && this.myWebView.canGoBack()) {
            this.myWebView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

Edit to add logcat (added a very small portion because it's almost infinite):

    05-04 08:01:33.850  23385-23454/? E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.ff.a
05-04 08:01:33.880  23385-23454/? E/dalvikvm﹕ Could not find class 'android.app.Notification$Builder', referenced from method com.google.android.gms.common.ff.b
05-04 08:01:36.010  10233-23463/? E/TalkProvider﹕ replaceContactWithContactId: contactId==0!!! pablogarces123@gmail.com, acct=1
05-04 08:01:36.010  10233-23463/? E/TalkProvider﹕ insert presence failed for account=1 username=pablogarces123@gmail.com client_type=2 status= cap=0 priority=0 mode=3
05-04 08:01:36.090  10233-23463/? E/TalkProvider﹕ replaceContactWithContactId: contactId==0!!! pablogarces123@gmail.com, acct=1
05-04 08:01:36.090  10233-23463/? E/TalkProvider﹕ insert presence failed for account=1 username=pablogarces123@gmail.com client_type=2 status= cap=0 priority=0 mode=3
05-04 08:01:37.469  23385-23466/? E/dalvikvm﹕ Could not find class 'android.os.UserManager', referenced from method com.google.android.gms.common.util.a.b
05-04 08:01:37.469  23385-23466/? E/dalvikvm﹕ Could not find class 'android.os.UserManager', referenced from method com.google.android.gms.common.util.a.c
05-04 08:01:37.469  23385-23466/? E/dalvikvm﹕ Could not find class 'android.os.UserManager', referenced from method com.google.android.gms.common.util.a.e
05-04 08:01:37.499  23223-23241/? E/﹕ statfs /mnt/secure/asec failed, errno: 13
05-04 08:01:40.499  23223-23233/? E/﹕ statfs /mnt/secure/asec failed, errno: 13
05-04 08:01:49.679  10207-10207/? E/Launcher﹕ setWindowOpaque()
05-04 08:01:49.729  10207-10207/? E/Launcher﹕ MTP-LAUNCHER: media scanning not yet finished.
05-04 08:01:57.119  10079-23504/? E/PlayerDriver﹕ Creating Non-Tunnel mode playback - uncompressed MIO
05-04 08:01:57.159  10079-23506/? E/PlayerDriver﹕ Creating Non-Tunnel mode playback - uncompressed MIO
05-04 08:01:57.179  10079-23508/? E/PlayerDriver﹕ Creating Non-Tunnel mode playback - uncompressed MIO
05-04 08:01:57.199  10079-23510/? E/PlayerDriver﹕ Creating Non-Tunnel mode playback - uncompressed MIO
05-04 08:01:57.219  10079-23512/? E/PlayerDriver﹕ Creating Non-Tunnel mode playback - uncompressed MIO
05-04 08:01:57.239  10079-23514/? E/PlayerDriver﹕ Creating Non-Tunnel mode playback - uncompressed MIO
05-04 08:02:04.759  23530-23530/? E/dalvikvm﹕ Could not find class 'android.database.sqlite.SQLiteCantOpenDatabaseException', referenced from method com.google.android.gms.plus.provider.PlusProvider.a
05-04 08:02:15.619  10233-10233/? E/AuthorizationBluetoothService﹕ Proximity feature is not enabled.
05-04 08:02:16.169  23602-23602/? E/dalvikvm﹕ Could not find class 'android.database.sqlite.SQLiteCantOpenDatabaseException', referenced from method com.google.android.gms.plus.provider.PlusProvider.a
05-04 08:02:17.359  10233-10233/? E/AuthorizationBluetoothService﹕ Proximity feature is not enabled.
05-04 08:02:25.849  23551-23551/? E/Finsky﹕ [1] InstallerTask.requireInternalStorageOrCancel: Cancel download of com.google.android.gms because insufficient free space
05-04 08:03:25.599  23641-23641/? E/dalvikvm﹕ Could not find class 'android.database.sqlite.SQLiteCantOpenDatabaseException', referenced from method com.google.android.gms.plus.provider.PlusProvider.a
05-04 08:03:26.139  10233-10233/? E/AuthorizationBluetoothService﹕ Proximity feature is not enabled.
05-04 08:03:26.759  23641-23656/? E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.ff.a
05-04 08:03:26.759  23641-23656/? E/dalvikvm﹕ Could not find class 'android.app.Notification$Builder', referenced from method com.google.android.gms.common.ff.b
05-04 08:03:27.959  23641-23656/? E/dalvikvm﹕ Could not find class 'android.security.KeyPairGeneratorSpec$Builder', referenced from method com.google.android.gms.auth.e.a.a
05-04 08:03:30.809  23641-23664/? E/dalvikvm﹕ Could not find class 'android.os.UserManager', referenced from method com.google.android.gms.common.util.a.b

I've finally started a new project from scratch, copied the code and figured it was because I were extending actionBarActivity (deprecated) instead of Activity, and it looks like this class need a theme and I weren't using one. Thank you very much to all who has answered or read my question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM