Android :: Passing Object To Method In Java Appears To Be By Reference
Sep 29, 2010
I thought when you passed objects to methods in Java, they were supposed to be by value.
public class MyClass{
int mRows;
int mCols;
Tile mTiles[][]; //Custom class
//Constructor
public MyClass(Tile[][] tiles, int rows, int cols) {..........
At this point, any changes to the mTiles object are reflected back to the tiles object.
View 3 Replies
Jun 25, 2010
I'm writing some stuff in java and i ran into some problems lately. cut short, i need to compare an object i created to another instance of this very class i instantiazed before with different data.
looks like this:
CODE:........
with a class a:
CODE:..........
The problem is, that i end up finding out that the values from newA are always equal to those from oldA. so i guess sth went wrong with passing the references of the objects in the last line of the loop...i thought java always passes references of objects unless an explicit copy() is called? if this does matter: this code is running on android - don't know if the dalvik vm messes aroung with this...
View 5 Replies
View Related
Nov 3, 2010
Object getThisObjName= new Object();
I'd like to get the name of an object reference.
Is this possible via reflection in Android?
View 10 Replies
View Related
Feb 23, 2010
I am trying to get a reference to the android internal class PhoneWindow (android.policy.PhoneWindow) object from an application. I would like to call a method on it (specifically I would like to call getDecorView on PhoneWindow). How do I do this?
View 2 Replies
View Related
Apr 2, 2010
I have a Timer in my application, and would like to allow the user to set the timer, and maybe cancel it in another execution of the application. To do that, I need to have a reference to the Timer created in the first execution. How can I obtain that? Is there a way of storing/retrieving memory addressed?
View 3 Replies
View Related
Oct 19, 2010
In my Android application, I have to call the getSharedPreferences in non-activity classes. In order to avoid passing a Context object to each constructor of these classes (and because many of them are classes with only static methods which don't have a constructor), I have implemented these steps : The main activity of my application (which is launched at startup) is called Dispatcher. In the onCreate method of this class, I keep a reference to the created Dispatcher object in a static public variable of the Dispatcher class which can be accessed from any class in the project.
View 2 Replies
View Related
Aug 27, 2010
Which parameter passing method is used in Android? Pass by reference or Pass by Value? Please provide an Android Specific example for the same.
View 2 Replies
View Related
Mar 16, 2009
I'm trying to call a java method from javascript using addJavascriptInterface(); but seems does not work, it always display "failure"; java code...
View 2 Replies
View Related
Jun 16, 2010
I'm writing an Android app. I have a main method, which creates and runs a new Thread using an anonymous inner Runnable class. The run() method, when it's done, calls a method on it's parent class (in the main thread) that calls notifyDataSetChanged() so that the main thread can redraw the new data. This is causing all kinds of trouble (ViewRoot$CalledFromWrongThreadException). The thing is, this method being called from the worker thread is on the class that's created in the UI thread. Shouldn't that be running on the UI thread? Or am I missing something.
View 1 Replies
View Related
Mar 9, 2009
In my layout xml, I have: <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</TabHost>
My question is how can I get a reference to TabWidget object. I tried this: mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs); but this won't compile since 'com.android.internal.R is not visible. And then I try change the id to android:id="@+id/tabs" but I get a run time error saying: "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'"
View 2 Replies
View Related
Aug 16, 2010
Trying to assign a key to each field in android to reference in Java.
View 1 Replies
View Related
Feb 18, 2009
I am trying to get a FileInputStream object on an image that the user selects from the picture gallery.
This is the android URI returned by android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI content://media/external/images/media/3
When I try to construct a java URI object from this object, I get an IllegalArgumentException with the exception description Expected file scheme in URI: content://media/external/images/media/3 whereas the android URI shows the scheme as content
Never found a solution for the original question. But if you want the byte stream of an image in the pictures gallery, this piece of code will do that.
CODE:...................
View 4 Replies
View Related
Jun 23, 2010
I am trying to display a dialog box in a simple Java class that is called from my main Activity but not successful. Please help me to figure it out.
I am passing the required values as parametrs.
I have two class: class MainActivity extends Activity :: Main *starting point *of Application class ShowMyDialog :: a simple java program In which I *generate an URl* and *display a dialog with WebView*.
I am passing the Acitivity from my MainActivity to this class as a parameter in function.
But I am *unable to call* the onCreateDialog method that I have *defined in the simple java class.
However, If I define the *onCreateDialog method in MainActivity, I am able to display it successfully.
What Should I pass as Parameter to the non Activity class from MainActivity class so that I am able to display the dialog as defined by showdialog method in JAVA class ???*
My steps of source code is as follow:
code:.........................
View 3 Replies
View Related
Sep 12, 2010
For example, say the main Activity has an custom object foo with a method called bar. When foo.bar() is called via the activity, it does a bunch of things on a thread and when it is finished it would somehow alert the main ui thread, the activity, it's parent that it is finished.
I imagine this working the same way an event like a OnClickListener works. The bar() method would trigger an event saying "Hey I'm finished" and that would trigger the listener on the main activity.
View 1 Replies
View Related
Feb 11, 2009
I'm having some problems porting a Java application to work in Android platform. I detected an incompatibility problem between java sun and Adroid sdk in java.lang.Class. I oberved that: public Field[] getFields() Returns an array containing Field objects describing all fields which are defined. That's array is sorted as attributes are declared in the main Class in sun jdk. For example, next Class is defined as: public class Example { public boolean stop; public int atr1; public String name; ....
}
View 5 Replies
View Related
Nov 7, 2010
In my program I have a custom view object. Inside the view class is a method called foo. For some reason when I call foo from my activity, it doesn't fire. Here is the code:
XML of the custom view:
<com.company.application.MyView android:id="@+id/my_view" android:layout_width="wrap_content" android:layout_height="wrap_content" />
The activity code:
public class Main extends Activity { MyView mView;
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.main);
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.main, null); mView= (MyView) v.findViewById(R.id.my_view);
//A button to fire the method inside foo() Button switchLeft = (Button) findViewById(R.id.switch_left); switchLeft.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { callTest(); } } );
} public void callTest() { Log.w(this.getClass().getName(), "clicked left arrow");
mView.foo(); mView.postInvalidate();
} }
Finally here is foo:
public void foo() { mBackground = mContext.getResources().getDrawable(R.drawable.temp_canvas); Log.w(this.getClass().getName(), "background set");
}
The first log is always written when the button is pressed, but the second log is not because foo never fires. What is the deal here? Upon request, the full view
public class MyView extends View { Drawable mBackground; Context mContext;
public MyView (Context context) { super(context); mContext = context;
mBackground = mContext.getResources().getDrawable(R.drawable.leftarrow1);
} public MyView (Context context, AttributeSet attrs) {
super(context, attrs); mContext = context;
mBackground = mContext.getResources().getDrawable(R.drawable.leftarrow1);
} public void foo() { mBackground = mContext.getResources().getDrawable(R.drawable.temp_canvas);
Log.w(this.getClass().getName(), "background set"); }
@Override public void onDraw(Canvas canvas) { Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL); // draw a solid blue circle
paint.setColor(Color.BLUE); canvas.drawCircle(20, 20, 15, paint);
// draw a test background mBackground.setBounds(0, 0, 300, 400);
mBackground.draw(canvas); }
}
View 1 Replies
View Related
Jan 9, 2010
I am creating an application which retrieves images from the web. In case the image cannot be retrieved another local image should be used.
While trying to execute the following lines:
CODE:................
The line if(drawable.equals(null)) throws an exception if drawable is null.
Does anyone know how should the value of drawable be checked in order not to throw an exception in case it is null and retrieve the local image (execute drawable = getRandomDrawable())?
View 5 Replies
View Related
Apr 14, 2009
I have constructed a Bitmap object in Java. Can you please tell me how can I scale it (x, y with a different ratio) on android?
View 2 Replies
View Related
Feb 7, 2010
I'm having some issues with the old "Cannot make a static reference to a non-static method" error in my Android program. I am creating a sand falling game (similar to the Powder Game) and I created a class called Control to create a Control Bar at the bottom of the screen with a slider for brush size (that works fine) and a button to pop up a Dialog to allow users to pick the selected element. However, when I call DemoActivity.showDialog(2) from my code, it gives the static reference to non-static error (DemoActivity is the main activity of my application). I also tried changing it to just Activity.showDialog(2), but I got exactly the same error!
Here's my code:
CODE:................
I fixed it by adding the following to my Control.java code:
CODE:..........
And then calling control.setActivity(this); from my onResume section of DemoActivity.java!
View 2 Replies
View Related
Dec 2, 2009
(Code is for Android Actually, I need code to be portable between Android and Java SE.)
I want to have a "settings" class with various game settings, like
public int map_size;
public String server_name;
etc.
The data needs to be accessed fairly frequently (so members, not a key-value map), and from time to time de/serialized in some standard way (mainly to send it through network).
I want to be able to
Serialize and deserialize the object into XML or JSON, without having to explicitly write the code for every member (but still having some degree of control over the format).
Define some (constant) meta-data about every member (default value, GUI name, XML identifier, ...), in a way that allows for easy modification in the source code (I want to be able to add a new meta-property, define a default value for it, and not have to specify it everywhere else).
1 is achievable by using reflection. I thought Java annotations for class members would be perfect for 2:
CODE:.............
But it looks like (user-defined) annotations don't work in Android yet - code using them crashes the compiler...
What would be the easiest way to store the meta-data about the settings (or another way to approach all this)?
Store information about settings in some external XML file? Store it in a Java data structure, with content defined in the code? Defining the data in this way somehow seems very unwieldy, especially compared to keyword arguments of annotations?
View 3 Replies
View Related
Oct 29, 2010
From my android client i am sending a json to string object.but the .net client is getting it as empty string. here is my code...
is this header format correct?
View 3 Replies
View Related
Jun 21, 2010
I have created a class with 3 sub classes in Java. The 3 sub classes contain variables to store information from my XML document. I am able to store in the first two for there are only single instances of the data in the XML. The third class contains variables that repeat multiple times. I want to store an object of "third class" objects. I also have a SAX parser class. I am doing this within an Android environment (1.6).
Does this make enough sense without displaying any code?
View 2 Replies
View Related
Jul 28, 2010
I have 2 String Operations I would need relevant in Object C // Get the newstring from mystring start at counter Java: newstring = mystring.substring(counter) OBJ-C: ? // Get the position from searchstring in mystring Java: startpos = mystring.indexOf(searchstring) ObJ-C: ?
I had two other questions and found now the solution (here als for others)
CODE:.................................
View 1 Replies
View Related
May 23, 2010
I was wondering what the simplest program for sending an object from my Android phone to my computer wirelessly (via LAN) would be. I have created Java RMI programs with a server and multiple clients, so I have a grasp of the concept. However with android I'm just not sure where to start.
What I am aiming to do is send some sort of information (could simply be text) to my computer and my computer will do an action. I have the GUI interface's and the actions to be carried out all worked out, just the sending of some sort of information is getting me.
View 1 Replies
View Related
Apr 19, 2010
I'm building an android application, which has a list view, and in the list view, a click listener, containing an onItemClick method. So I have something like this:
public class myList extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/* Do something*/
}
}
}
Normally, this works fine. However, many times I find myself needing too preform an application using the outer class as a context. thusfar, I've used: parent.getContext(); to do this, but I would like to know, is that a bad idea? I can't really call: because it's not really a subclass, just an embedded one. So is there any better way, or is that considered cosure? Also, if it is the right way, what should I do if the embedded method doesn't have a parameter to get the outside class?
View 2 Replies
View Related
Mar 2, 2010
I'm writing an Android application. One problem is your app cannot contain a file whose uncompressed size is bigger than about 1Mb. I have a serialized object that I want to load that totals about 2Mb. My plan was to split this file into two smaller files, then load the object by combining both files at runtime.
However, I cannot work out how to use e.g. InputStream and ObjectInputStream to specify that I want to read the data from two input streams. How can I do this?
For example, say my object was split between file O1 and O2. How can I say "Load the object by reading the data from O1 then from O2"?
View 2 Replies
View Related
Jun 22, 2010
So I ran into a problem today while working on my Android program. I have a class that turns that an XML string into a Java object (third party) and it works fine in as a regular java project but on Android I get this weird error:
CODE:.......
I hide my application name and my package for obvious reasons but I was wondering if anyone has ever encountered problems like this. Class is in the correct package, which is a library I have added. Other classes that I reference before are there and those can be made. Are there any other reasons a ClassNotFound Exception is thrown?
View 2 Replies
View Related
Aug 28, 2010
the title say it all. I wanna know a corresponding method in Android/Java that is like the GetTickCount method in C#/C++?
View 1 Replies
View Related
Aug 25, 2010
In my Android client I want to receive JSON objects from a server. By googling I found a lot of different possibilities how to best parse the InputStream from the Server, but most of them wrote their own parser. Isn't there a library which does this parsing for me? Or how should I best implement it by myself?
View 4 Replies
View Related
Sep 15, 2010
Why should a static method in java accept only final or non final variables within its method, but not static?
For example I have the following method:
CODE:.................
View 6 Replies
View Related