【发布时间】: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