Android应用程序在开始时崩溃

时间:2017-06-16 02:47:39

标签: android android-studio crash

我正在尝试使用google登录选项构建应用程序。但是,在我成功构建它之后,它在启动时崩溃了。

这是activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.nebib.googlesignin.MainActivity">

    <com.google.android.gms.common.SignInButton
        android:id="@+id/bn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></com.google.android.gms.common.SignInButton>

    <LinearLayout
        android:id="@+id/prof_section"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="50dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/prof_pic"
            android:layout_width="match_parent"
            android:layout_height="90dp"
            android:src="@drawable/photo" />

        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="83dp"
            android:gravity="center"
            android:text="Nebih Basaran"
            android:textSize="18dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="nebih@nebih.com"
            android:textAlignment="center" />

        <Button
            android:id="@+id/bn_logout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="logout" />

    </LinearLayout>

</LinearLayout>

和活动主要:

package com.example.nebib.googlesignin;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener{

    private LinearLayout Prof_Section;
    private Button SignOut;
    private SignInButton SignIn;
    private TextView Name,Email;
    private ImageView Prof_Pic;
    private GoogleApiClient googleApiClient;
    private static final int REQ_CODE = 9001;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Prof_Section = (LinearLayout)findViewById(R.id.prof_section);
        SignOut = (Button)findViewById(R.id.bn_logout);
        SignIn = (SignInButton)findViewById(R.id.bn_login);
        Name = (TextView)findViewById(R.id.name);
        Email = (TextView)findViewById(R.id.email);
        Prof_Pic = (ImageView)findViewById(R.id.prof_pic);
        SignIn.setOnClickListener(this);
        SignOut.setOnClickListener(this);
        Prof_Section.setVisibility(View.GONE);
        GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
        googleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this,this).addApi(Auth.GOOGLE_SIGN_IN_API,signInOptions).build();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.bn_login:
                signIn();
                break;
            case R.id.bn_logout:
                signOut();
                break;
        }
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    private void signIn()
    {
        Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
        startActivityForResult(intent,REQ_CODE);

    }

    private void signOut()
    {
        Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(new ResultCallback<Status>() {
            @Override
            public void onResult(@NonNull Status status) {
                updateUI(false);
            }
        });
    }

    private void handleResult(GoogleSignInResult result)
    {
        if(result.isSuccess())
        {
            GoogleSignInAccount account = result.getSignInAccount();
            String name = account.getDisplayName();
            String email = account.getEmail();
            String img_url = account.getPhotoUrl().toString();
            Name.setText(name);
            Email.setText(email);
            Glide.with(this).load(img_url).into(Prof_Pic);
            updateUI(true);
        }
        else{
            updateUI(false);
        }
    }

    private void updateUI(boolean isLogin) {
        if(isLogin) {
            Prof_Section.setVisibility(View.VISIBLE);
            SignIn.setVisibility(View.GONE);
        }
        else{

                Prof_Section.setVisibility(View.GONE);
                SignIn.setVisibility(View.VISIBLE);

            }
        }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);

        if(requestCode==REQ_CODE){
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleResult(result);
        }
    }
}

我在模拟器和真实设备上都尝试过。两者都给出了同样的崩溃。任何帮助表示赞赏。提前致谢

这是logcat:

Logcat:
02-02 18:52:57.132: VERBOSE/ProtocolEngine(24): DownloadRate 104166 bytes per sec. Downloaded Bytes 5643/34714
08-03 13:31:16.196: DEBUG/dalvikvm(2227): HeapWorker thread shutting down
08-03 13:31:16.756: INFO/dalvikvm(2234): Debugger is active
08-03 16:26:45.965: WARN/ActivityManager(564): Launch timeout has expired, giving up wake lock!
08-04 16:19:11.166: ERROR/AndroidRuntime(4687): Uncaught handler: thread main exiting due to uncaught exception
08-04 16:24:11.166: ASSERT/Assertion(4687): Expected true but was false

完整的logcat文件:

06-16 03:17:44.140 5906-5906/? I/art: Not late-enabling -Xcheck:jni (already on)
06-16 03:17:44.140 5906-5906/? W/art: Unexpected CPU variant for X86 using defaults: x86
06-16 03:17:44.352 5906-5906/? W/System: ClassLoader referenced unknown path: /data/app/com.example.nebib.googlesignin-2/lib/x86
06-16 03:17:44.382 5906-5906/? D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
06-16 03:17:44.399 5906-5906/? D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
06-16 03:17:44.449 5906-5906/? I/FA: App measurement is starting up, version: 9877
06-16 03:17:44.449 5906-5906/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
06-16 03:17:44.449 5906-5906/? D/FA: Debug-level message logging enabled
06-16 03:17:44.449 5906-5906/? D/FA: AppMeasurement singleton hash: 43392192
06-16 03:17:44.470 5906-5906/? V/FA: Collection enabled
06-16 03:17:44.471 5906-5906/? V/FA: App package, google app id: com.example.nebib.googlesignin, 1:893119025741:android:8e1b171f63066841
06-16 03:17:44.471 5906-5906/? I/FA: To enable faster debug mode event logging run:
                                       adb shell setprop firebase.analytics.debug-mode com.example.nebib.googlesignin
06-16 03:17:44.561 5906-5906/? V/FA: Registered activity lifecycle callback
06-16 03:17:44.563 5906-5906/? I/FirebaseInitProvider: FirebaseApp initialization successful
06-16 03:17:44.564 5906-5906/? I/InstantRun: starting instant run server: is main process
06-16 03:17:44.568 5906-5934/? V/FA: Using measurement service
06-16 03:17:44.592 5906-5934/? V/FA: Connecting to remote service
06-16 03:17:44.709 5906-5906/com.example.nebib.googlesignin W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-16 03:17:44.720 5906-5906/com.example.nebib.googlesignin V/FA: onActivityCreated
06-16 03:17:44.924 5906-5906/com.example.nebib.googlesignin W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
06-16 03:17:44.949 5906-5906/com.example.nebib.googlesignin W/System: ClassLoader referenced unknown path: 
06-16 03:17:44.949 5906-5906/com.example.nebib.googlesignin W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
06-16 03:17:44.977 5906-5906/com.example.nebib.googlesignin W/System: ClassLoader referenced unknown path: /data/user_de/0/com.google.android.gms/app_chimera/m/00000009/n/x86
06-16 03:17:45.157 5906-5906/com.example.nebib.googlesignin I/art: Starting a blocking GC Alloc
06-16 03:17:45.158 5906-5906/com.example.nebib.googlesignin I/art: Starting a blocking GC Alloc
06-16 03:17:45.187 5906-5906/com.example.nebib.googlesignin I/art: Alloc sticky concurrent mark sweep GC freed 14718(1509KB) AllocSpace objects, 6(224KB) LOS objects, 36% free, 6MB/10MB, paused 1.911ms total 28.443ms
06-16 03:17:45.187 5906-5906/com.example.nebib.googlesignin I/art: Starting a blocking GC Alloc
06-16 03:17:45.203 5906-5906/com.example.nebib.googlesignin I/art: Alloc partial concurrent mark sweep GC freed 295(14KB) AllocSpace objects, 0(0B) LOS objects, 38% free, 6MB/10MB, paused 467us total 15.277ms
06-16 03:17:45.203 5906-5906/com.example.nebib.googlesignin I/art: Starting a blocking GC Alloc
06-16 03:17:45.211 5906-5906/com.example.nebib.googlesignin I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 38% free, 6MB/10MB, paused 661us total 7.585ms
06-16 03:17:45.212 5906-5906/com.example.nebib.googlesignin I/art: Forcing collection of SoftReferences for 380MB allocation
06-16 03:17:45.212 5906-5906/com.example.nebib.googlesignin I/art: Starting a blocking GC Alloc
06-16 03:17:45.221 5906-5906/com.example.nebib.googlesignin I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 38% free, 6MB/10MB, paused 460us total 8.355ms
06-16 03:17:45.222 5906-5906/com.example.nebib.googlesignin W/art: Throwing OutOfMemoryError "Failed to allocate a 398647020 byte allocation with 4194304 free bytes and 377MB until OOM"
06-16 03:17:45.222 5906-5906/com.example.nebib.googlesignin I/art: Starting a blocking GC Alloc
06-16 03:17:45.222 5906-5906/com.example.nebib.googlesignin I/art: Starting a blocking GC Alloc
06-16 03:17:45.228 5906-5906/com.example.nebib.googlesignin I/art: Starting a blocking GC Alloc
06-16 03:17:45.238 5906-5906/com.example.nebib.googlesignin I/art: Alloc partial concurrent mark sweep GC freed 6(144B) AllocSpace objects, 0(0B) LOS objects, 38% free, 6MB/10MB, paused 966us total 9.731ms
06-16 03:17:45.238 5906-5906/com.example.nebib.googlesignin I/art: Starting a blocking GC Alloc
06-16 03:17:45.257 5906-5906/com.example.nebib.googlesignin I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 38% free, 6MB/10MB, paused 486us total 11.881ms
06-16 03:17:45.258 5906-5906/com.example.nebib.googlesignin I/art: Forcing collection of SoftReferences for 380MB allocation
06-16 03:17:45.258 5906-5906/com.example.nebib.googlesignin I/art: Starting a blocking GC Alloc
06-16 03:17:45.271 5906-5906/com.example.nebib.googlesignin I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 38% free, 6MB/10MB, paused 671us total 12.728ms
06-16 03:17:45.277 5906-5906/com.example.nebib.googlesignin W/art: Throwing OutOfMemoryError "Failed to allocate a 398647020 byte allocation with 4194304 free bytes and 377MB until OOM"
06-16 03:17:45.277 5906-5906/com.example.nebib.googlesignin D/skia: --- allocation failed for scaled bitmap
06-16 03:17:45.279 5906-5906/com.example.nebib.googlesignin D/AndroidRuntime: Shutting down VM
06-16 03:17:45.280 5906-5906/com.example.nebib.googlesignin E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.nebib.googlesignin, PID: 5906
                                                                              java.lang.OutOfMemoryError: Failed to allocate a 398647020 byte allocation with 4194304 free bytes and 377MB until OOM
                                                                                  at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                                  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                                  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
                                                                                  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
                                                                                  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1152)
                                                                                  at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720)
                                                                                  at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
                                                                                  at android.content.res.Resources.loadDrawable(Resources.java:854)
                                                                                  at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
                                                                                  at android.widget.ImageView.<init>(ImageView.java:157)
                                                                                  at android.widget.ImageView.<init>(ImageView.java:145)
                                                                                  at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
                                                                                  at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
                                                                                  at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1026)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1083)
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
                                                                                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
                                                                                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                                  at com.example.nebib.googlesignin.MainActivity.onCreate(MainActivity.java:38)
                                                                                  at android.app.Activity.performCreate(Activity.java:6662)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

1 个答案:

答案 0 :(得分:0)

在清单文件android:hardwareAccelerated="false"android:largeHeap="true"

中添加以下实体

here

还有其他选择