Android. Fragment getActivity() sometimes returns null

In developer console error reports sometimes I see reports with NPE issue. I do not understand what is wrong with my code. On emulator and my device application works good without forcecloses, however some users get NullPointerException in fragment class when the getActivity() method is called. Activity pulic class MyActivity extends FragmentActivity{ private ViewPager pager; … Read more

Using the “animated circle” in an ImageView while loading stuff

I am currently using in my application a listview that need maybe one second to be displayed. What I currently do is using the @id/android:empty property of the listview to create a “loading” text. <TextView android:id=”@id/android:empty” android:layout_width=”match_parent” android:layout_height=”match_parent” android:background=”#FF0000″ android:text=”Loading…”/> Now, I would like to replace that with the animated circle that is used in … Read more

Is AsyncTask really conceptually flawed or am I just missing something?

I have investigated this problem for months now, came up with different solutions to it, which I am not happy with since they are all massive hacks. I still cannot believe that a class that flawed in design made it into the framework and no-one is talking about it, so I guess I just must … Read more

Running multiple AsyncTasks at the same time — not possible?

I’m trying to run two AsyncTasks at the same time. (Platform is Android 1.5, HTC Hero.) However, only the first gets executed. Here’s a simple snippet to describe my problem: public class AndroidJunk extends Activity { class PrinterTask extends AsyncTask<String, Void, Void> { protected Void doInBackground(String … x) { while (true) { System.out.println(x[0]); try { … Read more

Warning: This AsyncTask class should be static or leaks might occur

I am getting a warning in my code that states: This AsyncTask class should be static or leaks might occur (anonymous android.os.AsyncTask) The complete warning is: This AsyncTask class should be static or leaks might occur (anonymous android.os.AsyncTask) A static field will leak contexts. Non-static inner classes have an implicit reference to their outer class. If that outer class is for example a Fragment or Activity, then this reference means that the long-running handler/loader/task will hold a … Read more

How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

I have this two classes. My main Activity and the one that extends the AsyncTask, Now in my main Activity I need to get the result from the OnPostExecute() in the AsyncTask. How can I pass or get the result to my main Activity? Here is the sample codes. My main Activity. public class MainActivity … Read more

Handler vs AsyncTask vs Thread [closed]

Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago. Improve this question I got slightly confused about the differences between Handlers, AsyncTask and Threads in Android. I’ve read … Read more

Android basics: running code in the UI thread

In the viewpoint of running code in the UI thread, is there any difference between: MainActivity.this.runOnUiThread(new Runnable() { public void run() { Log.d(“UI thread”, “I am the UI thread”); } }); or MainActivity.this.myView.post(new Runnable() { public void run() { Log.d(“UI thread”, “I am the UI thread”); } }); and private class BackgroundTask extends AsyncTask<String, Void, … Read more

AsyncTask Android example

I was reading about AsyncTask, and I tried the simple program below. But it does not seem to work. How can I make it work? public class AsyncTaskActivity extends Activity { Button btn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.button1); btn.setOnClickListener((OnClickListener) … Read more