I'm new to Android app development and Java in general. I have been watching some tutorials but decided it was time to start doing something on my own. I'm trying to make a fat caliper calculator where I insert all the info in one activity, calculate it and then pass the results on to another activity for displaying it. For some reason I'm getting a force close every time I hit the calculate button and I'm not sure what's causing it or which activity.
Here's the code:
MainActivity
Display
Can you see what's causing the force closes?
Thanks.
The problem is you are starting display Activity but not passing bundle to the Display Activity.
Change your code like this when you are starting Display Activity.
Intent a = new Intent(MainActivity.this, Display.class);
a.putExtras("giveResults",packet );
startActivity(a);
I believe its because where you are starting your activity, and your bundle, your not adding the bundle to the intent.
You would need to do something like
a.putExtra(packet)
before you start the activity
Take a look at this answer. It describes LogCat which is the best way to debug android code.
https://stackoverflow.com/a/3280126/771999
If you use LogCat you can usually pinpoint the exact line number of your issue.
Related
When using the intended(IntentMatcher) from the Android Espresso API, is it possible to do this twice in the same Activity?
So for example I click a button which starts an Activity for result. I check that this Activity has fired using intended(IntentMatcher). That works.
However, when I get the result, I want to fire off an Intent for a different Activity. In this case just a local Activity in the same app package with no result.
When I do this manually in the app it works fine, but Espresso can't seem to detect the second Intent in my test. What I am I missing or is this not possible? Alternatively, how should I be doing it? Maybe my design is bad.
When I do the check I'm trying this:
intended(toPackage(<packageName>));
intended(hasComponent(hasClassName(<className>)));
The first line matches but not the second one. And even if the second line is not completely correct it never seems to show anything in the error log about the second Activity I'm actually starting.
Another thing adding to the confusion is that two intents are definitely being fired as it shows that in the log. They both seem to be the same one but with slightly different details - one is a package, one is a component. Does it log the result from the first Intent as an Intent in itself? Sounds unlikely but where is this other intent coming from? I know it's not the second Activity I'm launching as it still fires even when that Activity isn't called (when the first Intent result is a fail).
I've also considered that maybe it's not getting detected because it's not waiting long enough for the second intent to fire. If that were the case, what would I do about that? I don't see much talk about handling time sensitive things in Espresso. Like checking if a progress bar is shown but then hidden again while not pressing anything. How do you do that? Maybe it's the same answer.
Any help appreciated!
Ok I found the problem. My IdlingResource wasn't working.
After fixing that it works like a charm :)
I have two Android apps that communicate with each other through BroadcastReceiver. But if the second receiver application wasn’t started, the first one just sits there... The second application, once started, needs to continue running indefinitely, but not interrupt the first application by popping up over it. So I need a way to start the second application through the first one, BUT the screen has to continue showing the first application.
Through the first app, I check for presence of the second application, and if it's found and is not running, I start the application:
Intent SecondApp = getPackageManager().getLaunchIntentForPackage("com.example.app2");
startActivity(SecondApp);
But this brings up the second application to the screen ontop of the first one. So I need a solution to ether
1) Start the second application under the fist one
Or
2) Put the current first application immediately one on top of the second app
What’s the best solution here, and how to do it?
I assume that your second application has a service running in background and when you do the startActivity(SecondApp); pass some parameters with the intent, and in the second app read this parameter and just call finish after starting the service if that parameter is present.
Thank you guys for your help. The command finish() surprisingly does get rind of the UI, but doesn’t kill the application.
There was still the issue of only “minimizing” it when I start it from scratch through the first app, and run the second application normally in all other cases. So I added a boolean flag to the first application intent:
Intent SecondApp = getPackageManager().getLaunchIntentForPackage("com.example.app2");
SecondApp.putExtra("StartMinimized",true);
startActivity(SecondApp);
In the second application onCreate I put this code in:
Intent glob_intent = getIntent();
boolean startmini = glob_intent.getBooleanExtra("StartMinimized",false);
glob_intent.putExtra("StartMinimized",false);
if(startmini)
{
finish();
Toast.makeText(this,"L",Toast.LENGTH_LONG).show(); //Inform that app started
}
Now when starting the first application, the second application only flashes a white screen for a moment. I think this is good enough.
I found that substituting finish(); with super.moveTaskToBack(true); also works, but can cause glitching later. This way I personally started the second application twice, or caused the introduction dialog to not display when making the application one instance only.
I'm designing an app composed two activities. The first one always run, and is asked to trigger a second one when some stuff happens. This works fine with the standard code used for running activities:
Intent myIntent = new Intent(this, allarme.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
however, the activity in allarme.class is not started if i'm using another app (i.e. gmail),
whilel it works perfectly from home or when the screen is locked.
I'm sure that the first activity is still running, it's just that the second action is not triggered.
Should I change anything in the manifest file to fix this?
I'm not really clear about what you want.
but I guess you want to run two activities simultaneously, am I right?
while one activity run in background and one activity update the UI.
always keep in mind that Android architecture only allowed one activity to run at the time. If you want to pass the data from Asynchronous Task, you can call the method from that class and let your handler 'recent' class update the UI.
You can create a background service that always run at background even if you are using another app or phone is locked. Here is link for more help :
http://developer.android.com/training/run-background-service/create-service.html
Thanks everybody, I think I solved it.
Basically, I found out that an activity already running can be brought to focus when re-started with a basic startActivity. Then I can immediatley switch to the new activity without losing the focus.
I'm using a singleTask-mode activity that handles a certain type of files by specifying a intent-filters.
I need to handle each such file Intent exactly once. The problem is onNewIntent() is only called if the task is already alive, forcing me to handle the intent from onCreate() too. Unfortunately, onCreate() gets called for a whole bunch of reasons (e.g. screen rotation), and the Intent returned by getIntent() may be the same one across several onCreate()'s.
Of course, it is possible to work-around this using some ugly hack, but I was wondering what the elegant solution would be.
So far the best solution I came up with is to setIntent(new Intent(Intent.ACTION_MAIN)) every time after handling an intent. This is a similar pattern to how web servers redirect you to a GET page after a POST page to avoid redoing an operation as result of refresh.
Thanks!
I am not sure If I get your Problem, but once you call an intent and start the new activity,
immediately after call finish(); to end the activity you are leaving. This will end your last activity and will prevent from multiple activities from running at the same time.
Also if you are using screen rotation as a way to launch activities, you can always control which one's you do not want to start multiple times by setting some checks using "If and Else" Statements.
remove singleTask will solve your problem;
call remove singleTask's activity, can replace with:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
How about setIntent(null) after handling it?
I have two apps that both extend activities and work perfect on their own, but when I call intent of one to view data in the other the app crashes. I have taken the second and made it a separate java file in my app. Also, I have updated the manifest to include this new activity. Where do I call the intent? If all I want from the second activity is for it to get data and return data should I use startactivityforresult instead? The main thing I want to do is get sensor data from the orientation sensor and show it on my display while still running the original activity.
This is how I call the second activity:
Intent intent = new Intent(this, LeanAngle.class);{
startActivity(intent);
Maybe the problem is where I am calling it. Is there a specific place to call it? Also, it has the same view as the main activity does, r.layout.main.
You are calling it correctly. I assume your placement is wrong. It will only work if it is the last thing you call in onCreate(). If that is indeed where it is located.