2

我有一个 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”手动启动意图,我也不会得到 ANR,所以我的问题只在我运行 Monkey 时出现。

非常感激任何的帮助。谢谢你。

4

0 回答 0