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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:08:43+00:00 2026-06-11T01:08:43+00:00

A newbie here in Android/Java but well founded in C/C++ & Object Pascal. I

  • 0

A newbie here in Android/Java but well founded in C/C++ & Object Pascal. I have 5 ImageButtons with only one enabled. Then at some point in time when the lone enabled button is pressed, the other four are supposed to be enabled too by calling setEnabled() passed with the boolean values ( enableBtn2..3.. etc). But whenever I press the lone enabled button to call the member newRung() which in turn calls another member refreshMnuButtons(),the exception is raised which I suspect originated by the call to the setEnabled() parent class member.Can someone pls. point out the loophole? I frantically searched for the explanation but I can’t seem to find the appropriate answer. Kindly see my code attached:

public class MyActivity extends Activity {
    private ImageView cell1,cell2,cell3,cell4,cell5,cell6,rungClose;
    private ImageButton mnuBtn1,mnuBtn2,mnuBtn3,mnuBtn4,mnuBtn5;
    private boolean enableBtn1 = true,
            enableBtn2 = false,
            enableBtn3 = false,
            enableBtn4 = false,
            enableBtn5 = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_ladder_main);

        // setup toolbar
        TableLayout tbl = (TableLayout)findViewById(R.id.tblToolbar);
        ImageButton mnuBtn1 = new ImageButton(this);

        mnuBtn1.setImageResource(R.drawable.rungopentool);
        mnuBtn1.setEnabled(enableBtn1);
        //setup mnuBtn1 listener

        mnuBtn1.setOnClickListener(new View.OnClickListener() {
            @Override   
            public void onClick(View view) {
                newRung();
            }
        });
        ImageButton mnuBtn2 = new ImageButton(this);

        mnuBtn2.setImageResource(R.drawable.noc);
        mnuBtn2.setEnabled(enableBtn2);
        //setup mnuBtn2 listener

        mnuBtn2.setOnClickListener(new View.OnClickListener() {
            @Override   
            public void onClick(View view) {
                addNOC();
            }
        });
        final ImageButton mnuBtn3 = new ImageButton(this);

        mnuBtn3.setImageResource(R.drawable.ncc); 
        mnuBtn3.setEnabled(enableBtn3);
        //setup mnuBtn3 listener

        mnuBtn3.setOnClickListener(new View.OnClickListener() {
            @Override   
            public void onClick(View view) {
                addNCC();
            }
        });
        ImageButton mnuBtn4 = new ImageButton(this);

        mnuBtn4.setImageResource(R.drawable.process);
        mnuBtn4.setEnabled(enableBtn4);
        //setup mnuBtn4 listener

        mnuBtn4.setOnClickListener(new View.OnClickListener() {
            @Override   
            public void onClick(View view) {
                addProcess();
            }
        });
        ImageButton mnuBtn5 = new ImageButton(this);

        mnuBtn5.setImageResource(R.drawable.coil);
        mnuBtn5.setEnabled(enableBtn5);
        //setup mnuBtn5 listener

        mnuBtn5.setOnClickListener(new View.OnClickListener() {
            @Override   
            public void onClick(View view) {
                addCoil();
            }
        });

        TableRow  mnurow = new TableRow(this);
        mnurow.setBackgroundColor(0x3F000000);

        // display menu
        mnurow.addView(mnuBtn1); 
        mnurow.addView(mnuBtn2);
        mnurow.addView(mnuBtn3); 
        mnurow.addView(mnuBtn4); 
        mnurow.addView(mnuBtn5);

        tbl.addView(mnurow); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_pic_ladder_main, menu);
        return true;
    }

    private  void  createRungImages() {
        // TODO
    }

    private void newRung()
    {
        Toast.makeText(this, "New Rung" ,Toast.LENGTH_SHORT).show();
        enableBtn2 = true;
        enableBtn3 = true;
        enableBtn4 = true;
        enableBtn5 = true;
        enableBtn1 = false;
        refreshMnuButtons();
    }
    private void addNOC()
    {
        Toast.makeText(this, "NOC" ,Toast.LENGTH_SHORT).show();
    }
    private void addNCC()
    {
        Toast.makeText(this, "NCC" ,Toast.LENGTH_SHORT).show();
    }
    private void addProcess()
    {
        Toast.makeText(this, "Add Process" ,Toast.LENGTH_SHORT).show();
    }
    private void addCoil()
    {
        Toast.makeText(this, "Coil" ,Toast.LENGTH_SHORT).show();
    }

    public void refreshMnuButtons()
    {
        try {  
            mnuBtn1.setEnabled(enableBtn1);
            mnuBtn2.setEnabled(enableBtn2);
            mnuBtn3.setEnabled(enableBtn3);
            mnuBtn4.setEnabled(enableBtn4);
            mnuBtn5.setEnabled(enableBtn5);
        }
        catch(Exception e) {Toast.makeText(this, "Exception" ,Toast.LENGTH_SHORT).show(); }
    }
}

Here’s my xml: Please note that I’m trying to generate most of the graphic elements by code.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="false"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="44dp" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TableLayout
                android:id="@+id/ladderSheet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

            </TableLayout>

        </ScrollView>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

       <HorizontalScrollView
           android:id="@+id/horizontalScrollView1"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content" >

           <TableLayout
               android:id="@+id/tblToolbar"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:scrollbarStyle="outsideInset"
               android:scrollbars="horizontal" >
           </TableLayout>

       </HorizontalScrollView>

    </RelativeLayout>

</RelativeLayout>

Thanks in advance!

  • 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-11T01:08:44+00:00Added an answer on June 11, 2026 at 1:08 am

    You are creating two mnuBtnX variables: one local and one class wide. When you declare the buttons like this:

    private  ImageButton mnuBtn1,mnuBtn2,mnuBtn3,mnuBtn4,mnuBtn5;
    

    These are obviously the class variables, but then in onCreate() you use:

    ImageButton mnuBtn1 = new ImageButton(this);
    

    Frustratingly (for us C/C++ folk) this doesn’t not throw a warning or error telling you that you have created a local variable with the same name as a class variable. This local mnuBtn1 does not relate to the class’ mnuBt1. So in refreshMnuButtons() you are probably receiving a NullPointerException since the class’ mnuBtn1 is still null.

    Simply do this in onCreate():

    mnuBtn1 = new ImageButton(this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Extreme Android developer newbie here...well, new to Android development, not development in general. I
Newbie question here but for some reason I cant figure this out. I have
newbie here. I have a java application and I am working on to monitor
Java Newbie here. I have a JFrame that I added to my netbeans project,
Android newbie here. I'm using this tutorial to show some rows that I fetch
NEWBIE ALERT! Here's the situation. I've got an Android ListActivity class (AppWindow) that contains
newbie here, so thanks in advance for help! I have a Wordpress site with
Newbie here! My problem is as follows: I have a dynamically populating ul where
Java newbie here, I am trying to find the output of the following chunk
CSS newbie here. Strange thing is happening, I have gaps between links, and I

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.