I had an idea to do a time tracking program using the back screen, so thought I'd knock it up and see how it is. But no, not so simple.
For background, I've actually released a few games on Android, but they were a few years ago and were all in C++. They only used Java to open a GL context and connect to input and audio. The build system was a custom one and I didn't go near Eclipse.
So I downloaded Android Studio, made a copy of the yp_hello project and got it all building and running. Except that it crashes on startup every time.
This:
setBSContentView(R.layout.bs_activity_main);
is immediately followed by this:
mTransferredText = (TextView) (findViewById(R.id.bs_sub_text));
But it crashes here with java.lang.ClassCastException: java.lang.reflect.ArtMethod cannot be cast to android.widget.TextView
However, it doesn't crash if I put in a breakpoint in and run it.
Without the breakpoint, a Log print shows that findViewById returns an java.lang.reflect.ArtMethod object.
With a breakpoint anywhere in the code (even after the crashing line), the same Log print show that findViewById returns an android.view.TextView object. Once the breakpoint has been hit, I can continue the program and it all works fine on the phone.
Can anybody help with getting this working? It's just a yp_hello sample that comes with the Yotaphone SDK, made to run in the latest Android Studio with the latest SDK.
Related
Hey all, i am starting to find out about creating apps. I have started with creating a simple webview app that shows a website.
What i am having trouble with is after editing the files in eclipse i hit run an in the SDK emulator all i get is a black screen wit android write on it.
Any ideas?
NOTE: i am very new to this
Be patient it takes like 10 mins or more for the emulator to load
Novice here.
Yes, it will take sometime. Here it starts quickly (core i5 notebook). But it may take a long long time.
You can also use your phone to debug and deploy. It's very very quick, takes virtually no time to load, like the emulator. At least here.
Good luck!
Thanks all it did take a while was juts me being inpatient.
Follow-up question: When debugging an app in eclipse, if you change your source during a debug session, it should restart my app/activity/whatsis, right?
Yeah.. not getting that at all, and my current dev box leaves something to be desired in the horsepower department. Those emulator restarts are killing my interest in working on this pet project of mine.
I found a link that should let me use my Evo directly, hopefully that will do the trick.
--Mark
Sent from my PC36100(Evo) using XDA App
Update:
Grr. I installed the HTC Sync 2.blah, so I can actually connect to my Evo now from within Eclipse but I'm still running into problems. Eclipse still isn't automagically updating my app when I change-n-save. Am I just expecting too much here?
Oddly enough, it DID update the changed app when I unplugged my evo from my laptop and plugged it back in. This took several seconds to catch up, but it beats the hell out of waiting for a new emulator to boot. Ouch. We need some way to freeze/save the emulator state so we can relaunch it with it's memory and so forth already in place.
I'd mangled my first hand-rolled "hello world" app (started monkeying with stuff without knowing what did what), so it no longer ran. Note To Self: "Test Only" flag seems to mean "emulator only". That was a snag.
So I whipped up a demo app (the "Skeleton App" for 2.1) project. Installed and ran fine. I changed a couple buttons to Log.d(...) instead of What They Did, and after the unplug-plugin trick, the app updated. The buttons no longer performed their original tasks... instead they now do what looks like NOTHING! LogCat isn't showing my debug messages at all (which wasn't a problem previously when I was just using the emulator).
Am I just missing the boat here or what?
Hello all,
I've written a simple audio player app and testing it in Samsung Galaxy S.
It is a simple app with can create playlist and some buttons to play the mp3 file.
However, there's a funny bug in it.
After starting the app, whenever I rotate the phone, the running app will launch another instance of the app. If I rotate again, another instance (3rd) would be started. So u can hear 3 copies of the same audio being played simultaneously.
What could be wrong with it?
Is there some code which can prevent program re-entry?
Thanks.
It's not a bug, that's the way andorid OS works. You need to account for this in OnCreate for your activity. I assume you are spawning off the actual playing of the MP3 in a thread or service. You need to check if the service is already running in OnCreate and attach, else spawn a new one.
Hello Gene,
How do I check if an activity is already running?
I could not find the answer on the android developer page (the topic on application fundamentals).
And no, I'm not converting it to a service yet as this is my first app.
Haven't explore service yet.
Thanks.
I read on another article, a simple way to prevent this is to add overwrite the onConfigurationChanged function
@Override
public void onConfigurationChanged(Configuration newConfig) {
//ignore orientation change
super.onConfigurationChanged(newConfig);
}
and modify the androidmanifest.xml
<activity android:name="selectCategories" android:configChanges="orientation|keyboardHidden"></activity>
But it still launch multiple instance of the app.
Thanks.
This is a good question and should be in an FAQ somewhere.
As already mentioned, changing the display orientation basically restarts your app. Read the Android dev page on Activity lifecycle for more info.
A short answer for your question is: the Bundle parameter for onCreate() will be null when your app is first run. When your app is paused and restarted, that Bundle will be non-null. You can store data in that Bundle by overriding onSaveInstanceState(), then check for that data in onCreate(). It's a good idea to learn how to do this (save/read app data on pause/restart). Once you start testing apps by rotating the display at various times, you'll find a lot of them FC at unexpected places.
This is indeed a topic that keeps surprising people who are new to android (ahem, like me 4-5 months ago ).
The solutions above are perfect, however in certain situations there's another trick that might make your life much easier. I suspect it won't help you in this case, but it might help others who tackle the problem and see this thread.
Certain applications, mainly games, should be fixed in a single orientation. I.E. you won't be playing angry birds on portrait - that game is locked to landscape, as it should be. In order to lock your activity in a certain orientation you add this attribute to the manifest under the Activity tag. So a standard activity might look something like that, combined with the ignore tag from the previous posts:
Code:
<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden"
android:name="whatever">
The great thing about this combination is that your activity will not restart at all when the phone is rotated. Of course, for a standard activity you'd usually want to support both landscape and portrait, but if you need your app to be in a fixed orientation this is the way to go - no weird FCs or annoying bugs
Hi,
How do I check the bundle?
And also, what to do if the bundle is not null? Just return?
Thanks.
regards
r_p_ang said:
This is a good question and should be in an FAQ somewhere.
As already mentioned, changing the display orientation basically restarts your app. Read the Android dev page on Activity lifecycle for more info.
A short answer for your question is: the Bundle parameter for onCreate() will be null when your app is first run. When your app is paused and restarted, that Bundle will be non-null. You can store data in that Bundle by overriding onSaveInstanceState(), then check for that data in onCreate(). It's a good idea to learn how to do this (save/read app data on pause/restart). Once you start testing apps by rotating the display at various times, you'll find a lot of them FC at unexpected places.
Click to expand...
Click to collapse
Hey guys, I'm fairly new to android applications. I am specifically working on an android application that runs for the Notion Ink Adam. A guy by the name of Mr.Guy actually wrote the app nearly perfectly and provided the source to me but there is a small issue I want to work out, hopefully with some help.
Basically the app runs from a search button long press, runs a native linux command (which toggles the backlight on and off) and closes.
The problem with the current application is that if you have somethings open in a current window they are closed by this application running. An example is opening the app drawer on the homescreen. Running the application closes the app drawer. In some cases th interruption makes a greater difference like when viewing media.
Anyway heres the source, ftp://eto.homeip.net/Shared/NotionInkHacks/PQiToggle.zip
I think I need the application to run a service instead of an activity but this didn't seem to matter. I'm pritty sure I need it to not call a UI and just run the service.
An ideal fix for this is to skip the application and just rely on keycodes run from init.rc but trying to do this I had zero success. I'm not sure if the version of init in my rom supports keycodes.
If you could help me either with making it a service or allowing a native linux command run from keycodes I would greatly appreciate it. Thanks!!!
You can call a service directly from a broadcast receiver which can listen for the "on search long press" intent
http://developer.android.com/guide/topics/fundamentals.html#ActivatingComponents
http://developer.android.com/reference/android/content/BroadcastReceiver.html
So, I'm developing an application for Android which will only be used in my own couple tablets which are going to public use.
I need an application which keeps track which (possibly 3rd party)application is foreground, and when that application goes to background the service starts activity which will go foreground and show something to the user(for example like some ad, or some review window which asks for start rating).
In Android 5.0 access to logcat is restricted so it needs to be done trough accesibility services I guess? Or is there way to use custom ROM with access to logcat? Or could rooting open it?
Thank you.
As for logcat, root allows access to it
You can get running apps using this piece of code
Code:
ActivityManager am = (ActivityManager) mContext .getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity .getPackageName();
and use an if conditition and a thread to keep checking, at the moment its not meeting the condition the desired code will be triggered
First of all I know that this title creates a lot of questions.
The problem is exactly what the title says.
While developing apps, you make mistakes (NullPointers etc)
They are thrown when the occur, normally.
Today, it's different, sometimes, when Android (Studio) feels like it wants to help me, it redirects the debugger to a class that is used to "crash" the app at an exception.
With the cool features of Android Studio, you are able to see the content of the strings during debugging. With that feature, I've been able to solve most of the problems.
But now, I ran into a problem that doesn't have the solution above.
When the app "crashes" the screen stays white, and nothing is outputted in any logcat.
All it says is
"D/AndroidRuntime: Shutting down VM"
Is this a bug in Android Studio or in Android?
Hello,
First of all make sure that you have the latest version of Android Studio.
When the app crashes is selected application above the logcat (there is a spinner which says you what app is being displayed, does it say 'com.yourpackagename'? or does it say 'No Application'?) If so I think there is a bug in Android Studio, I have faced it too several times. When your app crashes it force closes the app?
You can try these things:
Unplug your phone and plug it again and wait for a confirmation on the device's screen.
If the confirmation dialog is not showing up, while the device is connected via USB, go to the Settings and disable the USB debugging mod. Wait a few seconds and re-enable it. Wait for confirmation on the device. If still not shows. Close the Android Studio and repeat the above steps. Reopen the Android studio. Run your app, if still not getting logcat you can try getting it manually like this:
Go to the folder where adb is installed (Normally on Windows C:/Users/Username/Appdata/Android/sdk/platform-tools) via terminal with admin rights .
Run cmd.exe as admin, type:
cd ../../Users/Username/Appdata/Android/sdk/platform-tools (where Username is the username of your windows account.)
Type adb devices while your phone is connected via USB with debugging on
See if it says unauthorized, if not, you are good to proceed to the next step.
Type
adb logcat > mylogcat.txt
And quickly open your app to go to the app that causes the crash. Try to cause the bug. When the bug occurs press Ctrl+C
Now your logcat is in the mylogcat.txt within the same folder of adb.
If you need more help extracting the logcat via the above way (manually) let me know
Hmm, at the moment the crash occurs, the app freezes and stays white. Only the main thread though. I've checked the logcat in Android Studio, the app is my app, and the doesn't really matter. It shows nothing.
No logcat not even with the cmd option.
Could it be that my custom rom is causing difficulties?
Thanks,
Tim
Try to place breakpoints in every line on your code. Try step by step to run the app line by line and where the execution flow jumps some lines of code that it shouldn't or stucks at a line of code whithout going to the next lines of code that it should, there should be the problem.
Are you doing intense work on the main thread? I had experienced an issue where I was doing heavy work on a view's onTouchListener and because the touch events were bottlenecking, my UI stuck, and on logcats I didn't got any error, but got a warning that the thread is shutting down due to the fact that the onTouchListener was busy to handle the touch event.
The best option and the most reliable you have now is to debug your app line by line as mentioned above. If it does not help, try commenting lines of code where you think you may be doing intense work or inside listeners and see if your app still crashes. Then uncomment blocks of code and run again and again to see until it crashes again. In the last block of code that you uncommented should be the error.
That way you can limit your search on these lines of code.
If you could further provide any detail of what are you doing in the main thread or any sample code, I will be able to help you further
I do not think that it is related with the custom rom, otherwise logcats would have some errors of missing packages etc
Well, using breakpoints I was redirected to the piece of code that is used to output exceptions. I didn't had a breakpoint there so I don't know why it went that deep. I'm doing heavy for with the runtime.exec command.
I've discovered the exception that was thrown when I posted this question, and the exception wasn't printed anywhere so I don't know why this isn't working. I was getting the java.lang.NumberFormatException exception. This will cause the app to crash, right?
If not how do I 'enable' that?
Thanks
If you are catching that exception the app will not crash. (try catch block). It may crash when any variable from that try catch block had that Number format exception, but when referencing it from outside the try-catch block.
Numberformat exception is occured when trying to convert String to Integers or a Number, and the string does not match the Integer format. E.g. Integer.parseInt("4.3") will throw that exception.
To enable the crashing of the app during that exception, do not catch that exception (NumberFormatException). If you are catching Exception (it is a superclass of NumberFormatException and is more general, it includes this exception too) then again your app will not crash.
If you are catching any of these exceptions, your app will not crash (may crash inside the catch or outside the try catch block)
Exception
RuntimeException
IllegalArgumentException
NumberFormatException
Possibly, a string conversion to Integer or Long or Double or Float may be causing your problem The errors are "silenced" due to the try-catch block Are you printing the exceptions with e.printStrackTrace in the first line of the catch block? (e is the Exception caught)
Hmm, I'll take a look at my code so check if I used a try and catch block.
Thanks!
It was the fault of Google Analytics, I had exception reporting on and that was causing the app to freeze instead of crashing and showing the exception.
Thanks!!