Firebase应用程序崩溃

时间:2017-07-23 18:05:57

标签: android firebase firebase-realtime-database firebase-storage

我遵循本教程: - https://www.youtube.com/watch?v=x0ScnHJi8WY 而且我不知道,为什么我的应用程序崩溃了

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.vks_apps.blogs.MainActivity">
</RelativeLayout>

MainActivity.java

package com.vks_apps.blogs;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if(item.getItemId()==R.id.action_add)
        {
            startActivity(new Intent(MainActivity.this,PostActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_post.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.vks_apps.blogs.PostActivity">

    <ImageButton
        android:id="@+id/imageSelect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:background="#00ffffff"
        android:scaleType="centerCrop"
        android:src="@mipmap/add_btn" />

    <EditText
        android:id="@+id/titleField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageSelect"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/input_outline"
        android:hint="Post Title..."
        android:paddingBottom="15dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingTop="15dp"
        android:singleLine="true" />

    <EditText
        android:id="@+id/descField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/titleField"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/input_outline"
        android:ems="10"
        android:hint="Post Description..."
        android:inputType="textMultiLine"
        android:paddingBottom="15dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingTop="15dp" />

    <Button
        android:id="@+id/submitBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@color/colorPrimary"
        android:text="Submit Post"
        android:textColor="#ffffff" />
</RelativeLayout>

PostActivity.java

package com.vks_apps.blogs;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

public class PostActivity extends AppCompatActivity
{
    private ImageButton mSelectImage;
    public static final int GALLERY_REQUEST = 1;
    private Uri mImageUri = null;
    private EditText mPostTitle, mPostDesc;
    private Button mSubmitBtn;
    private StorageReference mStorage;
    private ProgressDialog mProgress;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);

        mSelectImage = (ImageButton)findViewById(R.id.imageSelect);
        mPostTitle = (EditText)findViewById(R.id.titleField);
        mPostDesc = (EditText)findViewById(R.id.descField);
        mSubmitBtn = (Button) findViewById(R.id.submitBtn);
        mProgress = new ProgressDialog(this);

        mStorage = FirebaseStorage.getInstance().getReference();

        mSelectImage.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent,GALLERY_REQUEST);
            }
        });

        mSubmitBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                startPosting();
            }
        });
    }

    private void startPosting()
    {
        mProgress.setMessage("Posting to Blog...");
        mProgress.show();

        String title_val = mPostTitle.getText().toString().trim();
        String desc_val = mPostDesc.getText().toString().trim();

        if((!TextUtils.isEmpty(title_val)) && (!TextUtils.isEmpty(desc_val)) && (mImageUri != null))
        {
            StorageReference filepath = mStorage.child("Blog_Images").child(mImageUri.getLastPathSegment());
            filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>()
            {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
                {
                    @SuppressWarnings("VisibleForTests") Uri downloadUri = taskSnapshot.getDownloadUrl();
                    mProgress.dismiss();
                }
            });
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==GALLERY_REQUEST && resultCode==RESULT_OK)
        {
            mImageUri = data.getData();
            mSelectImage.setImageURI(mImageUri);
        }
    }
}

应用/的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.vks_apps.blogs"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support:recyclerview-v7:26.+'
    compile 'com.android.support:cardview-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-database:10.0.1'
    compile 'com.google.firebase:firebase-storage:10.0.1'
    compile 'com.google.firebase:firebase-auth:10.0.1'
    compile 'com.firebaseui:firebase-ui-database:2.1.0'

    testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'

项目/的build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.google.gms:google-services:3.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vks_apps.blogs">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".PostActivity"></activity>
    </application>

</manifest>

RES /抽拉/ input_outline.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:color="@color/greyColor" android:width="1dp"/>
    <corners android:radius="5dp"/>
</shape>

RES /值/ colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#0488d1</color>
    <color name="colorPrimaryDark">#0477bd</color>
    <color name="colorAccent">#FF4081</color>
    <color name="greyColor">#c2c2c2</color>
</resources>

logcat的

07-23 23:22:31.904 6461-6461/com.vks_apps.blogs D/AndroidRuntime: Shutting down VM


                                                                  --------- beginning of crash
07-23 23:22:31.904 6461-6461/com.vks_apps.blogs E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.vks_apps.blogs, PID: 6461
                                                                  java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/internal/zzac;
                                                                      at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source)
                                                                      at com.vks_apps.blogs.PostActivity.onCreate(PostActivity.java:42)
                                                                      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:866)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                   Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.internal.zzac" on path: DexPathList[[zip file "/data/app/com.vks_apps.blogs-2/base.apk", zip file "/data/app/com.vks_apps.blogs-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.vks_apps.blogs-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.vks_apps.blogs-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.vks_apps.blogs-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.vks_apps.blogs-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.vks_apps.blogs-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.vks_apps.blogs-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.vks_apps.blogs-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.vks_apps.blogs-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.vks_apps.blogs-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.vks_apps.blogs-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.vks_apps.blogs-2/lib/x86, /system/lib, /vendor/lib]]
                                                                      at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                      at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                      at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                      at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source) 
                                                                      at com.vks_apps.blogs.PostActivity.onCreate(PostActivity.java:42) 
                                                                      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:866) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
PS:不要将此问题标记为重复,因为我已经尝试过所有其他方法。在这里,我没有任何修改地编写了原始代码。我还上传了我的应用崩溃的视频 - 你可以看到它:http://www.mediafire.com/file/xb61s4k0wojxy2z/app.mp4

我也认为这是因为这条线:

mStorage = FirebaseStorage.getInstance().getReference();

因为当我删除此行时,我的应用程序运行正常,但我需要此行上传文件

1 个答案:

答案 0 :(得分:1)

FirebaseUI documentation包含有关依赖项的部分,其中包含以下警告和兼容版本表:

  

如果您在表单编译的应用程序中使用任何依赖项   &#39; com.google.firebase:firebase- :X.Y.Z&#39;或编译   &#39; com.google.android.gms:发挥服务 - 的:X.Y.Z&#39;你需要确定   您使用与所选版本的FirebaseUI相同的版本   需要。

您使用的是Firebase库的10.0.1版。对于那些,您必须使用FirebaseUI组件的1.1.1版:com.firebaseui:firebase-ui-database:1.1.1

<强>更新

问题可能是版本与v26支持库不兼容。尝试这些依赖项:

compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support:recyclerview-v7:26.+'
compile 'com.android.support:cardview-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.firebase:firebase-database:11.0.2' // CHANGED
compile 'com.google.firebase:firebase-storage:11.0.2' // CHANGED
compile 'com.google.firebase:firebase-auth:11.0.2' // CHANGED
compile 'com.firebaseui:firebase-ui-database:2.1.0' // CHANGED