我过去制作过一些应用程序,但都没有这个问题。它在Android Studio中进行调试时有效,但是如果我停止调试并尝试直接从手机中打开应用程序(已通过调试将其安装后),我会收到“[应用程序名称]”已停止工作的信息”

表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.[HIDDEN].[HIDDEN]">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <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"
        android:fullBackupContent="@xml/backup_descriptor">
        <activity
            android:name=".MainActivity"
            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=".WebActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <service
        android:name=".[HIDDEN]FirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
    </application>
</manifest>

Gradle(项目)
buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath 'com.google.gms:google-services:4.0.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

摇篮(应用程式)
apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.[HIDDEN].[HIDDEN]"
        minSdkVersion 22
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner
        "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false // keeps unused methods instead of removing them
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.google.firebase:firebase-messaging:17.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.2'
    api 'com.google.android.gms:play-services-gcm:15.0.1'
    api 'com.google.firebase:firebase-core:16.0.1'
    api 'com.google.firebase:firebase-messaging:17.1.0'
}

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

主要 Activity
package com.[HIDDEN].[HIDDEN];

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdReceiver;

import org.json.JSONStringer;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    private Button mOpen_uOrder;

    private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }
    };

    private void mOpen_uOrderClicked() {
        Intent intent = new Intent(this, WebActivity.class);
        startActivity(intent);
    }



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

        mTextMessage = (TextView) findViewById(R.id.message);
        mOpen_uOrder = (Button) findViewById(R.id.open_uOrder);
        mOpen_uOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOpen_uOrderClicked();
            }
        });
        BottomNavigationView navigation = (BottomNavigationView)
findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            //TODO: Open activity specified in key `click_action`
            Log.println(Log.ASSERT, "Click Action: ",
bundle.getString("click_action"));
        }

        [HIDDEN]FirebaseMessagingService service = new
[HIDDEN]FirebaseMessagingService();

    }
}

最佳答案

我必须用困难的方式做,并注释掉MainActivity上的所有内容,然后逐行取消注释1行,安装apk,然后从我的手机打开以查看引起错误的行,这就是:

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    //TODO: Open activity specified in key `click_action`
    // THE CULPRIT:::Apparently you cannot have log statements in the app when not debugging?
    // Because bundle.getString("click_action") works.
    Log.println(Log.ASSERT, "Click Action: ", bundle.getString("click_action"));
}

关于android - 未处于 Debug模式时,Android应用程序崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51637455/

10-13 02:01