Getting a string from EditText - android

I'm trying to get the the text written by the user in an edittext view and store it in a String value then set this String value in a class that I created that contains two String Objects but when I run the code it gives me an error with a null pointer exception because the "WrittenSubj" and "WrittenDeta" are equal to null, how can i solve this problem?
public class TaskDetails extends AppCompatActivity {
EditText WrittenSubj;
EditText WrittenDeta;
Button SaveBut;
TheTask theTask;
private Database database;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_details);
WrittenSubj=(EditText)findViewById(R.id.thesubject);
WrittenDeta=(EditText)findViewById(R.id.theDetails);
SaveBut=(Button)findViewById(R.id.SaveButton);
String subj=WrittenSubj.getText().toString();
String deta=WrittenDeta.getText().toString();
theTask.setSubject(subj);
theTask.setDetails(deta);
}

I think TheTask is your Beam class, And you have not initialize it.
Initialize this.
theTask = new TheTask();
And use
theTask.setSubject(subj);
theTask.setDetails(deta);

You have to initialize the TheTask class before using that like below
TheTask theTask = new TheTask();
And after that use object of theTask like below
theTask.setSubject(subj);
theTask.setDetails(deta);

Problem is arrising because you are initializing edittexts and getting its text getText() in method onCreate() and OnCreate() is the method called when activity is created So at the time of creation of activity your edit texts will obviously be having null/empty value.
So you must call edittext.getText() on any event click or button click after actually the text have entered.

Related

Passing an EditText Object and EditText.setText() throw Exception

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

How can i call AsyncTask from my adapter class passing String values?

textview and button in my listview by using adapter class. When i click on that button i have to call AsyncTask passing parameters i.e String of that perticular position in adapter class getview method .Here am created my Asynctask is another class i.e an activity class. Please provide some examples.
Thanks in advance.
Your AsycnTask takes an Array of some sort - Strings for instance, so when you instantiate the AsyncTask, you just pass it an Array like so:
String[] arr = new String[] {"A string to pass..."};
MyAsyncTask task = new MyAsyncTask();
task.execute(arr);
Full example on how to use it:
http://www.android-ever.com/2012/10/android-asynctask-example.html
Create public class in different file or within your activity. If you are creating within Activity then define like this way
public class static MyAsync extends AsyncTask<String, Void, String>{
}
and then used anywhere like this
YourActivity.MyAsync myAsync = new YourActivity.MyAsync();
for passing the value to the your Async class use this way
myAsync.execute(yourstring);
access in doInBackground like this way
public String doInBackground(String... param){
String s = param[0]; // here you can access you string like this way
}

get a string value from a thread

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.

Passing value from static class to non static (android)?

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.)

AsyncTask with parameters of textview

I have an AsyncTask with a textview loaded in the class so it looks something like this:
private class MyClass extends AsyncTask<TextView, Void, Void>{
}
TextView tv;
Loaded this way
"new MyClass(tv).execute();"
The reason for this is because of I have a textview loaded inside a viewflipper and I have a long load method inside the task to implement a process dialog.
My error is found on "protected Void doInBackground(TextView... params) {" this is where params is a TextView[] but not a single TextView.
Does anyone have a solution of to this problem?
Your TextView is the first element in params:
TextView tv = params[0];
NOTE:
If you plan to modify that TextView in doInbackground() don't do it because you'll throw an exception(you can't modify a view from another thread, instead use the onPostExecute method).
You can't change the method signature of the AsyncTask.doInBackground() method.
It is defined to take a varargs parameter, so you will have to pass a TextView[] parameter to your AsyncTask.
Try
Arrays.asList(tv);
If you want to pass a single TextView you will need to define a Construcotr in MyClass and then store the TextView as a field in MyClass. Beware doing this though, you should not hold references to View's or Contexts in your tasks. This will stop the Android OS from garbage collecting the activity that owns the TextView and could leat to a memory leak. If you must keep a reference to a view or context in your AsyncTask, use something like this:
private class MyClass extends AsyncTask<TextView, Void, Void>{
private WeakReference<TextView> tvRef;
public MyClass(TextView tv) {
this.tvRef = new WeakReference<TextView>(tv);
}
}
Just read params[0] as your parameter. You can pass a single value, a sequence or an array wherever you see ... in the parameter list. (A single value is actually a sequence containing just one element.)

Categories

Resources