在onCreate()中日志语句没有被调用

7
我在我的onCreate()方法中有一系列的Log语句用于调试,但有时它们不会在我的LogCat中打印出来。有人知道这是什么原因吗?
也许我没有理解Activity生命周期的基础知识。我已经访问了开发者页面,但还是不明白。
让我澄清一下,如果我卸载应用程序,然后再从Eclipse启动它,Log调用将显示出来。但是,我正在运行测试,以查看应用程序在已经运行后的功能。
我有一个MainMenu Activity,它与多个其他活动相关。当我卸载应用并重新启动时,它会打印出我期望的内容。然后,我进入设备设置并“强制停止”应用程序(这样做对吗?)。之后,我再次打开我的应用程序。在onCreate()方法中没有Log消息,但是当我移动到另一个Activity时,我的MainMenu的onPause()和onStop()方法有正确打印到LogCat的Log消息。
有什么想法吗?
编辑:
我想补充一点,在重新启动时,onStart()方法中的Log消息也被跳过了。
如果您感兴趣,这是我的onCreate/onStart/onPause/onStop方法。其中有很多重复的代码,这是为了尝试找出应用程序重新启动时调用的方法。不应该有影响,我觉得这是我重新启动应用程序的方法有问题。
public void onCreate(Bundle savedInstanceState) {
    // This calls all inherited methods, as this is a subclass of Activity.
    super.onCreate(savedInstanceState);
    if(D) Log.e(TAG, "+++ ON CREATE +++");
    Log.i( TAG, "Whats going onnnn" );


    // Set the view the main.xml
    setContentView(R.layout.main);
    RelayAPIModel.bluetoothConnected = false;
    // Initialize the connection.
    setupConnection();
    Log.i( TAG, "Whats going onnnn2" );

    // Check how if bluetooth is enabled on this device.
    mService.checkBluetoothState();
    // Initialize stuff from PilotMain() method
    initMain();
    Log.i( TAG, "Whats going onnnn3" );
    // Add listeners to all of the buttons described in main.xml
    buildButtons();
    Log.i( "HERE", "HERE" );
    // If the adapter is null, then Bluetooth is not supported
    if (mService.getAdapter() == null) {
        Toast.makeText(this, R.string.toast_bt_not_avail, Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    savedStuff = (SerializableObjects)LocalObjects.readObjectFromFile( getApplicationContext(), "LastDevice.txt" );
    if( savedStuff != null ) {
        hasLastDevice = true;
        Log.i( "HAS", "LAST DEVICE" );
        Log.i( "HAS", savedStuff.getName() );
    } else {
        hasLastDevice = false;
        Log.i( "HAS NO", "LAST DEVICE" );
    }

    pairedDeviceList = new ArrayList<BluetoothDevice>();
    pairedDevices = mService.getAdapter().getBondedDevices();

    for( BluetoothDevice device: pairedDevices ) {
        pairedDeviceList.add( device );
    }
    if( hasLastDevice ) {
        for( int i = 0; i < pairedDeviceList.size(); i++ ) {
            Log.i( "1 HERE HERE", pairedDeviceList.get( i ).getName() );
            Log.i( "1 HEUH?I@JD", savedStuff.getName() );
            if( pairedDeviceList.get( i ).getName().equals( savedStuff.getRealName() ) ) {
                // THIS IS THE DEVICE WE NEED
                previousDevice = pairedDeviceList.get( i );
                i = pairedDeviceList.size();
            }
        }
    }

}

onStart()

 public void onStart() {
    super.onStart();
    if(D) Log.e(TAG, "++ ON START ++");



    savedStuff = (SerializableObjects)LocalObjects.readObjectFromFile( getApplicationContext(), "LastDevice.txt" );
    if( savedStuff != null ) {
        hasLastDevice = true;
        Log.i( "HAS", "LAST DEVICE" );
        Log.i( "HAS", savedStuff.getName() );
    } else {
        hasLastDevice = false;
        Log.i( "HAS NO", "LAST DEVICE" );
    }

    pairedDeviceList = new ArrayList<BluetoothDevice>();
    pairedDevices = mService.getAdapter().getBondedDevices();

    for( BluetoothDevice device: pairedDevices ) {
        pairedDeviceList.add( device );
    }
    if( hasLastDevice ) {
        for( int i = 0; i < pairedDeviceList.size(); i++ ) {
            Log.i( "2 HERE HERE", pairedDeviceList.get( i ).getName() );
            Log.i( "2 HEUH?I@JD", savedStuff.getName() );
            if( pairedDeviceList.get( i ).getName().equals( savedStuff.getRealName() ) ) {
                // THIS IS THE DEVICE WE NEED
                previousDevice = pairedDeviceList.get( i );
                i = pairedDeviceList.size();
            }
        }
    }


    // If BT is not on, request that it be enabled.
    // setupChat() will then be called during onActivityResult
    if (!mService.getAdapter().isEnabled()) {
        Log.i( TAG, "first !isEnabled " );
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        Log.i( TAG, "second !isEnabled" );
    // Otherwise, setup the connection
    } else {
        if (mService == null) {
            Log.i( TAG, "setupConnection BEFORE" );
            setupConnection();
            Log.i( TAG, "setupConnection AFTER" );
        }
    }
}

onPause()

@Override
public synchronized void onPause() {
    super.onPause();
    if(D) Log.e(TAG, "- ON PAUSE -");


    savedStuff = (SerializableObjects)LocalObjects.readObjectFromFile( getApplicationContext(), "LastDevice.txt" );
    if( savedStuff != null ) {
        hasLastDevice = true;
        Log.i( "HAS", "LAST DEVICE" );
        Log.i( "HAS", savedStuff.getName() );
    } else {
        hasLastDevice = false;
        Log.i( "HAS NO", "LAST DEVICE" );
    }

    pairedDeviceList = new ArrayList<BluetoothDevice>();
    pairedDevices = mService.getAdapter().getBondedDevices();

    for( BluetoothDevice device: pairedDevices ) {
        pairedDeviceList.add( device );
    }
    if( hasLastDevice ) {
        for( int i = 0; i < pairedDeviceList.size(); i++ ) {
            Log.i( "4 HERE HERE", pairedDeviceList.get( i ).getName() );
            Log.i( "4 HEUH?I@JD", savedStuff.getName() );
            if( pairedDeviceList.get( i ).getName().equals( savedStuff.getRealName() ) ) {
                // THIS IS THE DEVICE WE NEED
                previousDevice = pairedDeviceList.get( i );
                i = pairedDeviceList.size();
            }
        }
    }




    if( savedStuff != null ) {
        Log.i( "HAS", savedStuff.getName() );
    }
}

onStop()

public void onStop() {
    super.onStop();
    if(D) Log.e(TAG, "-- ON STOP --");
    if( savedStuff != null ) {
        Log.i( "HAS", savedStuff.getName() );
    }
    //  Relay();
}

你可能想展示一些代码。 - jsmith
@jsmith 刚刚添加了它。虽然我不认为这会有什么影响,但是跳过整个 onCreate 方法是如何可能的呢?我感觉我正在错误地终止应用程序。 - JuiCe
你确定你没有以某种方式过滤掉那条消息吗?否则,我会说看起来你做得很正确。 - jsmith
@jsmith 我不知道那怎么可能。它们第一次显示,然后我选择强制退出,然后重新启动,onCreate()onStart() 就不会产生日志消息。此外,在 onCreateonStart 中的方法调用有单独的日志消息,但从未显示。它一定没有调用 onCreateonStart,这毫无意义。 - JuiCe
嗯,我在想“强制退出”到底是做什么的。它只是影响服务吗?你可能需要查找更多相关信息。在我们的系统中,在应用程序中选择返回按钮将销毁该活动。可以试试这个。 - jsmith
显示剩余2条评论
1个回答

5

我曾遇到Application.onCreate()日志记录的同样问题。我发现,如果我将筛选器从“仅显示所选应用程序”更改为“无过滤器”,并在LogCat中搜索使用的TAG,那么我就可以看到日志消息。


由于这是很久以前的事情,我不记得我为哪个项目提出了这个问题,也不知道我是如何解决它的,所以我不知道是否应该接受你的答案。希望将来有人会发现它有用! - JuiCe
@JuiCe 不用担心。 :) - Sean Leather
这个小费运作得很好。谢谢。 - frank

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接