Extending HandlerThread and when to call getLooper() - java

I have a simple Activity with a button on it. Upon the button's onClick method, I want to run some code in another thread, getting a HandlerThread's Looper object and then posting a Runnable to the Handler associated with it.
My objective is to call a thread's method from another thread (and run it from its own thread) using Handlers (probably a bad idea, but I wanted to learn this instead of abusing static methods)
So I decided to extend HandlerThread. The new class does not really do much.
public class EpicThread extends HandlerThread {
public EpicThread(String name) {
super(name);
}
}
The activity does not really do much either. I did this for some testing.
public class MainActivity extends Activity {
EpicThread et;
Looper l;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EpicThread et = new EpicThread("The epic thread");
et.start();
}
public void click(View v) {
l = et.getLooper();
Handler h = new Handler(l);
h.post(new Runnable() {
#Override
public void run() {
System.out.println("This is running on the epic thread");
}
});
}
}
So I test this by pressing the button, which gives me a InvocationTargetException when getLooper() is called:
E/AndroidRuntime(23262): FATAL EXCEPTION: main
E/AndroidRuntime(23262): java.lang.IllegalStateException: Could not execute method of the activity
E/AndroidRuntime(23262): at android.view.View$1.onClick(View.java:3660)
E/AndroidRuntime(23262): at android.view.View.performClick(View.java:4162)
E/AndroidRuntime(23262): at android.view.View$PerformClick.run(View.java:17088)
E/AndroidRuntime(23262): at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(23262): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(23262): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(23262): at android.app.ActivityThread.main(ActivityThread.java:4867)
E/AndroidRuntime(23262): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(23262): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(23262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
E/AndroidRuntime(23262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
E/AndroidRuntime(23262): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(23262): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(23262): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(23262): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(23262): at android.view.View$1.onClick(View.java:3655)
E/AndroidRuntime(23262): ... 11 more
E/AndroidRuntime(23262): Caused by: java.lang.NullPointerException
E/AndroidRuntime(23262): at com.asdf.qwert.MainActivity.click(MainActivity.java:49)
E/AndroidRuntime(23262): ... 14 more
Fiddling a little with this code, I noticed that :
Getting the Looper with l = et.getLooper(); works right after creating the thread, after et.start();.
If I use HandlerThread directly instead of EpicThread, it works regardless of when getLooper() is called.
So I have no idea why this exception is thrown, and why simply by not using a subclass things started to work.

Related

Crash caused by animation cancel()

I have 2 animations, slide[1] and slide[3], and when I try to cancel them when the animation repeats itself, it causes a crash. here is some relevant code:
slide[1].setDuration(500); slide[1].setStartDelay(500);
slide[3].setDuration(500); slide[3].setStartDelay(500);
slide[1].addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
if (!start) {
table[2][2].setBackgroundResource(R.drawable.black);
slide[1].setDuration(1000);
slide[1].setStartDelay(0);
slide[3].setDuration(1000);
slide[3].setStartDelay(0);
slide[1].removeAllListeners();
slide[3].cancel();
slide[1].cancel();
}
}
#Override
public void onAnimationEnd(Animator animation) {
}
}); slide[1].start(); slide[3].start();
The line which causes the crash is "slide[1].cancel();", and I don't know why.
Apparently the crash is being caused when the phone is running on JellyBean, but not Marshmallow for example.
How can I solve this problem?
Thanks!
UPDATE: here is the logcat:
03-13 16:18:01.943 1623-1623/com.example.ohad.squerz E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at android.animation.ValueAnimator$AnimationHandler.doAnimationFrame(ValueAnimator.java:603)
at android.animation.ValueAnimator$AnimationHandler.run(ValueAnimator.java:639)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:524)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
This crash has been fixed in AOSP in this commit.
If you want to support API 16, you need to cancel your animations somewhere else than in onAnimationUpdate/onAnimationRepeat.
In your case (if it matters, because this answer comes a long time after your question), you probably want to avoid the repeat on your first animator, using setRepeatCount(0).

My AsyncTask in another class is causing NullPointerException when it is called in this class

I am quite new to android development and I am facing a NullPointerException when I am trying to call an AsyncTask which is an inner class in another class.
I believe it is the way I am instantiating it but this is what I have:
// This onClick is in my adapter class - which is a separate Java File and class
#Override
public void onClick(View v) {
UploadRes uploadRes = new UploadRes();
UploadRes.UploadResConfirm uploadResConfirm = uploadRes.new UploadResConfirm(v.getContext());
uploadResConfirm.execute(fileName, filePath);
}
public class UploadResConfirm extends AsyncTask<String, String, String> {
Dialog dialog;
Context context;
public UploadResConfirm(Context context){
this.context = context;
}
#Override
protected void onPreExecute(){
dialog = new Dialog(UploadRes.this);
dialog.setTitle("Currently uploading");
dialog.show();
}
I believe it has got something to do with the dialog box itself being instantiated in the AsyncTask class.
The error stack trace on Logcat - its the Dialog, I think its in the wrong place...
java.lang.NullPointerException
at codeman.androapp.UploadRes$UploadResConfirm.onPreExecute(UploadRes.java:246)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Some help if any would be much obliged.
Instead of dialog = new Dialog(UploadRes.this);
Try
dialog = new Dialog(context);
your are passing a wrong context to create dialog.

ASyncTask with progressdialog and button

I am beginner and I had a test. I did all tasks, but I have a problem -
public class HttpTask extends AsyncTask<Integer, String, String> {####
ProgressDialog dialog;
Context context;
public HttpTask(Activity activity) {
//init progress dialog
dialog = new ProgressDialog(context);****
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
protected void onPreExecute() {
// show progress dialog
dialog.setMessage("Loading...");
dialog.setCancelable(false);
}
protected String doInBackground(Integer... params) {
//freeze system to 5 seconds
try {
int seconds = params[0]*5;####
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(final String success) {
// if there is progress dialog hide it
dialog.dismiss();
}
}
It crashes, when I try to compile it (I showed where are problems with * sign):
08-03 10:43:10.873 29441-29441/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
at android.app.AlertDialog.<init>(AlertDialog.java:98)
at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
at net.joerichard.androidtest.main.f.HttpTask.<init>(HttpTask.java:26)
at net.joerichard.androidtest.main.f.F_Networking_Activity$1.onClick(F_Networking_Activity.java:27)
at android.view.View.performClick(View.java:4107)
at android.view.View$PerformClick.run(View.java:17166)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5559)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
at dalvik.system.NativeStart.main(Native Method)
08-03 10:43:10.913 754-877/? E/EmbeddedLogger﹕ App crashed! Process: net.joerichard.androidtest
08-03 10:43:10.913 754-877/? E/EmbeddedLogger﹕ App crashed! Package: net.joerichard.androidtest v1 (1.0)
08-03 10:43:10.913 754-877/? E/EmbeddedLogger﹕ Application Label: AndroidTest
This is class of main activity.
public class F_Networking_Activity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_f__networking_);
// bDownload: start HttpTask
Button bDownload = (Button) findViewById(R.id.bDownload);
bDownload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
HttpTask task = new HttpTask(F_Networking_Activity.this);****
task.execute();
}
});
}
Thank you for your answers. Now I have another problem (I showed with # sign of second problems)
08-03 11:28:18.292 754-877/? E/EmbeddedLogger﹕ App crashed! Process: net.joerichard.androidtest'
08-03 11:28:18.292 754-877/? E/EmbeddedLogger﹕ App crashed! Package: net.joerichard.androidtest v1 (1.0)
08-03 11:28:18.292 754-877/? E/EmbeddedLogger﹕ Application Label: AndroidTest
08-03 11:28:18.292 30544-30726/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:864)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at net.joerichard.androidtest.main.f.HttpTask.doInBackground(HttpTask.java:40)
at net.joerichard.androidtest.main.f.HttpTask.doInBackground(HttpTask.java:20)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:864)
Actually, your context is null because you didn't initialize it.
Add one extra line inside your HttpTask:
public HttpTask(Activity activity) {
this.context = activity;
dialog = new ProgressDialog(context);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
and change Context to Activity like this:
Activity context;
Now call this context anywhere in your class.
Yes you must get NullPointer. Because your context is null.
Change this like
public HttpTask(Context _context) {
context = _context;
//init progress dialog
dialog = new ProgressDialog(context);****
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}

RejectedExecutionException when loading bitmaps for ListView with asyncTask

I'm trying to create a ListView with bitmaps that are generated on demand. I'm using https://github.com/chrisbanes/Android-BitmapCache to cache the bitmaps
I'm using a SimpleAdapter, with this :
setViewBinder(new SimpleAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Object object,String string) {
if( string.startsWith("ClassName:") ){
final ImageView yourImageView=(ImageView) view;
AsyncTask<String, Void, Bitmap> imageLoadAsyncTask = new AsyncTask<String, Void, Bitmap>() {
String cname;
#Override
protected Bitmap doInBackground(String... classnames) {
cname=classnames[0];
return db.getCharIcon(classnames[0]);
}
#Override
protected void onPostExecute(Bitmap bitmap) {
yourImageView.setImageBitmap(bitmap);
}
};
imageLoadAsyncTask.execute(string.substring(TextUtils.getTrimmedLength("ClassName:")));
return true;
}
return false;
}
}
the getCharIcon function is here :
Bitmap getCharIcon(String classname){
Bitmap modBmp;
CacheableBitmapDrawable cacheBmp=mCache.get(classname);
if(cacheBmp==null)
{
modBmp=createCharIcon(classname);
mCache.put(classname,modBmp);
}
else
{
modBmp=cacheBmp.getBitmap();
}
return modBmp;
}
where createCharIcon generates the bitmap this way (I have yet to implement the final version that selects the good portion of the image):
Bitmap createCharIcon(String classname)
{
Bitmap modBmp = Bitmap.createBitmap(srcBmp,0,0,60,60);
return modBmp;
}
Sadly I am getting this error :
07-02 14:13:04.248 11592-11592/com.lectem.gecharacters E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.util.concurrent.RejectedExecutionException
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1876)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:774)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1295)
at android.os.AsyncTask.execute(AsyncTask.java:394)
at com.lectem.gecharacters.CharFragment$1.setViewValue(CharFragment.java:110)
at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:168)
at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
at android.widget.AbsListView.obtainView(AbsListView.java:1294)
at android.widget.ListView.makeAndAddView(ListView.java:1730)
at android.widget.ListView.fillDown(ListView.java:655)
at android.widget.ListView.fillSpecific(ListView.java:1287)
at android.widget.ListView.layoutChildren(ListView.java:1573)
at android.widget.AbsListView.onLayout(AbsListView.java:1147)
at android.view.View.layout(View.java:7035)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:7035)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:7035)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1241)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
at android.view.View.layout(View.java:7035)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:7035)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1128)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1045)
at android.view.View.layout(View.java:7035)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:7035)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:7035)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
I get this error when I scroll the list back too fast, so I suppose it is linked with the number of asyncTasks I can use...
I'm really not sure if this is how I should do it or not, but the library bitmapLruCache needs to be ran on something else than the main thread.
Usually, that error means that you are trying to use too many AsyncTasks at once. In an AdapterView, this can occur if you are forking an AsyncTask for every item (e.g., every row in a ListView) without taking into account row recycling, and the user flings the list.
Quoting the library's documentation:
If you wish for the library and recycling feature to work, you MUST use the bundled CacheableImageView wherever possible.
Since you appear to be using this in an AdapterView, please make sure that you are using CacheableImageView.

How can I access a function in another class?

I am developing an app for android that has to access another class, but i don't know why it doesn't work.
When run the App in android 2.3.3 it force closes, and I don't understand why. I think that the method is correct.
Log in the force close the phone android:
> app_vercode:1
device_model:u8800
build_version:111180
condition:1
processName:beta.tester
pid:13277
uid:10088
tag:null
shortMsg:java.lang.NullPointerException
longMsg:java.lang.NullPointerException: Unable to start activity ComponentInfo{beta.tester/beta.tester.BetaTesterActivity}: java.lang.NullPointerException
stackTrace:java.lang.RuntimeException: Unable to start activity ComponentInfo{beta.tester/beta.tester.BetaTesterActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3703)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at beta.tester.BetaTesterActivity.onCreate(BetaTesterActivity.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
... 11 more
Detail logs:
EDIT: This code already is correctly.
The code:
class BetaTesterActivity:
package beta.tester;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class BetaTesterActivity extends Activity {
public TextView text1;
private teste cmd;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text1 = (TextView) findViewById(R.id.text1);
//Start the function
cmd = new teste();
cmd.start(this);
}
}
class teste:
package beta.tester;
public class teste {
//Function that I will start
public void start(BetaTesterActivity zav){
zav.text1.setText("Hello");
}
//
}
In class teste, you are creating a new BetaTesterActivity, which is useless. You need to use the instance created by the framework. Change your class teste to this:
public class teste {
//Function that I will start
public void start(BetaTesterActivity zav){
zav.text1.setText("Hello");
}
}
Then in the onCreate method of your activity class, you need to initialize cmd and then call start like this:
cmd.start(this);

Categories