不幸的是应用停止

时间:2015-08-01 18:47:45

标签: android android-studio

public class LoginActivity extends Activity implements LoaderCallbacks<Cursor> {

    private static final String DUMMY_CREDENTIALS = "user@test.com:hello";

    private UserLoginTask userLoginTask = null;
    private View loginFormView;
    private View progressView;
    private AutoCompleteTextView emailTextView;
    private EditText passwordTextView;
    private TextView signUpTextView;

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

        emailTextView = (AutoCompleteTextView) findViewById(R.id.email);
        loadAutoComplete();

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

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

        loginFormView = findViewById(R.id.login_form);
        progressView = findViewById(R.id.login_progress);

        //adding underline and link to signup textview
        signUpTextView = (TextView) findViewById(R.id.signUpTextView);
        signUpTextView.setPaintFlags(signUpTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        Linkify.addLinks(signUpTextView, Linkify.ALL);

        signUpTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("LoginActivity", "Sign Up Activity activated.");
                // this is where you should start the signup Activity
                // LoginActivity.this.startActivity(new Intent(LoginActivity.this, SignupActivity.class));
            }
        });
    }

    private void loadAutoComplete() {
        getLoaderManager().initLoader(0, null, this);
    }


    /**
     * Validate Login form and authenticate.
     */
    public void initLogin() {
        if (userLoginTask != null) {
            return;
        }

        emailTextView.setError(null);
        passwordTextView.setError(null);

        String email = emailTextView.getText().toString();
        String password = passwordTextView.getText().toString();

        boolean cancelLogin = false;
        View focusView = null;

        if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
            passwordTextView.setError(getString(R.string.invalid_password));
            focusView = passwordTextView;
            cancelLogin = true;
        }

        if (TextUtils.isEmpty(email)) {
            emailTextView.setError(getString(R.string.field_required));
            focusView = emailTextView;
            cancelLogin = true;
        } else if (!isEmailValid(email)) {
            emailTextView.setError(getString(R.string.invalid_email));
            focusView = emailTextView;
            cancelLogin = true;
        }

        if (cancelLogin) {
            // error in login
            focusView.requestFocus();
        } else {
            // show progress spinner, and start background task to login
            showProgress(true);
            userLoginTask = new UserLoginTask(email, password);
            userLoginTask.execute((Void) null);
        }
    }

    private boolean isEmailValid(String email) {
        //add your own logic
        return email.contains("@");
    }

    private boolean isPasswordValid(String password) {
        //add your own logic
        return password.length() > 4;
    }

    /**
     * Shows the progress UI and hides the login form.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    public 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);

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

            progressView.setVisibility(show ? View.VISIBLE : View.GONE);
            progressView.animate().setDuration(shortAnimTime).alpha(
                    show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    progressView.setVisibility(show ? View.VISIBLE : View.GONE);
                }
            });
        } else {
            // The ViewPropertyAnimator APIs are not available, so simply show
            // and hide the relevant UI components.
            progressView.setVisibility(show ? View.VISIBLE : View.GONE);
            loginFormView.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<String>();
        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<String>(LoginActivity.this,
                        android.R.layout.simple_dropdown_item_1line, emailAddressCollection);

        emailTextView.setAdapter(adapter);
    }


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

        int ADDRESS = 0;
        int IS_PRIMARY = 1;
    }

    /**
     * Async Login Task to authenticate
     */
    public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

        private final String emailStr;
        private final String passwordStr;

        UserLoginTask(String email, String password) {
            emailStr = email;
            passwordStr = password;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            //this is where you should write your authentication code
            // or call external service
            // following try-catch just simulates network access
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                return false;
            }

            //using a local dummy credentials store to authenticate
            String[] pieces = DUMMY_CREDENTIALS.split(":");
            if (pieces[0].equals(emailStr) && pieces[1].equals(passwordStr)) {
                return true;
            } else {
                return false;
            }
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            userLoginTask = null;
            //stop the progress spinner
            showProgress(false);

            if (success) {
                //  login success and move to main Activity here.
            } else {
                // login failure
                passwordTextView.setError(getString(R.string.incorrect_password));
                passwordTextView.requestFocus();
            }
        }

        @Override
        protected void onCancelled() {
            userLoginTask = null;
            show Progress(false);
        }

。在运行代码之后,它会抛出异常:

  

08-01 20:45:09.682 2253-2297 / com.example.abhisheksharma1.bridgeapp   E / AndroidRuntime:致命异常:AsyncTask#1流程:   com.example.abhisheksharma1.bridgeapp,PID:2253   java.lang.RuntimeException:执行时发生错误   doInBackground()在android.os.AsyncTask $ 3.done(AsyncTask.java:300)at at   java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)   at java.util.concurrent.FutureTask.setException(FutureTask.java:222)   在java.util.concurrent.FutureTask.run(FutureTask.java:242)at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)   在java.lang.Thread.run(Thread.java:818)引起:   java.lang.SecurityException:Permission Denial:打开提供者   来自的com.android.providers.contacts.ContactsProvider2   ProcessRecord {1f2048ab   2253:com.example.abhisheksharma1.bridgeapp / u0a50}(pid = 2253,   uid = 10050)需要android.permission.READ_CONTACTS或   android.permission.WRITE_CONTACTS在   android.os.Parcel.readException(Parcel.java:1540)at   android.os.Parcel.readException(Parcel.java:1493)at   android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3267)   在   android.app.ActivityThread.acquireProvider(ActivityThread.java:4589)   在   android.app.ContextImpl $ ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2439)   在   android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1442)   在android.content.ContentResolver.query(ContentResolver.java:462)at   android.content.CursorLoader.loadInBackground(CursorLoader.java:64)at   android.content.CursorLoader.loadInBackground(CursorLoader.java:42)at   android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)   在   android.content.AsyncTaskLoader $ LoadTask.doInBackground(AsyncTaskLoader.java:69)   在   android.content.AsyncTaskLoader $ LoadTask.doInBackground(AsyncTaskLoader.java:57)   在android.os.AsyncTask $ 2.call(AsyncTask.java:288)at   java.util.concurrent.FutureTask.run(FutureTask.java:237)
  在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)   在java.lang.Thread.run(Thread.java:818)

信息:Gradle任务[:app:assembleDebug] :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :app:compileDebugNdk UP-TO-DATE :应用:checkDebugManifest :app:preReleaseBuild UP-TO-DATE :app:prepareComAndroidSupportAppcompatV72200Library UP-TO-DATE :app:prepareComAndroidSupportSupportV42200Library UP-TO-DATE :应用:prepareDebugDependencies :app:compileDebugAidl UP-TO-DATE :app:compileDebugRenderscript UP-TO-DATE :app:generateDebugBuildConfig UP-TO-DATE :app:generateDebugAssets UP-TO-DATE :app:mergeDebugAssets UP-TO-DATE :app:generateDebugResValues UP-TO-DATE :app:generateDebugResources UP-TO-DATE :app:mergeDebugResources UP-TO-DATE :app:processDebugManifest UP-TO-DATE :应用:processDebugResources C:\ Users \用户Abhishesk.Sharma1 \ AndroidStudioProjects \ BridgeApp \程序\编译\ \中间体舱单\全\调试\ AndroidManifest.xml中 错误:(42,28)找不到与给定名称匹配的资源(值为&#39;值为&#39; @ integer / google_play_services_version&#39;)。 错误:任务&#39;:app:processDebugResources&#39;执行失败。

  

com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException:进程&#39;命令&#39; C:\ Users \ Abhishesk.Sharma1 \ AppData \ Local \ Android \ SDK \构建工具\ 21.1.2 \ aapt.exe&#39;&#39;完成非零退出值1   信息:建筑失败   信息:总时间:3.495秒   信息:2个错误   信息:0警告   信息:请参阅控制台中的完整输出

2 个答案:

答案 0 :(得分:1)

在AndroidManifest.xml中添加以下权限

<uses-permission android:name="android.permission.READ_CONTACTS " />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

答案 1 :(得分:0)

查看堆栈跟踪我注意到您必须在清单上设置此权限:

android.permission.READ_CONTACTS
android.permission.WRITE_CONTACTS
  

引起:java.lang.SecurityException:权限拒绝:打开   提供者com.android.providers.contacts.ContactsProvider2来自   ProcessRecord {1f2048ab   2253:com.example.abhisheksharma1.bridgeapp / u0a50}(pid = 2253,   uid = 10050)需要android.permission.READ_CONTACTS或   android.permission.WRITE_CONTACTS