【问题标题】:Android - Application Launch testAndroid - 应用程序启动测试
【发布时间】:2015-03-02 15:49:39
【问题描述】:

我正在创建一个应用程序,它将启动各种 Android 应用程序(即:联系人、日历、电子邮件...)并记录其启动时间。

为此,我有以下代码。

public class LaunchPerformanceBase extends InstrumentationTestCase {

public static final String LOG_TAG = "Launch Perf";

public Bundle mResults;
public Intent mIntent;

public LaunchPerformanceBase(String pkg_name, String class_name) {
    mResults = new Bundle();
    try {
        mIntent = new Intent(Intent.ACTION_MAIN);
        mIntent.setClassName("com.android.contacts.activities", "com.android.contacts.activities.PeopleActivity");
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    } catch (NullPointerException e) {
        Log.d(LOG_TAG, "NULL POINTER EXCEPTION setClassName()");
        e.printStackTrace();
    }

}

/**
 * Launches intent, and waits for idle before returning.
 *
 */
public void LaunchApp() {
    try {
        Activity act = getInstrumentation().startActivitySync(mIntent); <-- LINE NUMBER 69
        getInstrumentation().waitForIdleSync();
        act.finish();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}
}

在主应用程序中,我正在执行以下操作。

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

    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                LaunchPerformanceBase LaunchData = new LaunchPerformanceBase("com.google.android.contacts", "com.android.contacts.activities.PeopleActivity");
                LaunchData.LaunchApp(); <--LINE NUMBER 38
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    thread.start();
}

我在 startActivitySync() 调用时收到 NullPointerException。 01-05 08:55:59.430 4653-4692/perftest.com.applicationlaunchtime W/System.err: java.lang.NullPointerException: 尝试调用虚拟方法'android.app.Activity android.app.Instrumentation.startActivitySync(android. content.Intent)' 在空对象引用上 01-05 08:55:59.430 4653-4692/perftest.com.applicationlaunchtime W/System.err:在 perftest.com.applicationlaunchtime.LaunchPerformanceBase.LaunchApp(LaunchPerformanceBase.java:69) 01-05 08:55:59.430 4653-4692/perftest.com.applicationlaunchtime W/System.err:在 perftest.com.applicationlaunchtime.MainActivity$1.run(MainActivity.java:38)

我是 Android 应用程序开发的新手,因此非常感谢任何帮助。

【问题讨论】:

  • @OP 让我知道我的回答是否对您有帮助。如果没有在下面发表评论,我很乐意提供帮助。

标签: android android-intent nullpointerexception performance-testing


【解决方案1】:

你得到一个空指针异常是因为这段代码

public Intent mIntent;

这个变量mIntent的作用域是整个LaunchPerformanceBase类。

在构造函数public LaunchPerformanceBase(String pkg_name, String class_name)中你已经初始化了与上面同名的意图,

Intent mIntent = new Intent(Intent.ACTION_MAIN);

当您在LaunchApp() 中调用startActivitySync(mIntent); 时,它指的是上面声明的未初始化的public Intent mIntent; 变量。这就是您收到 Null 指针异常的原因。

所以改这行代码,

Intent mIntent = new Intent(Intent.ACTION_MAIN);

mIntent = new Intent(Intent.ACTION_MAIN);

【讨论】:

  • 感谢您的帮助。我已经尝试过了,但我仍然遇到同样的错误。
      W/System.err: java.lang.NullPointerException: 尝试在空对象引用 W 上调用虚拟方法 'android.app.Activity android.app.Instrumentation.startActivitySync(android.content.Intent)' /System.err:在 perftest.com.applicationlaunchtime.LaunchPerformanceBase.LaunchApp(LaunchPerformanceBase.java:91) W/System.err:在 perftest.com.applicationlaunchtime.MainActivity$1.run(MainActivity.java:68)
  • @JackupPeter - LaunchPerformanceBase.java:89, MainActivity.java:67, MainActivity.java:66 - 你能在你的代码中指出这些行吗?另外请在每次编译之前保存您的程序,否则只会编译您的旧代码。
  • LaunchPerformanceBase.java:89: Activity act = getInstrumentation().startActivitySync(mIntent); MainActivity.java:66: LaunchPerformanceBase LaunchData = new LaunchPerformanceBase("com.google.android.contacts", "com.android.contacts.activities.PeopleActivity"); MainActivity.java:67: LaunchData.LaunchApp();
  • 我没有足够的积分在那里聊天,你至少需要 20 积分才能聊天。
  • @JackupPeter 你所有的错误都指向这部分代码getInstrumentation().startActivitySync(mIntent);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-26
  • 2014-07-22
  • 2012-04-17
相关资源
最近更新 更多