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

  • SEARCH
  • Home
  • 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 8586095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:13:38+00:00 2026-06-11T22:13:38+00:00

I’m new to Java and Android, and I’m working on my first test app.

  • 0

I’m new to Java and Android, and I’m working on my first test app.

I’ve progressed with it, but I’m blocked with a Dialog.

I show the dialog from the Activity like this:

//BuyActivity.java
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shop);

    initialize_PR();
    display_PR();
    BuyDialog=new Dialog(this);
    BuyDialog.setContentView(R.layout.dialog_buy);

}
public void Action_ShowDialog_Buy(View view) {
    BuyDialog.show() ;
}

And the dialog is correctly shown when the button of the Activity that triggers Action_ShowDialog_Buy is clicked. But after that, the Dialog itself has a button:

<!-- dialog_buy.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- Other stuff -->

<Button
    android:id="@+id/Button_Buy"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Some_Other_Stuff"
    android:layout_centerHorizontal="true"
    android:text="@string/button_buy"
    android:onClick="Action_ShowDialog_Buy" />

</RelativeLayout>

The button method Action_ShowDialog_Buy is implemented on the Activity:

public void Action_ShowDialog_Buy(View view) {
    BuyDialog.dismiss() ;
}

but when I click on the Button in the Dialog I receive the error:

java.lang.IllegalStateException: Could not find a method BuyActivity.Action_ShowDialog_Buy(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'Button_Buy'

and below:

Caused by: java.lang.NoSuchMethodException:BuyActivity.Action_ShowDialog_Buy

but as you can see above, the method exists on the Activity.

I think I understand this is some kind of scope issue, but I do not manage to understand it. Please note that I have readed Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs but I need to understand, not just to copy code.

Many thanks

  • 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-06-11T22:13:39+00:00Added an answer on June 11, 2026 at 10:13 pm

    Thanks to everyone that tried to help.

    I’ve managed to sort this out by creating a class derived from Dialog and using it, using this code:

    import android.app.Dialog;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.widget.RelativeLayout;
    
    public class BuyDialogClass extends Dialog
    {
    
    //Ensure this Dialog has a Context we can use
    Context mContext ;
    
    public BuyDialogClass(Context context) {
        super(context);
        mContext=context; //Store the Context as provided from caller
    }
    
    @Override
     protected void onCreate(final Bundle savedInstanceState)
     {
      super.onCreate(savedInstanceState);
      RelativeLayout ll=(RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.dialog_buy, null);
      setContentView(ll); 
     }
    
    }
    

    This allowed me to call the dialog as this:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
    
        initialize_PR();
        display_PR();
        BuyDialog=new BuyDialogClass(this);
        //The setContentView is not necessary here as we call it on the onCreate
    
        //We can NOT access Dialog widgets from here,
        //because the dialog has not yet been shown.
    
    }
    public void Action_ShowDialog_Buy(View view) {
        BuyDialog.show() ;
    
        //NOW, after showing the dialog, we can access its widgets
        jobject_SeekBar_buy= (SeekBar) BuyDialog.findViewById(R.id.SeekBar_Dialog_Buy) ;
        jobject_SeekBar_buy.setMax(PR_num_coins/currentPR_buy_price) ;
        jobject_SeekBar_buy.setOnSeekBarChangeListener(this);
    
    }
    public void Action_Buy_PR(View view) {
        BuyDialog.dismiss() ;
    }
    

    I managed to do this by reading Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs but I still do not understand this Context issue.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms

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.