Currently, I'm trying to download some contents from Web and would like to push it to ListView:
I created a Runnable to do the download content part:
Runnable postTest = new Runnable() {
#Override
public void run() {
try {
URL u = new URL("192.168.1.1/getList.php");
ContentValues c = new ContentValues();
c.put("token", networkCommunication.getToken());
String k = networkCommunication.downloadContent(u, c);
getParent().runOnUiThread(
new Runnable() {
public void run() {
Toast.makeText(Main.this, k, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
};
The code works fine until String k = networkCommunication.downloadContent(u, c);
But then, the "K" variable is not accessable in "Run" method, also, I tried changing the k variable to "Hello world" (i.e. print hello world when it finish downloading the content) but then another exception occurs:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.runOnUiThread(java.lang.Runnable)' on a null object reference
How to solve this problem?
Because getParent() is NULL. According to the documentation:
public final Activity getParent () Since: API Level 1
Return the parent activity if this view is an embedded child.
You can refer to this similar question : Android Activity.getParent() always returning null
Related
Anonymous class hold a reference to the enclosing class.
In the following example, I created a small Activity. In the onCreate method, I just add a timer on another Thread, add a CompositeDisposable and clear it in the onDestroy.
Obviously without the CompositeDisposable, it will create a memory leak. With the CompositeDisposable it doesn't create any memory leak but how is it even working ?
RxJava just interrupt the Thread and put null on every callback ? Can you provide some line that do this work in RxJava source code, i suppose it's somewhere near the dispose method.
public class MainActivity extends AppCompatActivity {
private String TAG = "MainActivity";
private CompositeDisposable composite = new CompositeDisposable();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
composite.add(Flowable
.just(1)
.timer(90, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.subscribeWith(new DisposableSubscriber<Long>() {
#Override
public void onNext(Long aLong) { sayHello(); }
#Override
public void onError(Throwable t) { sayHello(); }
#Override
public void onComplete() { sayHello(); }
}));
}
#Override
protected void onDestroy() {
super.onDestroy();
composite.clear();
}
public void sayHello () { Log.w(TAG, "Hello everyone"); }
It is precisely in the source of the dispose method. You can probably jump into the source of methods in your libraries within your IDE as well, in IntelliJ it's Ctrl+B on Windows or ⌘B on Mac, and in Eclipse it's F3.
Anyhow, here's the source of the dispose method (comments mine):
#Override
public void dispose() {
if (disposed) { // nothing to do
return;
}
OpenHashSet<Disposable> set; // this is the same type as our field that holds the Disposables
synchronized (this) {
if (disposed) {
return; // another thread did it while we got our lock, so nothing to do
}
disposed = true; // setting this flag is safe now, we're the only ones disposing
set = resources; // the references are now in this local variable
resources = null; // our field no longer has the references
}
dispose(set); // from here on out, only this method has the references to the Disposables
}
And then the complete code of the dispose(OpenHashSet<Disposable>) method that we called above on the last line (mostly just error handling which I believe is self-explainatory):
/**
* Dispose the contents of the OpenHashSet by suppressing non-fatal
* Throwables till the end.
* #param set the OpenHashSet to dispose elements of
*/
void dispose(OpenHashSet<Disposable> set) {
if (set == null) {
return;
}
List<Throwable> errors = null;
Object[] array = set.keys();
for (Object o : array) {
if (o instanceof Disposable) {
try {
((Disposable) o).dispose();
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
if (errors == null) {
errors = new ArrayList<Throwable>();
}
errors.add(ex);
}
}
}
if (errors != null) {
if (errors.size() == 1) {
throw ExceptionHelper.wrapOrThrow(errors.get(0));
}
throw new CompositeException(errors);
}
}
As you can see, at the end of that method, set can now be garbage collected, as nobody is holding a reference to it.
I have a JFace application and want to do some work on startup. So I overrode the open method of the window.
But now I have the problem that in the case of a failure I can't display an error message because the shell is null at this time. And I have no idea to solve this problem.
public class MyExample extends ApplicationWindow {
public MyExample() {
super(null);
}
public void run() {
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
#Override
public int open() {
// do some work
if (...) {
MessageDialog.openError(getShell(), "Error", "Error occured");
}
return super.open();
}
}
I would try:
Display.getDefault().syncExec(new Runnable() {
#Override
public void run() {
MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error", "Message");
}
});
EDIT:
The static method Display.getDefault() returns the default Display thread or a new one is created if it did not already exist.
On the other hand, the static method Display.getCurrent() returns the Display instance from the currently running thread, or null if the currently running thread is not a user-interface thread for any display.
See more on the Java Documentation of the Display class.
You may also want to take a look at the difference between syncExec() and asyncExec().
I have a custom adapter that it's associated with a ListView in my MainActivity class and when I press on one of the items of the List (setOnItemClickListener method) I execute an AsyncTask to retrieve the info from the database and send it into a bundle.
Therefore, I have to wait until the AsyncTask finishes to send the info retrieved in the bundle.
For this purpose, I created an interface:
public interface OnCarListener {
void onCarCompleted(String c);
void onCarError(String error);
}
And I have my AsyncTask in another class:
class findCar extends AsyncTask<Integer, Integer, String> {
private final OnCarListener mListener;
public findCar(OnCarListener listener)
{
mListener = listener;
}
protected void onPreExecute() {
}
protected String doInBackground(Integer... idCar) {
String nameCar = "";
//Here the code to retrive the info
nameCar = obj.getString("name");
//Now nameCar = "Car1"
} catch (Exception ex) {
}
return nameCar;
}
protected void onProgressUpdate() {
}
protected void onPostExecute(String c) {
if (mListener != null) {
mListener.onCarCompleted(c);
}
}
}
And I execute my AsyncTask (in my MainActivity) as follows:
new findCar(new OnCarListener()
{
#Override
public void onCarCompleted(String c) {
synchronized (c)
{
name = c;
}
}
#Override
public void onCarError(String error) {
}
}).execute(idCar);
And after executing the AsyncTask I throw the bundle:
bundle.putString("name", name);
Note: I send more info with the bundle but I omitted it to simplify the question.
It should work in my opinion but in the first click in one element of the List the name isn't being passed by the bundle, just in the second and the rest of the clicks I made at the same or in the rest elements of the List, it works.
Expected result: AsyncTask will be executed and until it finishes the rest of the code shouldn't work. It is clear that it's not what it's doing right now.
What I want to know: Why the synchronized doesn't work in the first iteration? I mean, when I have the List and I click on one of the elements of the List the information of the element it's show (in another Activity) but the value name it's not shown.
If I go back and I do one or more clicks on the same element (or a different one) from the List, in all of them appears the value name correctly.
Why in the first iteration it doesn't work?
And I have another question: Why if I quit the synchronized as adelphus said in his answer, any of the times that I click on the elements of the List the value name appears?
I tried the solution in this question: synchronized not synchronizing but still doesn't work.
What could I do? (Without changing the logic of the program)
Thanks in advance!
I'm not sure you understand what synchronized does - it creates an exclusive lock on a given object to create ordered access to a section of code. When multiple threads attempt to enter a synchronized block (with the same lock object), only one thread will be allowed to continue into the block at a time.
In your code, you're synchronizing on the String parameter c. Since no other threads will be accessing this parameter, synchronized has no effect here.
Since your interface callback is being called on the UI thread (via onPostExecute()), you can just set the bundle value in the callback:
void amethod() {
final Bundle bundle = new Bundle();
new findCar(new OnCarListener()
{
#Override
public void onCarCompleted(String c) {
bundle.putString("name", c);
}
#Override
public void onCarError(String error) {
}
}).execute(idCar);
}
So my problem is that i have a AsyncTask that scraps html from a page on a server so i used Jsoup as a library .
so the problem is that i want to set a timeout to cancel the Task if i don't receive any data from the page and display that there is a "communication error " on a toast
is there anyway to kill or stop the asynctask within it self and return a result on onPostExecute
{
private class getPageTitle extends AsyncTask<Void, Void, String> {
String title;
#Override
protected void onPreExecute() {
super.onPreExecute();
connectServerProgressDialog = new ProgressDialog(LoginScreen.this);
connectServerProgressDialog.setTitle("CheckingServer");
connectServerProgressDialog.setMessage("Loading...");
connectServerProgressDialog.setIndeterminate(true);
connectServerProgressDialog.show();
}
#Override
protected String doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(CONNECT_URL).get();
title = document.title();
} catch (IOException e) {
e.printStackTrace();
}
return null ;
}
#Override
protected void onPostExecute(String result) {
if(result!=null){
switch (title) {
case "0":
Toast.makeText(LoginScreen.this,"offline",Toast.LENGTH_SHORT).show();
connectServerProgressDialog.dismiss();
break;
case "1":
connectServerProgressDialog.dismiss();
Toast.makeText(LoginScreen.this,"Connected",Toast.LENGTH_SHORT).show();
break;
}}else{
Toast.makeText(LoginScreen.this,"Communication error",Toast.LENGTH_SHORT).show();
}
}
}}
I have a convention that I use for AsyncTask subclasses.
Define an inner interface for the client to use. This decouples the client class so that the AsyncTask can be re-used. The interface is named in the form blahblahListener.
The interface has two methods of the form blahblahCompleted() and blahblahException().
Accept a callback object (listener) that is an implementation of that interface. This is either passed in the AsyncTask constructor or set with a setListener() method.
Hold that listener reference in a WeakReference field so that if the listener goes away before the task completes, the listener can still be garbage-collected.
Define a field to hold an Exception. If an exception occurs in the background method, this field remembers the exception in order to report it to the client.
In the onPostExecute() method, check if the Exception field is null. If it is, call blahblahCompleted() with the result. If it isn't, call blahblahException() with the exception. Also check if the WeakReference is still valid.
For killing the task, you can have a timeout set on your connection. Then when your connection times out, you will get an exception, which is remembered and reported.
So using that convention, your code would look like this:
public class WebPageTitleRemoteTask extends AsyncTask<URL, Void, String> {
private WeakReference<WebPageTitleRetrievalListener> mListener;
private Exception mException;
public WebPageTitleRemoteTask(WebPageTitleRetrievalListener listener) {
super();
mListener = new WeakReference<WebPageTitleRetrievalListener>(listener);
}
#Override
protected String doInBackground(URL... params) {
String title = null;
try {
// Connect to the web site
Document document = Jsoup.connect(params[0]).get();
title = document.title();
} catch (IOException e) {
mException = e;
}
return title;
}
#Override
protected void onPostExecute(String result) {
WebPageTitleRetrievalListener listener = mListener.get();
if (listener != null) {
if (mException == null) {
listener.webPageTitleRetrieved(result);
} else {
listener.webPageTitleRetrievalException(mException);
}
}
}
public static interface WebPageTitleRetrievalListener {
public void webPageTitleRetrieved(String title);
public void webPageTitleRetrievalException(Exception e);
}
}
And your client code would look something like this, with your Activity implementing that inner interface:
.
.
.
connectServerProgressDialog = new ProgressDialog(LoginScreen.this);
connectServerProgressDialog.setTitle("CheckingServer");
connectServerProgressDialog.setMessage("Loading...");
connectServerProgressDialog.setIndeterminate(true);
connectServerProgressDialog.show();
new WebPageTitleRemoteTask(this).execute(url);
.
.
.
#Override
public void webPageTitleRetrieved(String title) {
if (isFinishing()) return;
connectServerProgressDialog.dismiss();
Toast.makeText(this, "title = " + title, Toast.LENGTH_SHORT).show();
}
#Override
public void webPageTitleRetrievalException(Exception e) {
if (isFinishing()) return;
connectServerProgressDialog.dismiss();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
NOTE: Because the listener is held in a WeakReference, you can't use an anonymous inner class for the listener, because the reference will go away almost immediately and be eligible for garbage collection.
I use this convention consistently, and the extra boilerplate code in the AsyncTask subclass makes it a lot cleaner to use in the client class.
I know there are answers for my questions but I did not understand the problem in my code.
Why I get :
java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
I'm trying to add text from a Task to a Text flow using a method in the controller class, for some reason the program fails on .getChildren() method .
Call for Spliter in the controller class:
btnSplit.setOnMouseClicked(new EventHandler<Event>() {
#Override
public void handle(Event event) {
Thread split = new Thread(new Spliter(custToSplit, Controller.this));
split.setDaemon(true);
split.start();
}
});
class Spliter constractor :
public Spliter(File f, Controller c){
this.c = c;
this.cust = f;
}
c.updateHebFlow("Dir created: "+ newDir.getAbsolutePath() , INFO_TEXT);
Part of the Controller class :
#FXML
private TextFlow hebFlow;
#Override
public void initialize(URL location, ResourceBundle resources) {
assert hebFlow != null : "fx:id=\"hebFlow\" was not injected: check your FXML file 'MainXml.fxml'.";
public void updateHebFlow(String text,String type){
normalText = new Text();
errorText = new Text();
errorText.setFill(Color.RED);
infoText = new Text();
infoText.setFill(Color.BLUE);
switch(type){
case(ERROR_TEXT) :
errorText.setText(text);
hebFlow.getChildren().addAll(new Text("/n"), errorText);
break;
case(INFO_TEXT) :
infoText.setText(text);
hebFlow.getChildren().addAll(new Text("/n"), infoText);
break;
case(NORMAL_TEXT) :
normalText.setText(text);
hebFlow.getChildren().addAll(new Text("/n"), normalText);
break;
}
}
}
Call for updateHebFlow in Spliter class:
try{
c.updateHebFlow("Script by TulTul", INFO_TEXT);
}catch (Exception e){
e.printStackTrace();
}
From what I understand I cant change the UI from other class other then the controller so Ive made a method in the controller class that will make the changes and call it in the Task class, why I'm getting this exception?
If this is wrong what is the wright way ?
From what I understand I cant change the UI from other class other then the controller
Actually the correct statement is: "You cannot change the UI from any thread other than the JavaFX UI thread". So, the solution is to use Platform.runLater() from the Splitter as:
// Java 8
Platform.runLater(() -> {
c.updateHebFlow("Script by TulTul", INFO_TEXT);
});
// Java 7
Platform.runLater(new Runnable() {
public void run() {
c.updateHebFlow("Script by TulTul", INFO_TEXT);
}
});
Platform.runLater() is guaranteed to run the blocks in the JavaFX UI thread and in call order.