【发布时间】:2012-12-18 18:30:35
【问题描述】:
我的onCreate() 方法中有一系列Log 语句用于调试,但有时它们只是没有打印在我的LogCat 中。有没有人知道的任何理由?
也许我误解了 Activity 生命周期的基础知识。我访问过the developer's page,但仍然没有任何意义。
让我澄清一下,如果我卸载应用程序然后再次从 Eclipse 启动它,则会显示 Log 调用。但是,我正在运行测试以查看已运行的应用程序功能。
我有一个 MainMenu Activity,它源于多个其他活动。当我卸载应用程序并重新启动它时,它会打印我期望它打印的内容。然后,我转到我的设备设置和Force Quit 应用程序(这是正确的做法吗?)。之后,我再次打开我的应用程序。 onCreate() 方法中没有 Log 消息,但是当我移动到不同的 Activity 时,我的 MainMenu 的 onPause() 和 onStop() 方法有 Log 消息正确打印到 LogCat。
有什么想法吗?
编辑:
我想在重新启动时添加,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 刚刚添加。不要认为它会有所作为,怎么可能一起跳过
onCreate方法?我觉得我错误地终止了应用程序。 -
您确定您没有以某种方式过滤掉该消息吗?否则,我会说你做得对。
-
@jsmith 我不知道这怎么可能。它们第一次显示,然后我选择强制退出,然后重新启动,
onCreate()和onStart()只是不产生日志消息。此外,onCreate和onStart中的方法调用具有从未显示的单独日志消息。它不能调用onCreate或onStart,这没有任何意义。 -
嗯。我想知道“强制退出”到底是做什么的。只是影响服务吗?您可能需要查找更多相关信息。在我们的系统上,在应用程序中选择后退按钮将破坏活动。试试看。