Hoping to change widget attributes by selecting a menu item

by midicool » Thu, 05 Mar 2009 11:47:18 GMT

Sponsored Links

I am writing a program that involves a menu. Once you press the menu
and then select "Left",  it's supposed to change the attributes of
myButton12, an ImageButton.

However, I am getting a "myButton12 cannot be resolved" error message.
Please help me figure out what is wrong with this code.


The code:

package com.innodesign.testingbuttons;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.ImageButton;
import android.widget.Button;
import android.widget.EditText;
import android.net.Uri;
import android.content.Intent;

public class TestingButtons extends Activity {
       protected void onCreate(Bundle icicle) {

               final ImageButton myButton12 = (ImageButton)
               myButton12.setOnClickListener(new View.OnClickListener
               public void onClick(View v) {
               // Perform action on click

   public boolean onCreateOptionsMenu(Menu menu){

               menu.add(0, 1, 0, "Left");
               menu.add(0, 2, 0, "Right");
   return true;

       public boolean onOptionsItemSelected(MenuItem item){
               case 1:
               return true;
               case 2:
               return true;
       return true;

       public void shiftLeft(){
AbsoluteLayout.LayoutParams(10, 10,
10, 10));



Hoping to change widget attributes by selecting a menu item

by Jesse McGrew » Thu, 05 Mar 2009 13:14:11 GMT


You've defined myButton12 as a local variable for the onCreate()
method. It doesn't exist in shiftLeft(), so you're getting that error

Define myButton12 as a field of your activity class instead so it can
be used from the other methods:

  public class TestingButtons extends Activity {
      private ImageButton myButton12;
      /* ... */

Then remove the words "final ImageButton" from the line in onCreate()
where you assign to myButton12.


Sponsored Links

Other Threads

1. setPreviewFrameRate not working on device


I'm doing some processing on the preview frames returned from the
camera using:

onPreviewFrame(byte[] data, Camera camera)

Before starting the preview I set the preview frame rate like this:

Camera.Parameters p = c.getParameters();

This works fine on the Android Emulator, but on the device I'm still
getting approx 15 fps.

Has anyone else had this problem and how have they solved it?



2. How can we restart an Activity properly?

Hi All,
I created a restart() method to restart an Activity, there are some
reasons for us to do this since there are some thing that we can not
do unless we make the Activity completely restarted, for example, if
the SurfaceView is created in GPU memory and we want to "move" it back
to normal memory, than we have to restart the Activity.
Anyway, the following cod is our implementation of restart():

    public void restart(String commandLineArguments)
        Intent intent = new android.content.Intent();
        intent.setClass(this, this.getClass());  //We will start the
Activity itself, not any other
        //this.finish();                     //I need to remove this
line, otherwise the new Activity will just live for a short moment.

The code is almost same as the SDK sample cod from "Forwarding.java"
except the last line "this.finish()". If we have this line, then the
newly started Activity will be shut down too (why?). After I
eliminated this line, a new Activity is started successfully but the
"old activity" is still inside of the Activity Stack, as the result,
there going to be numbers of instance of same Activity in the Activity

So the question is: how to properly kill previous Activity in the
newly launched Activity? Or, in other words, how can a Activity knows
"who launched me? and need to terminate it" I tried to use Intent, but
could not find a suitable function.

Many thanks for any suggestions.


3. Android Apps Development :Trainer needed

4. Help - Android Market Suspends My 鈥淏log App鈥?

5. http-proxy does not work on emulator when i use the build of master branch with cupcake changes

6. Need help for creating submenu