Hoping to change widget attributes by selecting a menu item

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


Sponsored Links
 Hi,

I am writing a program that involves a menu. Once you press the menu
button
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.

Thanks!
-Jimmy



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;

@SuppressWarnings("unused")
public class TestingButtons extends Activity {
       protected void onCreate(Bundle icicle) {
               super.onCreate(icicle);
               setContentView(R.layout.main);

               final ImageButton myButton12 = (ImageButton)
findViewById
(R.id.btid12);
               myButton12.setOnClickListener(new View.OnClickListener
()
{
               public void onClick(View v) {
               // Perform action on click
                       phoneNumber.setText(phoneNumber.getText()+"#");
                       }
               });
       }

   @Override
   public boolean onCreateOptionsMenu(Menu menu){
       super.onCreateOptionsMenu(menu);

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

       @Override
       public boolean onOptionsItemSelected(MenuItem item){
               switch(item.getItemId()){
               case 1:
                       shiftLeft();
               return true;
               case 2:
               return true;
               }
       return true;
       }

       public void shiftLeft(){
               myButton12.setLayoutParams(new
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
message.

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.

Jesse
--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. VM aborting

Hi All,
            I have one service name syncservice which is getting
killed himself. Looks that VM aborting it. Please have look and guide
me.



I/DILEEP  (  262): Synchronise constructor
DILEEEEEEEEEEEEEEPPPPPPPPPPPPPPPPPPPPPPP
I/Synchronise(  262): onCreate......
I/ActivityManager(   61): Start proc com.example.syncservice for
service com.example.syncservice/.SyncmlService: pid=268 uid=10011
gids={3003}
I/Synchronise(  262): getInstance......
D/ddm-heap(  268): Got feature list request
I/SyncmlService(  268): Chetan: onCreate  of SyncmlService
D/dalvikvm(  268): Trying to load lib /system/lib/libsyncml_port.so
0x0
D/dalvikvm(  268): Added shared lib /system/lib/libsyncml_port.so 0x0
D/        (  268): *************** Loading PORT library.........
D/        (  268): after registering methods for ext handler
D/        (  268): after registering methods for DSAcc
D/        (  268): after registering methods for DevInfo
D/dalvikvm(  268): Trying to load lib /system/lib/libsyncml_core.so
0x0
D/dalvikvm(  268): Added shared lib /system/lib/libsyncml_core.so 0x0
D/        (  268): *************** Loading CORE library.........
D/        (  268): before registering methods for basic connector
D/        (  268): before registering methods for clam connector
D/        (  268): after registering methods for SAN
D/DsAccConnector(  268): chetan: Library to be loaded.....
D/DsAccConnector(  268): chetan: Library to be loaded 1 .....
D/dalvikvm(  268): Trying to load lib /system/lib/libsyncml_port.so
0x0
D/dalvikvm(  268): Shared lib '/system/lib/libsyncml_port.so' already
loaded in same CL 0x0
D/dalvikvm(  268): Trying to load lib /system/lib/libsyncml_core.so
0x0
D/dalvikvm(  268): Shared lib '/system/lib/libsyncml_core.so' already
loaded in same CL 0x0
I/SyncmlService(  268): start registering mExtHandler
I/SyncmlService(  268): calling registerExtHandlerConnector
W/dalvikvm(  268): JNI WARNING: JNI method called with exception
raised
W/dalvikvm(  268):              in Lsyncmlcore/Interface/
SyncmlConnector;.tslds_register_ext_handler_connector (Lsyncmlcore/
Interface/TsldsExtHandlerInterface;)I (NewGlobalRef)
W/dalvikvm(  268): Pending exception is:
I/dalvikvm(  268): Ljava/lang/NoClassDefFoundError;:
I/dalvikvm(  268):  at dalvik.system.NativeStart.main(Native Method)
I/dalvikvm(  268): "main" prio=5 tid=3 NATIVE
I/dalvikvm(  268):   | group="main" sCount=0 dsCount=0 s=N
obj=0x4001b268 self=0xbd00
I/dalvikvm(  268):   | sysTid=268 nice=0 sched=0/0 cgrp=default
handle=-1344001384
I/dalvikvm(  268):   at
syncmlcore.Interface.SyncmlConnector.tslds_register_ext_handler_connector(Native
Method)
I/dalvikvm(  268):   at
syncmlcore.Interface.SyncmlConnector.registerExtHandlerConnector(SyncmlConnector.java:
53)
I/dalvikvm(  268):   at
com.example.syncservice.SyncmlService.onCreate(SyncmlService.java:184)
I/dalvikvm(  268):   at
android.app.ActivityThread.handleCreateService(ActivityThread.java:
2780)
I/dalvikvm(  268):   at android.app.ActivityThread.access
$3200(ActivityThread.java:119)
I/dalvikvm(  268):   at android.app.ActivityThread
$H.handleMessage(ActivityThread.java:1917)
I/dalvikvm(  268):   at
android.os.Handler.dispatchMessage(Handler.java:99)
I/dalvikvm(  268):   at android.os.Looper.loop(Looper.java:123)
I/dalvikvm(  268):   at
android.app.ActivityThread.main(ActivityThread.java:4363)
I/dalvikvm(  268):   at java.lang.reflect.Method.invokeNative(Native
Method)
I/dalvikvm(  268):   at java.lang.reflect.Method.invoke(Method.java:
521)
I/dalvikvm(  268):   at com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:860)
I/dalvikvm(  268):   at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
I/dalvikvm(  268):   at dalvik.system.NativeStart.main(Native Method)
I/dalvikvm(  268):
E/dalvikvm(  268): VM aborting
I/DEBUG   (  184): *** *** *** *** *** *** *** *** *** *** *** *** ***
*** *** ***
I/DEBUG   (  184): Build fingerprint: 'generic/generic/generic/:2.1-
update1/ECLAIR/eng.amitarora.20100830.172601:eng/test-keys'
I/DEBUG   (  184): pid: 268, tid: 268  >>> com.example.syncservice <<<
I/DEBUG   (  184): signal 11 (SIGSEGV), fault addr deadd00d
I/DEBUG   (  184):  r0 00000354  r1 afe1331d  r2 0000000c  r3 deadd00d
I/DEBUG   (  184):  r4 00000026  r5 ad07ff50  r6 00000000  r7 fffe72f8
I/DEBUG   (  184):  r8 becca948  r9 4104bd60  10 4104bd48  fp 00000000
I/DEBUG   (  184):  ip ad080160  sp becca880  lr afe142dd  pc
ad035452  cpsr 20000030
W/InputManagerService(   61): Got RemoteException sending
setActive(false) notification to pid 231 uid 10010


Thanks
chetan

-- 

2. Spinner databinding?

Hi every body. I am trying to bind my database with spinner. But when I try
to run this code on device or emulator it shows force to shutdown the
application. So nice all of you if you resolve the issue, Here is the code.

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.TextView;
import android.widget.Spinner;
import android.widget.SimpleCursorAdapter;
import android.app.ListActivity;
import static com.servanetdatabase.Constants.Table_Name;
import static com.servanetdatabase.Constants.KEY_SensorID;
import static com.servanetdatabase.Constants.KEY_type;
import static com.servanetdatabase.Constants.KEY_ThresouldValue;
import static com.servanetdatabase.Constants.KEY_Address;
import static com.servanetdatabase.Constants.KEY_City;
import static com.servanetdatabase.Constants.KEY_State;
import static com.servanetdatabase.Constants.KEY_Latitude;
import static com.servanetdatabase.Constants.KEY_GPSNo;
import static com.servanetdatabase.Constants.KEY_SensorID;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.ArrayAdapter;




public class ServaNetDatabase extends Activity {

    private static String[] FROM = { KEY_SensorID, KEY_type,
KEY_ThresouldValue, };
    private static String ORDER_BY = KEY_SensorID + " DESC";
    public static String[] PROJECTION;
//    int[] to = new int[]{android.R.id.text1};


       private DBAdapter events;

       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          events = new DBAdapter(this);
          try {
             addEvent("This is an event");
             Cursor cursor = getEvents();
             showEvents(cursor);
          } finally {
             events.close();
          }

       }

       private void addEvent(String string) {
              // Insert a new record into the Events data source.
              // You would do something similar for delete and update.
              SQLiteDatabase db = events.getWritableDatabase();
              ContentValues values = new ContentValues();
             // values.put(KEY_SensorID, 1);
              values.put(KEY_type, 500);
              values.put(KEY_ThresouldValue, 500);
              db.insertOrThrow(Table_Name, null, values);
           }
       private Cursor getEvents() {
              // Perform a managed query. The Activity will handle closing
              // and re-querying the cursor when needed.
              SQLiteDatabase db = events.getReadableDatabase();
              Cursor cursor = db.query(Table_Name, FROM, null, null, null,
                    null, ORDER_BY);
              startManagingCursor(cursor);
              return cursor;
           }


       private void showEvents(Cursor cursor) {

              SimpleCursorAdapter adapter =
                new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, cursor, FROM, null );
              adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item );
              // get reference to our spinner
              Spinner s = (Spinner) findViewById( R.id.dspiner );
              s.setAdapter(adapter);



           }



        }
*

DB Helper class*

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import static com.servanetdatabase.Constants.Table_Name;
import static com.servanetdatabase.Constants.KEY_SensorID;
import static com.servanetdatabase.Constants.KEY_type;
import static com.servanetdatabase.Constants.KEY_ThresouldValue;
import static com.servanetdatabase.Constants.KEY_Address;
import static com.servanetdatabase.Constants.KEY_City;
import static com.servanetdatabase.Constants.KEY_State;
import static com.servanetdatabase.Constants.KEY_Latitude;
import static com.servanetdatabase.Constants.KEY_GPSNo;





public class DBAdapter extends SQLiteOpenHelper{





    private static final String DATABASE_NAME = "ServanetDB.db";

    private static final int DATABASE_VERSION = 3;


    public DBAdapter(Context ctx) {
        super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
     }

     @Override
     public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + Table_Name + " (" + KEY_SensorID
              + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_type
              + " TEXT," + KEY_ThresouldValue + " TEXT, " + KEY_Address
              + " TEXT," + KEY_City + " TEXT," + KEY_State + " TEXT,"+
KEY_Latitude +
              " TEXT," + KEY_GPSNo + " TEXT);");
     }

     @Override
     public void onUpgrade(SQLiteDatabase db, int oldVersion,
           int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + Table_Name);
        onCreate(db);
     }

  }

Regards,
Irfan

-- 

3. onpageshow event for Android Browser doesn't seem to work

4. Tool to generate platform documentation from sources

5. Cannot return Activity

6. android emulator is slow on vmware

7. MapsDemo and zoom buttons