Nick C. Pablo
Nick C. Pablo

Reputation: 3

ImageButton.setEnabled() throws Exception

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!

Upvotes: 0

Views: 314

Answers (1)

Sam
Sam

Reputation: 86958

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);

Upvotes: 3

Related Questions