Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6243421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:08:13+00:00 2026-05-24T12:08:13+00:00

I’m new to Android Testing stuff. I’d like to test if clicking a button

  • 0

I’m new to Android Testing stuff. I’d like to test if clicking a button will open up a corresponding activity or not. I did some research and found out that I will need to use ActivityManager to do the checking.

Problem is, I can’t get the “clicking” part working. I’m trying to use Button.performClick().

Initially I just called this function, and got an error saying that I can’t do this in the current thread. After some googling I found out that I need to call it in the UI thread, and came across runOnUiThread(Runnable r) method.

The button I’m trying to click is _helloButton_. This is obtained in the _setUp()_ method. I did _assertNotNull_ check on this to make sure it’s there.

Within a test method, I call

mActivity.runOnUiThread(new Runnable() {
        public void run() {
            helloButton.requestFocus();
        }
    });
helloButton.performClick();

and I get a NPE at the line calling requestFocus().

Next I tried

mActivity.runOnUiThread(new Runnable() {
        public void run() {
            helloButton.performClick();
        }
    });

and still get the same null pointer exception.

On the JUnit perspective, I get this message

Test failed to run to completion. Reason: 'Instrumentation run failed due to 'java.lang.NullPointerException''. Check device logcat for details

And the stackTrace look like this.

    08-05 19:03:11.922: ERROR/AndroidRuntime(578): Uncaught handler: thread main exiting due to uncaught exception
08-05 19:03:11.922: ERROR/AndroidRuntime(578): java.lang.NullPointerException
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at com.example.helloworldmk2.test.HelloWorldMK2Test$1.run(HelloWorldMK2Test.java:57)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at android.os.Handler.handleCallback(Handler.java:587)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at android.os.Looper.loop(Looper.java:123)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at java.lang.reflect.Method.invoke(Method.java:521)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-05 19:03:11.922: ERROR/AndroidRuntime(578):     at dalvik.system.NativeStart.main(Native Method)

Line 57 is where I’m calling the helloButton.performClick().

I’m not sure why I’m getting NPE; assertNotNull passes without problem. If you can help me with this issue, I’d really appreciate it. Thanks in advance.

EDIT: I’m subclassing ActivityInstrumentationTestCase2 for this particular test class.

EDIT2: Logcat spews out some errors before NPE happens.

I see

08-05 20:08:54.702: ERROR/AndroidRuntime(754): ERROR: thread attach failed

and

08-05 20:08:58.642: ERROR/gralloc(52): [unregister] handle 0x3e1b28 still locked (state=40000001)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:08:15+00:00Added an answer on May 24, 2026 at 12:08 pm

    You can annotate your test to run on UI thread:

    @UiThreadTest
    public void testNoErrorInCapitalization() {
        final String msg = "this is a sample";
        mMessage.setText(msg);
        mCapitalize.performClick();
        final String actual = mMessage.getText().toString();
        final String notExpectedRegexp = "(?i:ERROR)";
        assertNotContainsRegex("Capitalization found error:",
            notExpectedRegexp, actual);
    }
    

    This is an example taken from Android Application Testing Guide.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.