2017-10-06 2 views
-1

Ich möchte eine App in Android API 23 (Marshmallow) entwickeln, aber ich weiß nicht, warum meine App stürzt ab, wenn ich auf dem Gerät oder Emulator laufen. Ich bekomme bisher keinen Kompilierfehler.Warum stürzen Apps beim Ausführen auf Emulator oder Device Android API 23 ab?

Zuerst denke ich, das Problem ist auf die Abhängigkeiten und Gradle-Konfiguration, aber ich habe versucht, Abhängigkeiten zu ändern, aber es macht mehr Fehler beim Kompilieren oder Gradle-Fehler. Als ich versuchte, meine buildToolsVersion auf 23 zu ändern, bekam ich so viele Fehler wie srcCompat und andere.

Kann jemand darauf hinweisen, wo habe ich die falsche Konfiguration oder Code? Danke, übrigens.

das ist mein 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); 
     } 

    } 

} 

das ist mein 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> 

das ist mein build.gradle App

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

Bitte fügen Sie das Protokoll hinzu. – msecilmis

+0

ok, warte, ich versuche – DewzZ

+0

Es heißt: * "Sie müssen ein Theme.AppCompat Thema (oder Nachkommen) mit dieser Aktivität verwenden." * –

Antwort

0

Sie müssen logcat Ausgabe lesen - die Störungsursache genannt wird.

Falls Sie Probleme bei der Untersuchung der Ausgabe logcat haben, sollten Sie es hier veröffentlichen. Es ist wertvoller für das Verständnis, was falsch gelaufen ist, mehr als der Quellcode, den Sie gepostet haben.

0

Ihre SDK-Version wurde falsch geschrieben. Die Verwendung von 23.0. + Für beliebige SDk wird nicht empfohlen, da Android Studio immer eine neue Version des SDK herunterlädt und in einigen Fällen Fehler während der Laufzeit verursacht. Die neueste Version für API 23 ist 23.5.1.

+0

Fehler: (34, 13) Fehler beim Auflösen: com.android.support:design:23.5.1 Fehler: (32, 13) Fehler beim Auflösen: com.android .support: appcompat-v7: 23.5.1 – DewzZ

+0

Was ist mit der Support-Bibliothek Version ..? –

+0

kein Fehler, denke ich. Soll ich dir meine 'style.xml' zeigen? Ich denke, es ist verwandt – DewzZ

Verwandte Themen