如何解决猴子在子活动中的起始意图会导致ANR
| 我有一个Android应用程序,其主活动上带有一个按钮,可创建第二个PreferenceActivity以显示设置。 清单看起来像这样<application android:name=\".MyApp\"
android:icon=\"@drawable/icon\"
android:label=\"@string/app_name\">
<activity android:name=\".MyApp\"
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=\".Settings\"></activity>
</application>
当我使用Monkey进行测试时,就会出现问题。它将启动我的应用程序,然后按按钮创建我的PreferenceActivity。一旦创建了PreferenceActivity,monkey将发送一个意图以启动另一个包。 PreferenceActivity将暂停,其他程序包将运行。然后,猴子发送另一个意图启动我的原始应用程序。我看到我的PreferenceActivity输入onResume(),然后冻结,并发生ANR。
我在logcat中看到:
INFO/ActivityManager(211): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.MyApp/.MyApp } from pid 4532
# Main activity is created and runs.
# Monkey presses settings button,which sends intent to start PreferenceActivity.
INFO/ActivityManager(211): Starting: Intent { cmp=com.example.MyApp/.Settings } from pid 4519
# PreferenceActivity is created and runs.
# Monkey sends intent to start other package. PreferenceActivity pauses.
INFO/ActivityManager(211): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.OtherApp/.OtherApp } from pid 4532
# Other app runs.
# Monkey sends intent to start MyApp again.
INFO/ActivityManager(211): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.MyApp/.MyApp } from pid 4532
# PreferenceActivity\'s onResume() method runs.
# Screen is blank.
ERROR/ActivityManager(211): ANR in com.example.MyApp (com.example.MyApp/.Settings)
我删除了我的PreferencesActivity中的所有内容,所以剩下的就是
public class Settings extends PreferenceActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(this,R.xml.preferences,false);
addPreferencesFromResource(R.xml.preferences);
}
}
恢复PreferenceActivity时,我仍然得到ANR。但是,如果我将其更改为扩展Activity而不是PreferenceActivity并删除onCreate()中的首选项,我将不再获得ANR。如果我尝试使用\“ adb shell am \\”手动启动Intent,那么我也不会获得ANR,所以我的问题仅在运行Monkey时出现。
非常感激任何的帮助。
谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。