Crash while trying to get data from other class - java

I'm trying to get random letter from different class/activity, but for some reason it crashes
randomLogic randomFunc;
String resultRandom = randomFunc.randomLetter(); <- crashes here
randomKana.setText(resultRandom);
And here is the other class:
public String randomLetter(){
int hiraArray = sFirst.length;
String hiragana_array = sFirst[RANDOM.nextInt(hiraArray)];
return hiragana_array;
}

You have only declared randomLogic randomFunc;. But you have not initialized.
Also if randomLogic is a Activity class you should not instantiate a Activity class. Its only declared in manifest and has its own lifecycle.
To pass values between activities use intent.putExtra methods

Related

How to pass data from one android activity to an Java class?

MainActivity(Activity)
String emailid=e1.getText().toString();
Intent i = new Intent(MainActivity.this,LongOperation.class);
i.putExtra("emailid",emailid);
LongOperation(Java class )
Intent i = getIntent();
a = i.getStringExtra("emailid");
ERROR MESSAGE:
'getIntent(java.lang.String)' is deprecated as of API 15: Android
4.0.3 (IceCreamSandwich)
In 1st Class:
Intent i = new Intent(MainActivity.this, LongOperation.class);
i.putExtra("emailid", emailid);
startActivity(i);
In 2nd Class:
Intent i = getIntent();
String emailid= i.getStringExtra("emailid");
Intents are used to pass data from one activity to another activity.
So if you want to pass data to a normal Java class i would suggest you to use a getter function in combination with a static java class.
So LongOperation has to look like that:
public class LongOperation {
private static String eMailID;
// gives the variable eMailID in this class the new value mailID
public static void setMailID(String mailID) {
eMailID = mailID;
}
// add additional code here
}
In the MainActivity you have to do sth like:
String emailid = e1.getText().toString();
// set the variable in the LongOperation class
LongOperation.setMailID(emailid);
I hope I got your question right, there are several other ways but that depends on the structure and logic of your code.
In general I would recommend to work not so much witch static, more object orientated. But in this case static might be easier.
Cheers

Pass String from activity to library class

I trying to pass a string from mainActivity to the activity of a
library i been trying using intent but is not recognizing this function has someone done this before?
I get this error:error
create the OFAndroid class like this
public class OFAndroid {
private String text ;
public OFAndroid(String text) {
this.text = text ;
}
public void useText() {
Log.e("TAG" , this.text);
}
}
Inside your MainActivity instantiate OFAndroid:
OFAndroid of = new OFAndroid("some string");
now you can use the passed String
of.useText();
getIntent is a method that is not available in your OFAndroid Class. If you want to access the Intent that was passes to an Activity you should call It from inside an Activity's Class.

can subclass initialize superclass variable

i have an activity say A that is extended by many other activities , i have a variable in activity A which is used by all other sub activities(classes) extending it. I want to know if i can set a value for that variable from a sub-activity such that the change will reflect in all the other subclasses extending Activity A.
i know its a basic question ,i am new to this any help is appreciated.
eg:
Activity A has
public String global = "ABC"
Activity B extent A
display(global); ---> ABC
Activity C extent A
display(global); ---> ABC
Activity D extent A
display(global); ---> ABC
now how can i change global in Activity D such that Activity B and C should also be affected.
Seems like you want a variable whos value persists and remains same throughout.
But since you only want to your inherited classes to be able to update or read the variable, you can do something like this:
class A
{
private static int your_var = 0;
public int get()
{
return your_var;
}
public void set(int a)
{
your_var = a;
}
}
class B extends A
{
}
class C extends A
{
}
In your static void main:
new B().set(101);
new C().get() // this will return value 101.
Note: Here only one copy of variable your_var is created. And since it is private, only the non static getter setter methods will be able to read or modify it. Thus making it accessible only by either the containing class itself or the child class objects.
You need to create a static variable in Activity A.
A static variable is a variable of the class and not of the objects.
You can have a singleton class which can hold global data and when need you can fetch the data from the single commonly shared instance of the singleton class. But there is another better way.
You need a class which sits on top of your activities. In any app we usually do have one such class (may be some Initilizer or main or manager class ) which wires the entities and initiates our application.
In android we have Application class which can hold global data. For reference see Android global variable
try the following:
in class A
public static String global = "ABC";
in class B
public class B extends A {
public static void main(String[] args) {
B b = new B();
A a = new A();
System.out.println(b.global);
System.out.println(a.global);
System.out.println(global);
}
}
alternatively you could try to work with encapsulation: mark the string as private and make the "global" string available through getters and setters. This is often a more flexible solution then working with static variables
http://www.tutorialspoint.com/java/java_encapsulation.htm

Passing an object as an argument through an external method call

I have an object which I need to pass into an ArrayList. The ArrayList is contained within an external method, which is in another class.
I've debugged the program and know that the information being passed into the 'Application' Constructor is being put into the 'app' object. My problem is moving it from here to the 'GuestsAttending' Method.
Any help would be greatly appreciated.
Booking Screen Method:
public void saveBookingInfo(View view) {
GuestsAttending sendApplication = new GuestsAttending();
EditText applicantNameText = (EditText) findViewById(R.id.applicantNameTextField);
EditText itemToBurnText = (EditText) findViewById(R.id.itemToBurnTextField);
String appName = applicantNameText.getText().toString();
String appItemToBurn = itemToBurnText.getText().toString();
if (appItemToBurn.isEmpty() || appName.isEmpty()) {
Toast.makeText(BookingScreen.this, "Please fill in all fields.", Toast.LENGTH_SHORT).show();
}
else {
Application app = new Application(appItemToBurn, appName);
sendApplication.getGuestsAttending(app);
this.finish();
}
}
GuestsAttending Method:
public void getGuestsAttending(Application app){
peopleAttending.add(app);
}
Application Class:
public class Application {
private String name;
private String item;
public Application(String applicantName, String applicantItem) {
name = applicantName;
item = applicantItem;
}
}
For clarity, change "getGuestAttending" to "addGuestAttending". If "peopleAttending" is an array, your method won't work. Change "peopleAttending" to a List or Set, then it will work. If "peopleAttending" needs to be an array, you can't add items to it unless you declare it big enough to hold all your guests. Then you have to keep track of the number of guests, so you know where to insert new guests. In other words, don't use an array, use a List:
List attendingGuests = new LinkedList();
I found a different solution. Instead of creating the object in another class and passing it into the external method call as a parameter, I created the object within the external method, and passed a couple of strings to the method, which I passed into another classes' constructor to make the object within the method.

How to use setters and getters to pass data between classes in android

I'm writing a program that has two classes, one that extends Activity and another that extends SurfaceView. The activity has an object of the SurfaceView class. I am trying to use setters and getters to send data between these two classes, but every time I try, eclipse says that the methods for setting and getting need to be static. I can't do this because I don't want them to be static.
The Activity class contains the following methods:
public float getxTouch(){
return xTouch;
}
public float getyTouch(){
return yTouch;
}
The SufaceView class contains the following code:
xpos = ActivityClass.getxTouch();
ypos = ActivityClass.getyTouch();
How might I fix this without making the methods static?
You can use Intents to transfer references of variables between your Activity and your class.
First, let's create a serializable class that will contain your variables:
class XYTouch implements Serializable{
public static final String EXTRA = "com.your.package.XYTOUCH_EXTRA";
private float xTouch;
public void setX(String x) {
this.xTouch = x;
}
public String getX() {
return xTouch;
}
// do the same for yTouch
}
Then, in your activity's onCreate, create a new XYTouch object and set its xTouch and yTouch attributes using set and get methods. Then write
Intent intent = new Intent(this, OtherClass.class);
intent.putExtra(XYTouch.EXTRA, xytouchobject);
startActivity(intent);
In your other class (OtherClass) in which you want access to those variables:
public void onCreate(Bundle savedInstance){
// ....
XYTouch xytouch = (XYTouch) getIntent().getSerializableExtra(XYTouch.EXTRA);
// ....
}
Then, you can use get and set methods of XYTouch anywhere in your class to have a reference to xTouch and yTouch.
Another way would be to retrieve it from a class of your own that extends Application and keeps a reference to those variables. Then you would use an Activity context to read them in.
Pass the reference of the activity class to your surface class. Do something like this, so you don't have to make the methods static.
In your SurfaceView class:
Activity foo;
//some method to set foo:
public void setFoo(Activity foo) {
this.foo = foo;
}
// Then you can simple call your getX() and getY() methods like this:
foo.getX(); and foo.getY();
From your Activity class:
yourSurfaceViewInstance.setFoo(this);

Categories

Resources