2017-10-06 75 views
-1

我想開發一個應用程序在Android API 23(棉花糖),但我不知道爲什麼我的應用程序在設備或模擬器上運行時保持崩潰。到目前爲止,我沒有收到任何編譯錯誤。爲什麼在仿真器或設備Android API 23上運行時應用程序會一直崩潰?

起初,我認爲問題在於依賴關係和gradle配置,但我嘗試了更改依賴關係,但是在編譯或gradle錯誤時會造成更多錯誤。當我試圖將我的buildToolsVersion更改爲23時,我得到了像srcCompat和其他許多錯誤。

任何人都可以指出我哪裏有錯誤的配置或代碼? 謝謝,順便說一句。

這是我LoginActivity.java

package com.dcangkring.mobile; 

import android.animation.Animator; 
import android.animation.AnimatorListenerAdapter; 
import android.annotation.TargetApi; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.support.annotation.NonNull; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.app.LoaderManager.LoaderCallbacks; 

import android.content.CursorLoader; 
import android.content.Loader; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.AsyncTask; 

import android.os.Build; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.text.TextUtils; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.inputmethod.EditorInfo; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

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.tasks.OnCompleteListener; 
import com.google.android.gms.tasks.Task; 
import com.google.firebase.auth.AuthResult; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.auth.FirebaseUser; 

import java.util.ArrayList; 
import java.util.List; 

import static android.Manifest.permission.READ_CONTACTS; 

/** 
* A login screen that offers login via email/password. 
*/ 
public class LoginActivity extends AppCompatActivity implements 
     LoaderCallbacks<Cursor>{ 

    /** 
    * Id to identity READ_CONTACTS permission request. 
    */ 
    private static final int REQUEST_READ_CONTACTS = 0; 

    /** 
    * A dummy authentication store containing known user names and passwords. 
    * TODO: remove after connecting to a real authentication system. 
    */ 
    private static final String[] DUMMY_CREDENTIALS = new String[]{ 
      "[email protected]:hello", "[email protected]:world" 
    }; 
    /** 
    * Keep track of the login task to ensure we can cancel it if requested. 
    */ 
    private UserLoginTask mAuthTask = null; 

    private static final String TAG = "LoginActivity"; 
    // UI references. 
    private AutoCompleteTextView mEmailView; 
    private EditText mPasswordView; 
    private View mProgressView; 
    private View mLoginFormView; 
    private SignInButton mGoogleButton; 
    private FirebaseAuth mAuth; 
    private FirebaseUser currentUser; 
    private GoogleApiClient mGoogleApiClient; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     // Set up the login form. 
     mEmailView = (AutoCompleteTextView) findViewById(R.id.email); 
     populateAutoComplete(); 

     mPasswordView = (EditText) findViewById(R.id.password); 
     mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { 
       if (id == R.id.login || id == EditorInfo.IME_NULL) { 
        attemptLogin(); 
        return true; 
       } 
       return false; 
      } 
     }); 

     Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button); 
     mEmailSignInButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       attemptLogin(); 
      } 
     }); 

     mLoginFormView = findViewById(R.id.login_form); 
     mProgressView = findViewById(R.id.login_progress); 
     mGoogleButton = (SignInButton) findViewById(R.id.google_button); 
     // mGoogleButton.setOnClickListener(new OnClickListener() { 
     // @Override 
     // public void onClick(View view) { 
     //  signIn(); 
     // } 
     // }); 
     mAuth = FirebaseAuth.getInstance(); 

     GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
       .requestEmail() 
       .build(); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() { 
        @Override 
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
         Toast.makeText(LoginActivity.this, "There is error", Toast.LENGTH_SHORT); 
        } 
       }) 
       .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
       .build(); 
    } 

    private void doLogin() { 
     // Check if user is signed in (non-null) and update UI accordingly. 
     currentUser = mAuth.getCurrentUser(); 
     if (currentUser != null) { 
      // TODO: Make new Intent to MainActivity 
      Intent main = new Intent(this, MainActivity.class); 
      startActivity(main); 
     } 
    } 

    private Boolean doRegister(String email, String password) { 
     final Boolean[] result = new Boolean[1]; 
     mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() { 
      @Override 
      public void onComplete(@NonNull Task<AuthResult> task) { 
       if (task.isSuccessful()) { 
        // Register success, do login 
        Log.d(TAG, "createUserWithEmail:success"); 
        doLogin(); 
        result[0] = true; 
       } else { 
        // If register fails, display a message to the user. 
        Log.w(TAG, "createUserWithEmail:failure", task.getException()); 
        Toast.makeText(LoginActivity.this, "Registration failed.", 
          Toast.LENGTH_SHORT).show(); 

        result[0] = false; 
       } 
      } 
     }); 
     return result[0]; 
    } 

    private void populateAutoComplete() { 
     if (!mayRequestContacts()) { 
      return; 
     } 

     getLoaderManager().initLoader(0, null, this); 
    } 


    private boolean mayRequestContacts() { 
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { 
      return true; 
     } 
     if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) { 
      return true; 
     } 
     if (shouldShowRequestPermissionRationale(READ_CONTACTS)) { 
      Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE) 
        .setAction(android.R.string.ok, new View.OnClickListener() { 
         @Override 
         @TargetApi(Build.VERSION_CODES.M) 
         public void onClick(View v) { 
          requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS); 
         } 
        }); 
     } else { 
      requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS); 
     } 
     return false; 
    } 

    /** 
    * Callback received when a permissions request has been completed. 
    */ 
    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 
              @NonNull int[] grantResults) { 
     if (requestCode == REQUEST_READ_CONTACTS) { 
      if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       populateAutoComplete(); 
      } 
     } 
    } 


    /** 
    * Attempts to sign in or register the account specified by the login form. 
    * If there are form errors (invalid email, missing fields, etc.), the 
    * errors are presented and no actual login attempt is made. 
    */ 
    private void attemptLogin() { 
     if (mAuthTask != null) { 
      return; 
     } 

     // Reset errors. 
     mEmailView.setError(null); 
     mPasswordView.setError(null); 

     // Store values at the time of the login attempt. 
     String email = mEmailView.getText().toString(); 
     String password = mPasswordView.getText().toString(); 

     boolean cancel = false; 
     View focusView = null; 

     // Check for a valid password, if the user entered one. 
     if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) { 
      mPasswordView.setError(getString(R.string.error_invalid_password)); 
      focusView = mPasswordView; 
      cancel = true; 
     } 

     // Check for a valid email address. 
     if (TextUtils.isEmpty(email)) { 
      mEmailView.setError(getString(R.string.error_field_required)); 
      focusView = mEmailView; 
      cancel = true; 
     } else if (!isEmailValid(email)) { 
      mEmailView.setError(getString(R.string.error_invalid_email)); 
      focusView = mEmailView; 
      cancel = true; 
     } 

     if (cancel) { 
      // There was an error; don't attempt login and focus the first 
      // form field with an error. 
      focusView.requestFocus(); 
     } else { 
      // Show a progress spinner, and kick off a background task to 
      // perform the user login attempt. 
      showProgress(true); 
      mAuthTask = new UserLoginTask(email, password); 
      mAuthTask.execute((Void) null); 
     } 
    } 

    private boolean isEmailValid(String email) { 
     //TODO: Replace this with your own logic 
     return email.contains("@"); 
    } 

    private boolean isPasswordValid(String password) { 
     //TODO: Replace this with your own logic 
     return password.length() > 4; 
    } 

    /** 
    * Shows the progress UI and hides the login form. 
    */ 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) 
    private void showProgress(final boolean show) { 
     // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow 
     // for very easy animations. If available, use these APIs to fade-in 
     // the progress spinner. 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { 
      int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime); 

      mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE); 
      mLoginFormView.animate().setDuration(shortAnimTime).alpha(
        show ? 0 : 1).setListener(new AnimatorListenerAdapter() { 
       @Override 
       public void onAnimationEnd(Animator animation) { 
        mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE); 
       } 
      }); 

      mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); 
      mProgressView.animate().setDuration(shortAnimTime).alpha(
        show ? 1 : 0).setListener(new AnimatorListenerAdapter() { 
       @Override 
       public void onAnimationEnd(Animator animation) { 
        mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); 
       } 
      }); 
     } else { 
      // The ViewPropertyAnimator APIs are not available, so simply show 
      // and hide the relevant UI components. 
      mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); 
      mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE); 
     } 
    } 

    @Override 
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { 
     return new CursorLoader(this, 
       // Retrieve data rows for the device user's 'profile' contact. 
       Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, 
         ContactsContract.Contacts.Data.CONTENT_DIRECTORY), ProfileQuery.PROJECTION, 

       // Select only email addresses. 
       ContactsContract.Contacts.Data.MIMETYPE + 
         " = ?", new String[]{ContactsContract.CommonDataKinds.Email 
       .CONTENT_ITEM_TYPE}, 

       // Show primary email addresses first. Note that there won't be 
       // a primary email address if the user hasn't specified one. 
       ContactsContract.Contacts.Data.IS_PRIMARY + " DESC"); 
    } 

    @Override 
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) { 
     List<String> emails = new ArrayList<>(); 
     cursor.moveToFirst(); 
     while (!cursor.isAfterLast()) { 
      emails.add(cursor.getString(ProfileQuery.ADDRESS)); 
      cursor.moveToNext(); 
     } 

     addEmailsToAutoComplete(emails); 
    } 

    @Override 
    public void onLoaderReset(Loader<Cursor> cursorLoader) { 

    } 

    private void addEmailsToAutoComplete(List<String> emailAddressCollection) { 
     //Create adapter to tell the AutoCompleteTextView what to show in its dropdown list. 
     ArrayAdapter<String> adapter = 
       new ArrayAdapter<>(LoginActivity.this, 
         android.R.layout.simple_dropdown_item_1line, emailAddressCollection); 

     mEmailView.setAdapter(adapter); 
    } 

    private interface ProfileQuery { 
     String[] PROJECTION = { 
       ContactsContract.CommonDataKinds.Email.ADDRESS, 
       ContactsContract.CommonDataKinds.Email.IS_PRIMARY, 
     }; 

     int ADDRESS = 0; 
     int IS_PRIMARY = 1; 
    } 

    /** 
    * Represents an asynchronous login/registration task used to authenticate 
    * the user. 
    */ 
    public class UserLoginTask extends AsyncTask<Void, Void, Boolean> { 
     private final static String TAG = "UserLogin"; 
     private final String mEmail; 
     private final String mPassword; 

     UserLoginTask(String email, String password) { 
      mEmail = email; 
      mPassword = password; 
     } 

     @Override 
     protected Boolean doInBackground(Void... params) { 
      final Boolean[] result = new Boolean[1]; 

      // TODO: attempt authentication against a network service. 
      mAuth.signInWithEmailAndPassword(mEmail, mPassword) 
        .addOnCompleteListener(new OnCompleteListener<AuthResult>() { 
         @Override 
         public void onComplete(@NonNull Task<AuthResult> task) { 
          if (task.isSuccessful()) { 
           // Sign in success, do login. 
           Log.d(TAG, "signInWithEmail:success"); 
           doLogin(); 
           result[0] = true; 
          } else { 
           // If sign in fails, display a message to the user and try register. 
           Log.w(TAG, "signInWithEmail:failure", task.getException()); 
           Toast.makeText(LoginActivity.this, "Authentication failed.", 
             Toast.LENGTH_SHORT).show(); 

           // TODO: register the new account here. 
           result[0] = doRegister(mEmail, mPassword); 
          } 
         } 
        }); 

      return result[0]; 
     } 

     @Override 
     protected void onPostExecute(final Boolean success) { 
      mAuthTask = null; 
      showProgress(false); 

      if (success) { 
       finish(); 
      } else { 
       mPasswordView.setError(getString(R.string.error_incorrect_password)); 
       mPasswordView.requestFocus(); 
      } 
     } 

     @Override 
     protected void onCancelled() { 
      mAuthTask = null; 
      showProgress(false); 
     } 

    } 

} 

這是我AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.dcangkring.mobile"> 

    <!-- To auto-complete the email text field in the login form with the user's emails --> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.READ_PROFILE" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/dcangkring_logo" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".LoginActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_main" 
      android:theme="@style/AppTheme.NoActionBar" /> 

    </application> 

</manifest> 

這是我build.gradle應用

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion '26.0.2' 
    defaultConfig { 
     applicationId "com.dcangkring.mobile" 
     minSdkVersion 23 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    compileOptions { 
     sourceCompatibility JavaVersion.VERSION_1_7 
     targetCompatibility JavaVersion.VERSION_1_7 
    } 
    productFlavors { 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    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:23.0.+' 
    compile 'com.android.support:support-v4:23.0.+' 
    compile 'com.android.support:design:23.0.+' 
    compile 'com.android.support.constraint:constraint-layout:1.0.2' 
    compile 'com.google.firebase:firebase-auth:10.0.1' 
    compile 'com.google.android.gms:play-services-auth:10.0.1' 
    testCompile 'junit:junit:4.12' 
} 


apply plugin: 'com.google.gms.google-services' 

UPDATE:

This is my logcat

+0

請添加日誌。 – msecilmis

+0

好的,等一下我會試試 – DewzZ

+0

它說:*「你需要在這個活動中使用Theme.AppCompat主題(或者後代)。」* –

回答

0

您需要閱讀logcat輸出 - 失敗的原因是有提及。

如果您在調查logcat輸出時遇到問題 - 您應該在此處張貼。對於瞭解哪裏出了問題,比發佈的源代碼更有價值。

0

您的SDK版本寫錯了。建議不要使用這種23.0。+作爲任何SDK,因爲Android Studio將繼續下載任何新版本的SDK,並且在某些情況下會在運行時帶來錯誤。 API 23的最新版本是23.5.1。

+0

錯誤:(34,13)無法解決:com.android.support:design:23.5.1 錯誤:(32,13)無法解析:com.android .support:appcompat-v7:23.5.1 – DewzZ

+0

支持庫版本..? –

+0

我認爲沒有錯誤。我應該向你展示我的'style.xml'嗎?我認爲它相關 – DewzZ

相關問題