Android java trouble with start new activity - java

In MainActivity starts another activity via startActivityForResult
Intent intent = new Intent(MainActivity.this, AutorisationForm.class);
intent.putExtra("req", 1);
startActivityForResult(intent, 0);
Method onCreate executes successful, activity displays on the screen and then app crash. Eclipse returns "source not found" error.
How I can solve this problem?
P.S. All activities declared in manifest.
P.P.S All worked successfully before I add a lot of logic in MainActivity. This code and second class hasn't changed.

Problem solved. Mistake was in onDestroy() method in MainActivity. In this method app try to save unloaded file. Theme closed.

Undo all changes before adding "a lot of logic in MainActivity."
Add changes one at a time and test in between until you discover problem.
Problem discovered!

Related

How do I launch a specific (explicit) activity of another app

I want to launch a specific activity of another app from my app. For example, on the onCreate of my app, I want to launch the activity named Rolling (not main) activity of com.pas.webcam.pro. I have heard that you must have control of both apps to do this because you must add an intent filter to the manifest of the second app. This is not true though, because activity launcher apps in the Google Play Store can launch the Rolling Activity of IP Webcam Pro.
The Activity Launcher app is open source, so I tried reviewing the source code here. It was too complicated though, so I could not figure out how this app magically launches this activity. There are many other questions like this on Stack Overflow, and I have read every one. I have also tried lots of the code from the answers too, like this:
Intent intent = new Intent(); intent.setComponent(new ComponentName("com.pas.webcam", "com.pas.webcam.RollingActivity")); startActivity(intent);
I have also tried variants of this code from other posts. My app always crashes and I get variants (depending on the code I use) of the following error:
An error occurred
Invalid intent operation. Unable to find explicit activity class {com.pas.webcam.pro/com.pas.webcam.pro.Rolling}; have you declared this activity in your AndroidManifest.xml?
I have put both of the following in my Android Manifest and the same thing happens:
<uses-permission android:name="android.permission.GET_INSTALLED_PACKAGES" />
<activity android:name="com.pas.webcam.pro.RollingActivity"
Thanks in advance for any answers, I really appreciate it, as I have been working on this problem for a while.
Edit: Here is the activity of the app I want to launch: https://i.stack.imgur.com/Fa7Xq.jpg
Edit: David Wasser helped me solve the problem by giving me the code neccessary to solve the problem. It actually works! To anyone who wants to launch a specific activity of another app with code, please use this:
Intent intent = new Intent(); intent.setClassName("com.pas.webcam.pro", "com.pas.webcam.Rolling"); startActivity(intent);
You may replace com.pas.webcam.pro and Rolling with the app and activity of your choice, but this method truly works. Problem Solved!😀
Try this:
Intent intent = new Intent();
intent.setClassName("com.pas.webcam.pro", "com.pas.webcam.Rolling");
startActivity(intent);
Since you refer to the app as "IP webcam pro", I'm assuming the package name is "com.pas.webcam.pro" (found by Internet research).

Activity reopening is working only once

I have receiver and when some action is happening I need to reopen current activity(I am in HideSettingsActivity and I want to close that Activity and open the new one HideSettingsActivity). For this I'm just finishing current activity and open the new one via intent. Code below.
Intent reopenCurrentActivityIntent = new Intent(this, HideSettingsActivity.class);
reopenCurrentActivityIntent.putExtra(CURRENT_PASSWORD, passwordDialog.getPassword());
startActivity(reopenCurrentActivityIntent);
finish();
The problem is, that it's working only for first time,when receiver is gettings some action. Next times, opening new activity is not working. Other lines of code, which are before and after that piece of code, which I described above, they works fine.
So the question is, why it is happening like that? And maybe there are some others way, to reopen current activity?
Add flag intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)); to your intent.. like :-
reopenCurrentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
then,
startActivity(reopenCurrentActivityIntent);
and its better to use this :-
Intent reopenCurrentActivityIntent = new Intent(HideSettingsActivity.this, HideSettingsActivity.class);

When bringing Android activities from the stack to the front, how do I refresh (reinitialize) them? So to run onCreate again etc

If I'm bringing Android activities from the stack to the front, how do I refresh them? So to run onCreate again etc.
My code below, in conjunction with setting activities in the Android manifest to android:launchMode="singleTask" allows me to initiate an activity if that activity is not already active within the stack, if it is active within the stack it is then brought to the front.
How do I then, if the activity is brought to the front refresh it so that onCreate is ran again etc.
Intent intent = new Intent(myActivity.this,
myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
I think FLAG_ACTIVITY_CLEAR_TOP will resolved your problem:
Intent intent = new Intent(myActivity.this, myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I don't think there is an explicit way to refresh onCreate, perhaps you may want to add the code you want reloaded into onResume.
This workaround may work if you want to keep your code in onCreate.
finish();
startActivity(getIntent());
If all you want to do is refresh the content, you should override the onResume method, and add in the code to perform the refresh in this method. To do this, use the following code within the activity that you want to perform the refresh, (ie, not the same activity that you are calling startActivity() from):
#Override
protected void onResume(){
super.onResume();
//add your code to refresh the content
}
Tip: If you are using Android Studio, press Alt+Insert (while you have the Java file open), then click Override Methods, find onResume, and it should provide you with a basic template for the method.
The diagram I added shows the order that the methods are run (this is known as the Activity Lifecycle). onCreate() is run whenever an Activity is first created, followed by onStart(), followed by onResume(). As you can see, when a user returns to an Activity, onCreate() is not run again. Instead, onResume() is the first method that is called. Therefore, by putting your code into the onResume() method, it will be run when the user returns to the activity. (Unlike onCreate(), which will not be run again).
Extra info: Since you will be initially setting the data in onCreate() and then refreshing it within onResume(), you might want to consider moving all of your code used to initially set the data to onResume() as well. This will prevent redundancy.
Edit: Based on your following comment, I can give the following solution:
I'm wanting to properly refresh the page, e.g. if there is a variable count initialised at 0. And though running the activity it's has became equal to 300. When the activity is called (intent) then refreshed, count will once again be equal to it's initial value. Do you know how to do this?
Without your current activity's code, there is not much to work with, but here is some pseudo-code as to how I would accomplish your problem:
public class MyActivity extends Activity {
TextView numberTextView;
int numberToDisplay;
#Override
protected void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(myContent);
numberTextView = (TextView) findViewById(R.id.numberTextView);
numberTextView.setText(numberToDisplay+"")//converts the integer to a string
}
#Override
protected void onResume(){
super.onResume();
numberToDisplay = 0;
numberTextView.setText(numberToDisplay+"");
}
}

Puzzling java.lang.NullPointerException in Android App

I'm currently working on an Android App and, almost every time I use it, I get a an error. Most of the time it doesn't crash the app, but it is still frustrating that I'm seeing this error. I thought I did a check for it, but I could be wrong. Thanks for the help! The relevant code is below. (It's relevant because the line outside the if statement is throwing the NullPointerException.)
Activity activity;
if(activity == null)
{
activity = new Activity();
}
Intent intent = new Intent(activity, Service.class);
You don't usually instantiate the Activity class in this manner. Please see the documentation on the Android Activity class here:
http://developer.android.com/reference/android/app/Activity.html
You should post some more of the surrounding code, but your problem is that creating new Intent requires a valid Context. Usually you create an Intent within an Activity (or Service or BroadcastReceiver) class so you can just do something like:
Intent intent = new Intent(this, Service.class);
Occasionally you'll create it somewhere else and pass it that valid Context, but you should pretty much never create an Activity by calling the constructor directly. There's a lot of other initialization necessary to make it useful.
As postet previously there is more to initiate for an activity than calling the constructor. Probably you get a null pointer exception deep within the Intent Constructer where it is trying to get some of the Activityinformation usually provided.
If you really want to create a Service, heres a link for starting a Service, but you should really read the whole article and probably some more of the activity lifecycle ressources.
http://developer.android.com/guide/topics/fundamentals/services.html#StartingAService

How do I use Startactivity and leave the calling Cocos2d Layer active (Android/Java)?

I'm trying to port my iPhone Cocos2d game to Android using the Cosos2d for Android framework (Java).
I'm running into a problem when I start PreferenceActivity from my main activity. This is my setup:
(1) HelloWordActivity.java:
Inits the Cocos2d environment and starts GameLayer.scene
(2) Gamelayer.scene:
Presents the user with the game but also has a button to show the settingspane which gets called from the Gamelayer class like this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(helloworldactivity, ShowSettingsActivity.class.getName());
helloworldactivity.startActivity(intent);
This shows the Preference Activity just fine, but when closing the preferences and returning to my game it appears it did not persist (it get's reloaded/restarted and does not retain it's state).
How can I present the user with a standard Android preferences screen without destroying the current game session in Cocos2d ?
from the names of your classes , I'm assuming you used Dan's tutorial as a starter
make sure you remove dan's onStop() override method , that gets called when you are loading the preferences and it calls
CCDirector.sharedDirector().end();
removing this should do it for you
I did notice that you gave up and switched to andEngine , that's a nice engine too but I thought I'd answer this one anyway
You need to get the main activity to start new activity, I hope this code will help you
Activity context = CCDirector.sharedDirector().getActivity();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(helloworldactivity, ShowSettingsActivity.class.getName());
context.startActivity(intent);

Categories

Resources