For example I have two activities MainActivity and NextStartedActivity.
onCreate() method in MainActivity
...
Intent intent = (new Intent(this, NextStartedActivity.class).putExtra("start", 1));
startActivity(intent);
For MainActivity this invocation works fine
activity = Robolectric.buildActivity(MainActivity.class).create().visible().get();
onCreate() in NextStartedActivity
...
request_status = getIntent().getExtras().getInt("start");
textureView.setSurfaceTextureListener(this);
...
When I use invocation like above for NextStartedActivity, I have
java.lang.NullPointerException at NextStartedActivityTest.java:89 but when I use
activity = Robolectric.buildActivity(NextStartedActivity.class).get();
all works fine.
Please, give me understanding in differences of this ways with different activities. And is this equal to setupActivity()?
If the NPE is occurring on this line:
request_status = getIntent().getExtras().getInt("start");
then the problem is that you are not creating an intent in your test, which your code expects. Try something like this:
Intent intent = new Intent(Robolectric.getShadowApplication().getApplicationContext(),
NextStartedActivity.class);
intent.putExtra("start", 1);
Robolectric.buildActivity(NextStartedActivity.class).withIntent(intent).create().get();
Related
I'm trying to send HashMap to start new activity with startActivity(intent).
I followed the answer from this link:
How to send hashmap value to another activity using an intent
But I still get error
Of course, i tried to send String
intent.putExtra("test","some String");
startActivity(intent);
And it worked
My code:
HashMap<String,Diner> dinersOrdersHasMap = new HashMap<String,Diner>();
FillHashMap(); // Fill the HashMap with data
Intent intent = new Intent(this, BillForm.class);
intent.putExtra("dinersOrderHashMap",dinersOrdersHasMap);
startActivity(intent);
The exception:
"Could not execute method for android:onClick", e);
e.detailMessage = "Parcelable encountered IOException writing serializable
object (name = com.example.myfirstapp.Diner)
When i call
startActivity(intent);
is your Diner class Serializable or Parcelable ?
Could you post here your Diner class ?
I have a activity where you select a number in a spinner(dropdown-list in AndroidStudio) and sends it to a new activity/another class, before it is sent to a server. The array-adapter works fine, but using the getExtra Intent in the receiving activity is a lot of trouble for me.
The app crashes and logcat give this message:
NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference.
MainActivity.(MainActivity.java:76)
Line 76 in MainActivity String avd_nr= getIntent().getStringExtra("getData");
This is my code for passing the array value, and the line Log.i("data",avd); posts the spinner value(avd) in the logcat.
btnAvdeling.setOnClickListener(new View.OnClickListener()
{
final String avd = dropdown.getSelectedItem().toString();
#Override
public void onClick(View v)
{
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("getData",avd.toString());
startActivity(i);
Log.i("data",avd);
}
}
This is my code where I receive data
String avd_nr= getIntent().getStringExtra("getData");
private SurveyResponse fillInResponsData(Integer answer) {
surveyResponse.setAvdeling(avd_nr);
return surveyResponse;
}
Please give some help on what i need to add or change
in onCreate() Method put the following :
String avd_nr =getIntent.getExtras("getData");
String avd_nr =getIntent.getExtras("getData");
Its should be on onCreate() method
I am sending an implicit intent from one activity to another and I want to send some String variables. I'm doing this as an implicit intent as I wish to use activity B in multiple projects, it's part of a library project.
I have got the intent working just fine but the extra data is not being read in at activity B.
Here's my code in Activity A to create the intent:
Intent intent = new Intent(this, ShareResultsActivity.class);
intent.putExtra(EMAIL_SUBJECT, "Results");//String
intent.putExtra(RESULTS_SAVENAME, saveName);//String
intent.putExtra(RESULTS_BODY, body);//String
intent.putExtra(WITH_SAVE_BUTTON, withSaveButton);//Boolean
startActivity(intent);
And in Activity B:
Bundle extras = getIntent().getExtras();
Boolean withSaveButton = true;
if(extras != null){
emailSubject = extras.getString("EMAIL_SUBJECT");
resultsSaveName = extras.getString("RESULTS_SAVENAME");
resultsBody = extras.getString("RESULTS_BODY");
withSaveButton = extras.getBoolean("WITH_SAVE_BUTTON", true);
}
This doesn't work and the String variables are null.
I have also tried to do this in the way an explicit intent recieves the data:
Intent intent = getIntent();
emailSubject = intent.getStringExtra("EMAIL_SUBJECT");
resultsSaveName = intent.getStringExtra("RESULTS_SAVENAME");
resultsBody = intent.getStringExtra("RESULTS_BODY");
Boolean withSaveButton = intent.getBooleanExtra("WITH_SAVE_BUTTON", true);
But this doesn't work either.
The only way I can get it to work is if I reference the sending Activity in getStringExtra but I can't do this as it's supposed to be a library class.
Can anyone shed any light on this? Or am I going about this in the wrong way?
I assume your code like this:
final static String EMAIL_SUBJECT = "email_subject";
// RESULTS_SAVENAME and others are the same style.
Intent intent = new Intent(this, ShareResultsActivity.class);
intent.putExtra(EMAIL_SUBJECT, "Results");//String
startActivity(intent);
then in your another activity:
Intent intent = getIntent();
emailSubject = intent.getStringExtra("email_subject");
// or this
// emailSubject = intent.getStringExtra(EMAIL_SUBJECT);
Please make use of the key , it should be same in both the activities. I clearly see a difference in naming convention of the key name in both the activities.If key name is same, no way it data should not pass.
Is there a way to get the class name of the activity that launched the Intent without putting extras to the Intent?
I use the following code to launch other activities.
Intent intent = new Intent(CallerClass.this, TargetClass.class);
startActivity(intent);
Can I get the caller class' name in the target class?
One way you could do it is by using startActivityForResult() instead of startActivity(Intent) and then have the second activity use getCallingActivity() to get the CallerClass.
use startActivityForResult then you can retrieve activity caller by : getCallingActivity().getClassName()
String packageName = context.getPackageName();
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
String className = launchIntent.getComponent().getClassName();
I'm having a problem with an android application that I'm working on.
My application has several sections and the next screen that loads is based on a string. So, screen 1 of section 1 would be, S1S1.
My question is, how can I start an activity based on a string. I have S1S1 saved in a string, let us call it next activity. Rather than having to type S1S1.class, I need it to come from the string. I've tried everything I can think of and google hasn't helped much.
Some things I've tried are
Intent myIntent = new Intent(nextactivity);
Intent myIntent = new Intent(v.getContext(), getClass().getName().valueOf(nextactivity));
Intent myIntent = new Intent(v.getContext(), Class.forName(nextactivity));
and tried running with
startActivityForResult(myIntent, 0);
but nothing seems to work. Any ideas?
Here is a code by which you can start activity using the name of the activity
String activityToStart = "com.example.MainActivity";
try {
Class<?> c = Class.forName(activityToStart);
Intent intent = new Intent(this, c);
startActivity(intent);
} catch (ClassNotFoundException ignored) {
}
EDIT
Here class name will be full name of the class with the package name.
For example if your package name will be x.y.z and if you have Activity name called A then the full name of the Activity A will be x.y.z.A.
An even better way (and one that is used in the system to launch Browser.apk along with other apps that aren't bundled with AOSP):
Intent intent = new Intent();
intent.setClassName("com.android.browser","com.android.BrowserActivity");
context.startActivity(intent);
Alternatively, if you want to check that you can start the Activity from the command line, you can do something like this from your shell:
adb shell
am start com.android.browser/.BrowserActivity
I am not aware of solution but i have an alternative.. the way similar to div hide and show in web pages.
if your s1s1 is to loaded low content have them in a linearlayout and keep their visibility gone on loading form s1. when you click on s1 to reach s1s1 hide s1 and set the params of visibility to "visible".
By doing this you can avoid creating a separate activity and this way is also easy to navigate back.
Use Enums!
public enum SectionActivity {
S1S1(MyS1Activity.class),
S1S2(S2Activity.class);
private Class<? extends Activity> activityClass;
private SectionActivity(Class<? extends Activity> clazz) {
this.activityClass = clazz;
}
public Class<? extends Activity> getActivity {
return activityClass;
}
}
Then somewhere in your code:
SectionActivity act = SectionActivity.valueOf(string);
Intent intent = new Intent(this, act.getActivity());
startActivity(intent);