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 6786179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:12:53+00:00 2026-05-26T17:12:53+00:00

I’m working on making a Phonegap plugin for Android. When I add the findViewById

  • 0

I’m working on making a Phonegap plugin for Android. When I add the findViewById method to this.ctx.runOnUiThread(new Runnable() I get an error as stated in the title.

Here is my code:

package com.company.msgbox;


import java.io.File;

import org.crossplatform.phonegap.trial.alternativeTo.R;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.view.View;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;

public class msgbox extends Plugin {

    private static final String SHOW = "show";
    private static final int MSG_INDEX = 0;
    private String msg;

    @Override
    public PluginResult execute(String arg0, final JSONArray arg1, String arg2) {
        if ( arg0.equals(SHOW) ) {
            this.ctx.runOnUiThread(new Runnable() {
                public void run() {
                    // try/catch generated by editor
                    try {
                        msg = arg1.getString(MSG_INDEX);
                    } catch (JSONException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                    }

                    AlertDialog alertDialog = new AlertDialog.Builder(ctx).create();
                    alertDialog.setTitle("Title");
                    alertDialog.setMessage(msg);
                    alertDialog.show();

                    View content = findViewById(R.id.layoutroot);
                    Bitmap bitmap = content.getDrawingCache();
                    File file = new File("/sdcard/test.png");
                }
            });
        }

        return new PluginResult(Status.OK);
    }

}
  • 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-26T17:12:54+00:00Added an answer on May 26, 2026 at 5:12 pm

    You need to call findViewById from a class that actually has the method. The good practice is usually to pass around the Activity you’re creating this class from. Something like:

    public class msgbox extends Plugin {
        private static final String SHOW = "show";
        private static final int MSG_INDEX = 0;
        private String msg;
        private final Activity parent;
    
        // constructor
        public msgbox(Activity parent) {
            this.parent = parent;
        }
    

    Then you can do a:

    parent.findViewById(R.id.layoutroot)
    

    You construct your msgbox from within an activity by using:

    msgbox myMsgBox = new msgbox(this);
    

    Of course, to do that, the R.id.layoutroot component must be in the activity you passed.

    If you’re not in an activity when you construct the msgbox, you can replace the constructor injection with a setter:

    public void setParent(Activity parent) {
        this.parent = parent;
    }
    

    Although, to be able to use findViewById within your Runnable, parent needs to be final, so you’ll have to copy it to a final variable (setter injection cannot be final, obviously)

    (NB: Also, your class doesn’t use standard java naming conventions, it’s confusing: call it MsgBox)

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm making a simple page using Google Maps API 3. My first. One marker
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.