简体   繁体   English

应用程序在启动时崩溃

[英]Application crashes on startup

I am creating an android application.我正在创建一个 android 应用程序。 However once I click its icon, it crashes immediatelly.但是,一旦我单击它的图标,它就会立即崩溃。

Here is the code:这是代码:

activity_main.xml activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@color/accent_blue"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/login_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Log In"
        android:textSize="50sp"
        android:padding="40dp"
        android:textColor="@color/black"
        android:background="@color/white"
        android:textAlignment="center"
        app:layout_constraintBottom_toTopOf="@id/username_label"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/username_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Username"
        android:textColor="@color/black"
        android:textSize="24sp"
        android:layout_marginTop="50dp"
        android:layout_marginStart="50dp"
        app:layout_constraintBottom_toTopOf="@id/username_text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/login_header" />

    <TextView
        android:id="@+id/password_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Password"
        android:textColor="@color/black"
        android:textSize="24sp"
        android:layout_marginTop="40dp"
        android:layout_marginStart="50dp"
        app:layout_constraintBottom_toTopOf="@id/password_text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/username_text" />

    <EditText
        android:id="@+id/username_text"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="Enter email address"
        android:inputType="textEmailAddress"
        android:layout_marginStart="70dp"
        android:layout_marginEnd="50dp"
        app:layout_constraintTop_toBottomOf="@id/username_label"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toTopOf="@id/password_label"/>

    <EditText
        android:id="@+id/password_text"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="Enter password"
        android:inputType="textPassword"
        android:layout_marginStart="70dp"
        android:layout_marginEnd="50dp"
        app:layout_constraintTop_toBottomOf="@id/password_label"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toTopOf="@id/btn_sign_in"/>

    <Button
        android:id="@+id/btn_sign_in"
        android:layout_height="wrap_content"
        android:layout_width="200dp"
        android:text="Sign In"
        android:textSize="24sp"
        android:layout_marginBottom="200dp"
        android:layout_marginTop="80dp"
        android:onClick="signInUser"
        android:background="@drawable/btn_rounded"
        app:layout_constraintTop_toBottomOf="@id/password_text"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java MainActivity.java

package com.genius.nibebe;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

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;

public class MainActivity extends AppCompatActivity {

    EditText username, password;
    FirebaseAuth auth;
    ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if(user !=null){
            Intent i = new Intent(MainActivity.this,MapActivity.class);
            startActivity(i);
            finish();
        } else {
            setContentView(R.layout.activity_main);
        }

        username = findViewById(R.id.username_text);
        password = findViewById(R.id.password_text);
        auth = FirebaseAuth.getInstance();
        dialog = new ProgressDialog(this);

    }

    public void signInUser(View v){
        dialog.setMessage("Signing in. Please wait");
        dialog.show();

        if (username.getText().toString().equals("") && password.getText().toString().equals("")){
            Toast.makeText(getApplicationContext(),"Fields cannot be empty",Toast.LENGTH_SHORT).show();
        } else {
            auth.signInWithEmailAndPassword(username.getText().toString(),password.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()){
                        dialog.hide();
                        Toast.makeText(getApplicationContext(),"User successfully signed in!!!",Toast.LENGTH_SHORT).show();
                        Intent i = new Intent(MainActivity.this,MapActivity.class);
                        startActivity(i);
                        finish();
                    } else {
                        dialog.hide();
                        Toast.makeText(getApplicationContext(),"User not found",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

}

AndroidManifest.xml AndroidManifest.xml

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

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.
    -->
    <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">

        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/.
        -->
        <activity
            android:name=".MapActivity"
            android:label="@string/title_activity_map"></activity>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
    </application>

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <dist:module dist:instant="true" />

</manifest>

EDIT编辑

logcat日志猫

11-19 10:05:28.277 25221-25221/? W/System: ClassLoader referenced unknown path: /data/app/com.genius.nibebe-2/lib/arm
11-19 10:05:28.734 25221-25221/com.genius.nibebe W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.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
11-19 10:05:28.880 25221-25221/com.genius.nibebe I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
11-19 10:05:28.881 25221-25221/com.genius.nibebe I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
11-19 10:05:29.196 25221-25221/com.genius.nibebe D/AndroidRuntime: Shutting down VM
11-19 10:05:29.249 25221-25232/com.genius.nibebe I/art: Background sticky concurrent mark sweep GC freed 3836(479KB) AllocSpace objects, 1(20KB) LOS objects, 6% free, 4MB/4MB, paused 405us total 191.356ms
11-19 10:05:29.521 25221-25221/com.genius.nibebe I/Process: Sending signal. PID: 25221 SIG: 9

When I execute, it shows a white screen for a brief moment then crashes.当我执行时,它会短暂显示白屏然后崩溃。 Please help.请帮忙。 Have I made any error in the codes below.我在下面的代码中是否犯了任何错误。 Thanks in advance提前致谢

The setContentView should init early. setContentView 应该尽早初始化。

Try this.尝试这个。

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = findViewById(R.id.username_text);
        password = findViewById(R.id.password_text);
        dialog = new ProgressDialog(MainActivity.this);

        //Init Firebase
        auth = FirebaseAuth.getInstance();
        FirebaseUser user = auth.getCurrentUser();
        if(user != null){
            Intent i = new Intent(MainActivity.this,MapActivity.class);
            startActivity(i);
            finish();
        } else {
            //Do something or just ignore
        }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM