Whenever I'm writing an Android app and making a Toast, I tend to call Toast.makeText() but then forget to call show() and get mad at myself later. Since there is no reason I would ever call Toast.makeText() and not mean to call show(), is there a way I can make Eclipse show a warning if I do that? Thanks
for clarity: I want Eclipse to show a warning anytime I call "Toast.makeText(blah, blah, blah)" instead of calling "Toast.makeText(blah, blah, blah).show()".
You mean like this?
Lint warnings can do lots of handy things - page through them and see what suits you!
Related
I have run this method
sToast.cancel();
can I call this method
sToast.show();
I have tried it.the sToast can show in GB, but not in ICS.
Is it right?
As per the documentation, calling show() after cancel() doesn't show any Toast on screen. Do check properly.
Yes, you can use Toast.cancel(). Usually a Toast cancels by itself after a timeout so usually programmers never end up using cancel. If there is a need, you can very well go ahead and use it.
My Problem is similar to this one:
Error on dismissing ProgressDialog in AsyncTask
I have an AsyncTask that creates a dialog in onPreExecute like this:
dialog = ProgressDialog.show(activity, "login", "logging in, one moment please");
And dismisses the dialog in onPostExecute like this:
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
Still, i get error reports from users on the line where i do diolog.dismiss() with the message:
java.lang.IllegalArgumentException: View not attached to window manager
The most common root-cause is - or so i've read - when a user switches orientation (from portrait to landscape or vice versa).
My app however is forced to portrait mode, so this can not be the cause. (i've double checked this to make sure it really is not possible)
The reporter of the before mentioned post solved it in the end (and i've read this solution elsewhere too) by creating an inner class for the AsyncTask in the activity class and working with onCreateDialog from the activity class and calling showDialog from the AsyncTask. (read his post if you don't understand this)
I've begun trying to implement that, but it seems that showDialog is deprected: so this is not a solution for me.
The last solution i've found is by simply catching the Exception. I had thought of that myself too, but only as a last resort. I prefer to understand what really goes wrong and anticipate on that instead of simply catching the exception and not knowing what is going on.
You should add this in the manifest.xml
upto API level 12
<activity
android:label="#string/app_name"
android:name=".Activity_calling_AsyncTask"
android:configChanges="keyboardHidden|orientation">
after apl level 12,
<activity
android:label="#string/app_name"
android:name=".Activity_calling_AsyncTask"
android:configChanges="keyboardHidden|orientation|screensize">
</activity>
hope this will help you.
If what Chad is saying is correct - that some devices might start in landscape for a fraction of a second - then i think this is the most likely cause for getting the error-reports. While the solution of Rajendra might be able to prevent that, i've decided it really does feel like applying a band-aid as others have pointed out, since screen-rotation is not the only source of the real problem.
After some more searching, i've found two solutions:
Implementing the AsyncTask as part of a class which inherits from Application class, which is explained here: Handle screen orientation changes when there are AsyncTasks running
Implement onRetainNonConfigurationInstance() / setRetainInstance(). An example of the first is given here: http://twigstechtips.blogspot.com/2011/11/for-my-app-moustachify-everything-i-was.html - however, the method itself is deprecated, so it would be better to use setRetainInstance in a similar fashion.
And just for completeness, there's also the Droid-Fu for Android library, which claims to be able to solve this problem.
Im just coding the Android application which I thought of in order to learn the SDK a little more and ran into a problem when trying my program with the emulator. The application installs normally and then closes only to give me a logcat saying theres a NullPointerException in the main class. I cant manage to find it anywhere and research says that theres an object being used without it being initialized? I dont think this is possible as if it were the case, Eclipse would give me an error, but in this case it is a runtime exception. I do not have a lot of programming experience especially in Android and this bug is stopping me from seeing if the rest of my program works!
Heres the Source
and heres the Logcat
Thanks in advance for any help
The error occurs on line 80 in your onCreate method in IOweYou.java. You can see it on line 19 in the stacktrace you posted.
08-03 07:11:35.068: E/AndroidRuntime(681): at com.andrei.iou.IOweYou.onCreate(IOweYou.java:80)
Is localEL properly defined?
EDIT:
make your activity extend ExpandableListActivity instead of activity. Then remove setContentView. And change
localEl.setListAdapter(mAdapter);
to
this.setListAdapter(mAdapter);
and your list will show. However clicking on the item will still make it crash but its a start!
EDIT2:
here is an example I found.
http://smartandroidians.blogspot.com.es/2010/04/expandablelistactivity-in-android.html
I think the problem is that your ExpandableListActivity localEl has been created but not launched. When you call localEL.setListAdapter(mAdapter); it internally calls its setContentView, as is shown in the log. Probably getWindow() returns null because the activity has not yet been initalized.
Without knowing much of what you want to do, perhaps your activity should instead inherit from ExpandableListActivity?
(Note that I've searched online for the warnings I'm describing below, and have come up with next to nothing about them.)
I'm working with API level 10. I have a preference screen (XML-based), and one of the options in there creates a custom ListActivity as follows:
PreferenceActivity contains an option that creates a...
ListActivity which is a dialog that employs...
setOnClickListener() which contains an onClick() method that (right before calling finish()) will startActivity() a new Intent...
sub-Activity which starts up an...
AsyncTask which does variable time work which when done calls...
onPostExecute() which calls finish()
The thing is, it works... but I'm getting a raft of warning starting with:
10-16 21:59:25.010: WARN/WindowManager(170): Rebuild removed 4 windows but added 3
10-16 21:59:25.010: WARN/WindowManager(170): This window was lost:.....
Curiously, this raft of warnings ONLY comes up when the task executes quickly! When I added a Thread.sleep() call to my AsyncTask to artificially inflate its runtime it worked and threw no warnings whatsoever. In fact, as long as it takes more than (roughly) 500 ms to run it works fine. (Note that I tried using startActivityForResult() to no greater effect - the same problem occurs.)
The goal is that the user selects a preference item, they change its setting, some processing takes place, and then the user is left back at the preference menu they started on.
I'm betting it's a race condition... the order in which the windows are destroyed varies depending on that run-time... and I get the impression that when the sub-Activity closes before its parent ListActivity the warnings get thrown. But sprinkling a 1s sleep() in isn't a reasonable solution unless this is some sort of Android bug (unlikely, but then again I've reproduced a couple of those today already).
So, what's the flaw in this my that leads to this stream of warnings? It'd be nice to say "on preference, do this, then do that, then finish" but I think what I'm doing is the equivalent. Maybe not... thoughts?
Edit: I decided to try doing this ListActivity as a custom Dialog... that was one of the more painful things I've tried to do lately (getApplication() doesn't work and lots of other things seem to go wrong... it may be inexperience to some extent, but dialogs really weren't meant for this either...
Try the following two things:
Dismiss your dialog before calling finish() on its parent activity (PreferenceActivity).
Make sure you are starting your AsyncTask later in the sub-activity's lifecycle. I'm specifically thinking you should launch it in onResume().
My best guess is that the AsyncTask is calling finish() on the sub-activity, before the sub-activity has had a chance to fully start up. Why that would matter? I'm not sure. Something to try though. Good luck!
My app may launch a sub-activity for a specific purpose. When that activity finishes, I get the results in onActivityResult. These results are then processed in the subsequent onResume. This consists of a setContentView and also starting an AsyncTask that puts up a ProgressDialog.
This all works well when initiated the normal way, which is via a user request (i.e., menu selection) after the app is up and running. However, under some conditions I need to do this right as the app is starting up, so I initiate this sequence right from my onCreate. What then happens is that I get fatal ResourceNotFound errors within any o/s call that implicitly calls the layout inflater. I got around this with setContentView by pre-inflating the view in my onCreate method, but the AsyncTask's onPreExecute still fails on ProgressDialog.show() as it "fails to find" Android's own progress_dialog.xml!
Anyone know what's happening here?
I suspect it's something to do with the timing, where this is occurring before the main activity has even had a chance to display its screen. These calls are all being made on the main UI thread, but maybe something hasn't completed within the o/s under these conditions.
As a closeout, the problem turned out to be totally unrelated to what I described in my post. Turns out it was due to blindly using some code that had been posted in some online forum showing how to get and use AssetManager. Trouble is, at the end of the block of code he had put "assMan.close()". Well, this closes the asset manager for the entire activity and resources can no longer be accessed!
It took a while to find it since it was not something that I did via my own understanding.