I've successfully passed a value from a static class to a non-static one, but I got an error, null value, when I put that value to an EditText.
public class HelloBubblesActivity extends SherlockFragmentActivity {
public EditText editText1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discuss);
editText1 = (EditText) findViewById(R.id.editText1);
}
public static class MyDialogFragment extends SherlockDialogFragment {
//i fill variable emotx is "test string"
public void emot(String emotx){
HelloBubblesActivity hb=new HelloBubblesActivity();
hb.smiley(emotx); //send value to smiley method..
}
}
public void smiley(String name){
Log.d("test", name); //result value is "test string" (success)
editText1.setText(name); //here is error
}
}
I'm not sure why I'm getting this issue. Can anyone see why this is not doing what is expected?
HelloBubblesActivity hb=new HelloBubblesActivity();
hb.smiley(emotx); //send value to smiley method..
Creates a new HelloBubblesActivity object but doesn't call onCreate and hence does not assign anything to editText1. Fields that have never been assigned are null in Java.
(Of course, to say the code is not doing what is "expected" would be wrong -- it's doing exactly what one would expect from the scenario.)
(Your mistake is most likely in thinking that setting a value in ANY HelloBubblesActivity object makes the value appear in ALL HelloBubblesActivity objects. Individual objects do not share instance fields, and what you set in one object does not magically appear in another. You can't just create an object of some class and expect it to have some sort of paranormal communications with others of it's class.)
(But this is a common mistake made by those who are dumped into OOP without a good background in assembler programming, et al.)
Related
I have a settings screen where you can choose between, add and remove configurations for the app.
When adding a configuration, I create a new Instance of a inputBox Class (extending the settings activity class - where I stored the procedure for the standard android text input box) to query the name for the new configuration.
In the Onclick of this inputbox a procedure from the superClass (the settings-activity) is called to create a new configuration object.
This Procedure queries some things from the activity (e.g. selected spinner element) including the progress of a seekBar.
This is where I get a NPE:
java.lang.NullPointerException: Attempt to invoke virtual method'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
The same object creation procedure is also called on initialization of the app and works just fine.
I understand from the Error that the issue is that when calling the procedure from a child class the reference of the variables to the corrseponding elements of the screen is not set anymore - and therefore cannot be queried.
So the question:
How can I query values of activity elements, when the procedure is called from another class?
I know that the topic is quite broad, but I can't figure it out for a couple of days now
Thanks for your help in advance.
Here is a scheme of the problem:
public class Settings extends AppCompatActivity{
Context settingsContext = this;
private Spinner someSpinner;
//other elements
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
someSpinner = (Spinner) findViewById(R.id.someView);
//other elements
addNewConfig.setOnClickListener((v) --> {
inputBox inputBox = new inputBox("OK", "Cancel", settingsContext, "sourcePath",1,1);
newConfigName = inputBox.show();
});
public sSetting makeNewConfig(String name, String sourcePath, int dataFrom, int dataTo){
sSetting newConfig;
newConfig = new sSetting("NAME", someSpinner.getProgress()>0, ...);
return newConfig;
}
}
And the inputBox:
public final class inputBox extends Settings {
//someVars
inputBox(String buttonOk, String buttonCancel, Context setContext, String sourcePath, int dataFrom, int dataTo){
//variable setters
}
private String show() {
//show msgbox
//onclick ok
super.makeNewConfig(....);
}
For solving the problem I restructured my Project a little:
I removed the inputBox-Part, which, after some research considered for a too complicated solution for what I needed anyway.
However: I now added a editText to my Settings View.
Although I had to change my Settings view for this, it now looks better and it was ridiculously easy to edit the configuration name for the user.
I think in most cases that will do the trick. Adding Popup-Boxes just needs more error handling and makes the design more complicated
I hope this helps ;)
If you need the code for it it is available here:
GitHub - AIM
I have multiple fragments in ViewPager. How can i get fragment first EditText Data to last Fragment?
I have set value in my first fragment like below -
txtConsAcNo.setText(account_no);
txtMeterSrMo.setText(mtr_serial_no);
Now i am getting this txtConsAcNo, txtMeterSrMo value on my last fragment like below-
ConDetFirstFragment f1 = new ConDetFirstFragment();
txtConsAcNo = f1.txtConsAcNo.getText().toString();
txtMeterSrMo = f1.txtMeterSrMo.getText().toString();
Now what i want that i am getting Null value and my app get unfortunately stopped. i want to get this data to my last fragment without bundle. how can i achieve this ?
Very Easy to Achieve this without Creating Interface, Bundle or intent -
I have declared all the variables in all the fragment "Public Static" like Below -
public static EditText txtConsAcNo, txtMeterSrMo;
After on any fragment i have declared variable to get data like below-
public static String txtConsAcNo,txtMeterSrMo;
Now i have created function to get value from first fragment in above variable below-
public static void getalldata(){
ConDetFirstFragment f1 = new ConDetFirstFragment();
txtConsAcNo = f1.txtConsAcNo.getText().toString();
txtMeterSrMo = f1.txtMeterSrMo.getText().toString();
}
Happy Coding...
There are a couple of problems here:
The first fragment may have been destroyed by the Android system to conserve memory.
Your fragments should not talk to each other directly
To achieve what you need, you need to jump through a few hoops.
Assuming that the source texts are EditText objects (ie. editable by the user), then add a TextWatcher to each of the EditText objects.
Create an Interface:
public interface TextPurveyor {
void setText1(String t);
String getText1();
void setText2(String t);
String getText1();
}
Implement this interface in the host Activity; and save the text values locally in the activity. Don't forget to save/restore them with the rest of the Activity state.
Make the TextWatcher objects call the appropriate setText(..) methods on the host activity:
((TextPurveyor)getActivity()).setText1(...);
Make each fragment check that the host activity implements this method.
When the second fragment wants a string, ask the activity for it:
((TextPurveyor)getActivity()).getText1();
To avoid coupling your project code tightly, try to use the design patterns that have been proven to work best like the Publisher/Subscriber as I will show you below:
There is a popular library I have always used in my projects called EventBus - just add the following to your build.gradle (module-level) file under dependencies :
compile 'org.greenrobot:eventbus:3.0.0'
Secondly, create a simple Plain Old Java Object (POJO) to represent your Event:
public class FragmentAToLastEvent{
private String txtConsAcNo;
private String txtMeterSrMo;
FragmentAToLastEvent(String acNo, String srMO){
this.txtConsAcNo = acNO;
this.txtMeterSrMo = srMO;
}
//getters and setters if needed
public String gettxtConsAcNo(){
return txtConsAcNo;
}
public String gettxtMeterSrMo(){
return txtMeterSrMo;
}
}
Next step is to actually use your Event class here:
So, in your fragment that you want to send text from EditText, simply do this:
String txtConsAcNo = f1.txtConsAcNo.getText().toString();
String txtMeterSrMo = f1.txtMeterSrMo.getText().toString();
EventBus.getDefault().post(new FragmentAToLastEvent(txtConsAcNo, txtMeterSrMo));
In your last fragment, simply do this to complete:
Inside onCreate or onAttach of your Fragment:
//register your event - making this class a subscriber
EventBus.getDefault().register(this)
//next, override a single method to receive the values you passed from above code (Fragment 1?)
public void onEvent(FragmentAToLastEvent event){
String txtConsAcNo = event.gettxtConsAcNo();
String txtMeterSrMo = event.gettxtMeterSrMo();
//now you can use your text here without problems!
}
Finally, remember to unregister inside onDestroy:
#Override
public void onDestroy(){
super.onDestroy();
EventBus.getDefault().unregister(this);
}
This is what I have always done and it is cleaner, without using interfaces that your fragments MUST implement and do all that!
I hope you find it helpful to you and good luck!
I have an Activity the implements the following functions:
public class SettingsActivity extends Activity {
public void setText(EditText txtBox,String strText){
txtBox.setText(strText);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
EditText txtEdit = (EditText)findViewById(R.id.txtEdit);
setText(txtEdit,"String");
...
}
So I am trying to pass an EditText Object to the setText Function and then calling the txtBox.setText() function. But this throws the following exception:
android.content.res.Resources$NotFoundException: String resource ID #0x0
However, when calling the txtEdit.setText() function within onCreate() works perfectly fine.
Edit: The function setText was a simplification, I was actually passing an object a of custom class a then called txtBox.setText(object.value), which ( by mistake) was actually an Integer, not a String. Passing a String fixed the error. I am sorry for the inconvenience.
I just realized that, in fact I did not pass a String Parameter. Like #Lubos Horacek described the exception is thrown when you are passing an int to setText. Converting the parameter fixed the error.
May be your id doesn't exist in layout file
I have two Classes. Class A is an Activity that has integer variables that need to be used in Class B (not an Activity). I have been able to use Bundles to transfer data of variables from one Activity to another Activity before. The problem is that this time, Class B is not an Activity and extends ImageView so that the draw() function can be used. Because of this, I am unable to use normal Activity functions, such as Bundle-Intents or SharedPreferences to transfer data in primitive variables from Class A to my Class B. I receive an error saying that "getIntent() is undefined for type".
So my question is, how can Class B use the variables in Class A if I am unable to bundle? Is there another way?
Someone said they did not understand my question so hopefully the below example will help demonstrate better.
public Class1 extends Activity {
//so Class1 has some primitive data, and is an Activity w/layout
int var1;
int var2;
Bitmap bitmap;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
}
}
A different class needs to draw onto canvas, but also needs to use the
information in var1 and var2 to be able to draw properly. But how to obtain that information?
public Class2 extends ImageView {
/*I am unable to use normal Activity functions, so I
*cannot onCreate, for example. I also cannot bundle,
*getIntent(), or use getSharedPreferences(). So how do I get var1
*and var2 value? */
}
Update: I was able to get this to work using getters. I attempted this before, but it was not returning the correct values. If anyone else ever gets stuck with this similar issue, remember to setup your variables with "static". I'm still learning all the differences, but without static my getter was not working. Once I added static to my variables, everything worked out. So that's one observational tip (even without fully understanding the logic as to why). Thank you to all the responders for your tips.
You can do this in different way.
First of all you can use static variable to do this. Such that you can declare a variable in class A public static String variable; and from class B you can get the value of this variable like this way ClassA.variable.
Another way you can use by passing a context of class A to B and then use SharedPreference.
Or create a new class which extends android Application. From class A you can set different variable value in application class. Now you can retreive those values from Application class. Hope this can help you.
Some code Sample using static variable
public Class1 extends Activity {
public static int var1 =20;
public static int var2 = 30;
}
Now get the variable value from class two
public Class2 extends ImageView {
Class1.var1;
Class.var2;
}
Second way using getter.
public Class1 extends Activity{
int var1 =10;
int var2 =20;
public int getVar1() {
return var1;
}
public int getVar2() {
return var2;
}
}
Now you can get the variable value in Class2
public Class2 extends ImageView {
Class1 class1= new Class1();
class1.getVar1;
class1.getVar2;
}
Also you can use SharedPreference. Hope it can help you. Thanks.
Various options exist:
The Activity can pass the information to Class B:
class B {
public void tellMeInformat(int usefulNumber) {
// Do something
}
}
Or, you can pass the Activity to the ImageView:
class A {
initiation {
B mySpecialImageView = /* Set it upo */;
B.setParentActivity(this);
}
}
class B {
private myA = null;
public void setParentActiviy {
myA = A;
}
private void doSomething {
int usefulNumber = A.getUsefulNumbner();
// Do something
}
}
can someone tell me, how do get a Stringvalue from a thread to the mainActivity?
i have a thread like this:
public class XMLHandler extends DefaultHandler {
XMLDataCollected data = new XMLDataCollected();
......
......
public String getInformation() {
String information = "";
if (data.getData().equals("residential")) {
information = "Stadt";
}
return information;
}
}
in the mainActivity i tried to set the value into a textview like this:
textView.setText(xmlHandler.getInformation());
i does not work after all. what i am doing wrong? any solutions and advices? thanks in advance
If you have a SeparateThread class then you need to create one Interface say
public interface FetchValueListener{
public void sendValue(String value_to_send);
}
And your acctivity will be implementing this interface and thus sendValue(value_to_send) method will be added to your activity.
Next step would be when you create the object of the THread class then you need to pass the object of that interface in the paramater as follows:
public class myThreadClass{
FetchValueListener mllistener;
myThreadClass(FetchValueListener listenerObj){
mllistener=listenerObj;
}
}
Now when you want to send some value to the activity from thread you can just simply call
mllistener.sendValue(value_you_wan_to_send);
And inside your actiivty you will get the value in the sendValue() method..
In that method you need to post the data to runnable using the handler so that you can make changes to the UI like setText etc.....
If you directly try to set the value of text view in that method you will get an exception.