简体   繁体   English

按下主页后,应用程序崩溃

[英]App crashes after home is pressed

I'm a newer in android app. 我是android应用的新手。 My app just uses fragments but when app is running, I pressed home button and the app was crashed by NullPointerException. 我的应用程序仅使用片段,但是当应用程序运行时,我按下了主页按钮,该应用程序因NullPointerException而崩溃。 I don't understand why. 我不明白为什么。 Please help me. 请帮我。 (Because of private reason I need to use API 19, android:sharedUserId="android.uid.system" and android:process="system") (由于私人原因,我需要使用API​​ 19,android:sharedUserId =“ android.uid.system”和android:process =“ system”)

I make a app that was singed as system with below permissions 我制作了一个具有以下权限的应用程序:

    $ adb shell dumpsys package jp.co.doexample.test.doexample
Activity Resolver Table:
  Non-Data Actions:
      android.intent.action.MAIN:
        a526b630 jp.co.doexample.test.doexample/.MainActivity filter a52973c0
          Action: "android.intent.action.MAIN"
          Category: "android.intent.category.LAUNCHER"

Key Set Manager:
  [jp.co.doexample.test.doexample]
      Signing KeySets: 1

Packages:
  Package [jp.co.doexample.test.doexample] (a50c27f8):
    userId=1000 gids=[]
    sharedUser=SharedUserSetting{a508f4d8 android.uid.system/1000}
    pkg=Package{a52af5a0 jp.co.doexample.test.doexample}
    codePath=/data/app/jp.co.doexample.test.doexample-2.apk
    resourcePath=/data/app/jp.co.doexample.test.doexample-2.apk
    nativeLibraryPath=/data/app-lib/jp.co.doexample.test.doexample-2
    versionCode=1 targetSdk=19
    versionName=1.0
    applicationInfo=ApplicationInfo{a52e5500 jp.co.doexample.test.doexample}
    flags=[ DEBUGGABLE HAS_CODE ALLOW_CLEAR_USER_DATA TEST_ONLY ALLOW_BACKUP ]
    dataDir=/data/data/jp.co.doexample.test.doexample
    supportsScreens=[small, medium, large, xlarge, resizeable, anyDensity]
    timeStamp=2018-02-19 04:05:27
    firstInstallTime=2018-02-19 00:50:12
    lastUpdateTime=2018-02-19 04:05:27
    signatures=PackageSignatures{a50544a0 [a50550b8]}
    permissionsFixed=true haveGids=true installStatus=1
    pkgFlags=[ DEBUGGABLE HAS_CODE ALLOW_CLEAR_USER_DATA TEST_ONLY ALLOW_BACKUP ]
    User 0:  installed=true blocked=false stopped=true notLaunched=false enabled=0

Shared users:
  SharedUser [android.uid.system] (a508f4d8):
    userId=1000 gids=[3003, 1028, 1015, 3002, 3001]
    grantedPermissions:
      android.permission.ACCESS_NOTIFICATIONS
      android.permission.LOCATION_HARDWARE
      android.permission.MASTER_CLEAR
      android.permission.CLEAR_APP_USER_DATA
      android.permission.READ_USER_DICTIONARY
      android.permission.READ_CONTACTS
      android.permission.CALL_PHONE
      android.permission.WRITE_CONTACTS
      android.permission.BACKUP
      android.permission.READ_PHONE_STATE
      android.permission.ACCESS_WIMAX_STATE
      android.permission.MANAGE_USERS
      android.permission.SET_TIME
      android.permission.ACCESS_FINE_LOCATION
      android.permission.MANAGE_DEVICE_ADMINS
      android.permission.ACCESS_NETWORK_STATE
      android.permission.USE_CREDENTIALS
      android.permission.SET_POINTER_SPEED
      android.permission.INTERACT_ACROSS_USERS_FULL
      android.permission.CONFIGURE_WIFI_DISPLAY
      android.permission.MOVE_PACKAGE
      android.permission.HARDWARE_TEST
      android.permission.WRITE_SECURE_SETTINGS
      android.permission.ACCESS_CHECKIN_PROPERTIES
      android.permission.FORCE_STOP_PACKAGES
      android.permission.MODIFY_AUDIO_SETTINGS
      android.permission.READ_SYNC_STATS
      android.permission.INSTALL_LOCATION_PROVIDER
      android.permission.WRITE_SYNC_SETTINGS
      android.permission.MODIFY_PHONE_STATE
      android.permission.INTERNET
      android.permission.BLUETOOTH
      android.permission.WRITE_SETTINGS
      android.permission.CONFIRM_FULL_BACKUP
      com.android.launcher.permission.READ_SETTINGS
      android.permission.PACKAGE_USAGE_STATS
      android.permission.BLUETOOTH_ADMIN
      android.permission.DEVICE_POWER
      com.android.launcher.permission.WRITE_SETTINGS
      android.permission.CHANGE_CONFIGURATION
      android.permission.READ_EXTERNAL_STORAGE
      android.permission.WRITE_EXTERNAL_STORAGE
      android.permission.REBOOT
      android.intent.category.MASTER_CLEAR.permission.C2D_MESSAGE
      android.permission.ACCESS_WIFI_STATE
      android.permission.STATUS_BAR
      android.permission.ACCESS_COARSE_LOCATION
      com.google.android.googleapps.permission.GOOGLE_AUTH
      android.permission.MOUNT_UNMOUNT_FILESYSTEMS
      android.permission.BLUETOOTH_PRIVILEGED
      android.permission.WRITE_USER_DICTIONARY
      android.permission.ACCESS_DOWNLOAD_MANAGER
      android.permission.SET_KEYBOARD_LAYOUT
      android.permission.MANAGE_USB
      android.permission.CHANGE_WIMAX_STATE
      android.permission.NFC
      android.permission.WRITE_APN_SETTINGS
      android.permission.BATTERY_STATS
      com.android.certinstaller.INSTALL_AS_USER
      android.permission.READ_PROFILE
      android.permission.CHANGE_WIFI_STATE
      android.permission.INTERACT_ACROSS_USERS
      android.permission.READ_SYNC_SETTINGS
      android.permission.VIBRATE
      android.permission.COPY_PROTECTED_DATA

Built information in gradle(app) Gradle中的内置信息(应用程序)

    ....
defaultConfig {
        applicationId "jp.co.doexample.test.doexample"
        minSdkVersion 19
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
...

AndroidManifest.xml AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:sharedUserId="android.uid.system"
    package="jp.co.doexample.test.doexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:process="system"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java MainActivity.java

package jp.co.doexample.test.doexample;

import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;

public class MainActivity extends FragmentActivity implements TopFragment.OnFragmentInteractionListener, MainFragment.OnFragmentInteractionListener {


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

        if (savedInstanceState == null) {

            TopFragment fragmentTop = TopFragment.newInstance("PARAM1", "PARAM2");
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(R.id.top_bar_container, fragmentTop, "TopBarFragment");

            MainFragment mainFragment = MainFragment.newInstance("PARAM1", "PARAM2");
            fragmentTransaction.add(R.id.main_container, mainFragment, "MainFragment");
            fragmentTransaction.commit();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }


    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}

activity_main.xml activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context="jp.co.doexample.test.doexample.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/top_bar_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="100" />

        <FrameLayout
            android:id="@+id/main_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="900" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

I kept TopFragment.java and MainFragment.java the same as they was generated by fragment(Blank) from AndroidStuio. 我将TopFragment.java和MainFragment.java保持与AndroidStuio的fragment(Blank)生成的相同。

When app is crashed when home is pressed. 当按下Home键时应用崩溃。 FATAL EXCEPTION IN SYSTEM PROCESS: main java.lang.NullPointerException was thrown as below: 系统过程中的致命异常:引发了主要java.lang.NullPointerException,如下所示:

02-19 04:05:29.681 1960-2541/com.google.android.gms I/Icing: Indexing 491ABC3DAB0EED407E71F3C01527BAA56EADC801 from com.google.android.gms
02-19 04:05:29.681 1960-2541/com.google.android.gms I/Icing: Indexing done 491ABC3DAB0EED407E71F3C01527BAA56EADC801
02-19 04:05:37.511 1618-1757/system_process I/ActivityManager: Killing 2343:com.android.exchange/u0a27 (adj 15): empty #17
02-19 04:05:37.761 1618-1683/system_process I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher} from pid 1618
02-19 04:05:37.771 1960-1960/com.google.android.gms I/CastMirroringProvider: onDiscoveryModeChanged, mode=0
02-19 04:05:37.771 1960-2541/com.google.android.gms I/CastMirroringProvider: onDiscoveryModeChanged removing all remote displays
02-19 04:05:37.771 1168-1220/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 565248


                                                 [ 02-19 04:05:37.771  1618: 1683 D/         ]
                                                 HostConnection::get() New Host Connection established 0xb8f1a7f0, tid 1683
02-19 04:05:37.811 1618-1683/system_process D/dalvikvm: GC_FOR_ALLOC freed 536K, 18% free 8380K/10176K, paused 8ms, total 8ms
02-19 04:05:37.811 1618-1683/system_process I/dalvikvm-heap: Grow heap (frag case) to 8.767MB for 496812-byte allocation
02-19 04:05:37.821 1618-1683/system_process D/dalvikvm: GC_FOR_ALLOC freed <1K, 17% free 8865K/10664K, paused 8ms, total 8ms
02-19 04:05:37.831 1618-1683/system_process D/dalvikvm: GC_FOR_ALLOC freed 16K, 18% free 8849K/10664K, paused 13ms, total 13ms
02-19 04:05:37.831 1618-1683/system_process I/dalvikvm-heap: Grow heap (frag case) to 9.225MB for 496812-byte allocation
02-19 04:05:37.851 1618-1683/system_process D/dalvikvm: GC_FOR_ALLOC freed <1K, 17% free 9334K/11152K, paused 10ms, total 10ms
02-19 04:05:37.851 1960-2541/com.google.android.gms I/DiscoveryManager: Filter criteria(null) scannerFlags(0)
02-19 04:05:37.851 1618-1695/system_process E/WifiStateMachine: Error! unhandled message{ when=-1ms what=131156 target=com.android.internal.util.StateMachine$SmHandler }
02-19 04:05:37.861 1831-1831/com.android.launcher D/EGL_emulation: eglMakeCurrent: 0xb8e455b0: ver 2 0
02-19 04:05:37.911 1831-1831/com.android.launcher D/EGL_emulation: eglMakeCurrent: 0xb8e455b0: ver 2 0
02-19 04:05:37.911 1831-1831/com.android.launcher E/EGL_emulation: tid 1831: eglSurfaceAttrib(1199): error 0x3009 (EGL_BAD_MATCH)
02-19 04:05:37.911 1831-1831/com.android.launcher W/HardwareRenderer: Backbuffer cannot be preserved
02-19 04:05:38.461 1618-1618/? D/AndroidRuntime: Shutting down VM
02-19 04:05:38.461 1618-1618/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4ccdb20)
02-19 04:05:38.461 1618-1618/? I/Process: Sending signal. PID: 1618 SIG: 9
02-19 04:05:38.461 1618-1618/? E/AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: main
                                                 java.lang.NullPointerException
                                                     at android.os.Bundle.hasFileDescriptors(Bundle.java:383)
                                                     at android.os.Bundle.describeContents(Bundle.java:1658)
                                                     at android.os.Bundle.hasFileDescriptors(Bundle.java:365)
                                                     at com.android.server.am.ActivityManagerService.activityStopped(ActivityManagerService.java:5247)
                                                     at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3098)
                                                     at android.os.Handler.handleCallback(Handler.java:733)
                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                     at android.os.Looper.loop(Looper.java:136)
                                                     at com.android.server.ServerThread.initAndLoop(SystemServer.java:1093)
                                                     at com.android.server.SystemServer.main(SystemServer.java:1179)
                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                     at dalvik.system.NativeStart.main(Native Method)
02-19 04:05:38.471 1171-1171/? W/AudioFlinger: power manager service died !!!
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'user' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'sensorservice' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'power' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'gfxinfo' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'vibrator' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'hardware' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'consumer_ir' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'cpuinfo' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'permission' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'batterystats' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'usagestats' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'appops' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'display' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'telephony.registry' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'scheduling_policy' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'account' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'dbinfo' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'activity' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'procstats' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'meminfo' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'entropy' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'content' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'battery' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'package' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'alarm' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'window' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'input' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'audio' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'netpolicy' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'lock_settings' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'device_policy' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'wifip2p' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'statusbar' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'clipboard' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'network_management' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'textservices' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'netstats' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'input_method' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'mount' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'accessibility' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'location' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'connectivity' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'devicestoragemonitor' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'country_detector' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'updatelock' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'servicediscovery' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'dropbox' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'search' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'wallpaper' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'notification' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'wifi' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'usb' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'serial' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'uimode' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'backup' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'appwidget' died
02-19 04:05:38.471 1813-1948/com.android.phone E/WifiManager: Channel connection lost
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'diskstats' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'samplingprofiler' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'commontime_management' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'dreams' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'assetatlas' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'print' died
02-19 04:05:38.471 1162-1162/? I/ServiceManager: service 'media_router' died
02-19 04:05:38.471 1777-1788/com.google.android.gms.persistent W/Sensors: sensorservice died [0xb8e88d70]

If I remove super.onSaveInstanceState(outState); 如果我删除super.onSaveInstanceState(outState); from main activity or android:process="system" from AndroidManifest.xml, it will run as normal. 来自主要活动或来自AndroidManifest.xml中的android:process =“ system”,它将正常运行。 I don`t know why please help me. 我不知道为什么请帮助我。

Edit1: The place where NPE was thrown: https://android.googlesource.com/platform/frameworks/base/+/android-4.4.4_r2.0.1/services/java/com/android/server/am/ActivityManagerService.java#5247 Edit1:引发NPE的地方: https ://android.googlesource.com/platform/frameworks/base/+/android-4.4.4_r2.0.1/services/java/com/android/server/am/ActivityManagerService.java #5247

Edit2: my example source https://github.com/hanglt6174/DoExample Edit2:我的示例源https://github.com/hanglt6174/DoExample

I tried another way to initialize outState again following Mr.Trần Đức Tâm advice as below and the app run ok. 我按照下面的Tr tonĐứcTâm的建议再次尝试了另一种初始化outState的方法,应用运行正常。

 if (outState.isEmpty()) {
    outState = new Bundle();
 }

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

相关问题 如果按下主屏幕按钮而没有播放音频,则应用程序崩溃 - If home button is pressed without playing the audio, then the app crashes 按下“主页”按钮并按下图标(在启动器上)后,应用程序重新启动 - After Home button pressed and pressed icon (on launcher) app restart 按下主页按钮后重新启动应用程序时,不会调用 onResume - onResume is not called when I relaunch the app after home button pressed 按下主页按钮并返回应用程序后如何恢复以前的活动 - How to restore previous activity after pressed home button and return to the app 按下“主页”后杀死应用程序(停止暂停即可停止应用程序) - Kill app after 'Home' is pressed (stop it from just pausing) android手机按下home键后如何避免应用重启 - How to avoid app restart after pressed home button in android phone 按下主屏幕按钮但方向更改正常时,Android应用程序崩溃 - Android app crashes when home button is pressed but orientation change works fine 按下主页按钮后重新进入时,Android 应用程序崩溃。 这是代码 - Android application crashes when re-entering after the home button is pressed. This is the code 按下BackButton时应用崩溃 - App crashes when BackButton is pressed 按下按钮时,应用程序崩溃 - When the button is pressed, the app crashes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM