This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 7 years ago.
I want to start a new activity when i press the button, but when i press it my app crashes!
Where is the Problem?
Here is the code!
public void onClickButtonListener() {
button_play = (Button)findViewById(R.id.play_button);
button_play.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(".SecPage");
startActivity(intent);
}
}
);
}
Your Intent should have two parameters. The current activity and the activity it is going to.
Intent intent = new Intent(this, SecPage.class);
startActivity(intent);
Please consider the following issues,
Make sure you have specified your source and destination classes,
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
Make sure you have added an activity tag in the manifest file,
<activity
android:name=".ActivityA"
android:theme="#style/FullscreenTheme" >
</activity>
If you are using the Activity class in a different package add the full package name,
<activity
android:name="com.silverlining.bionot.ActivityA"
android:theme="#style/FullscreenTheme" >
</activity>
You are probably trying to invoke a custom action with this constructor for Intent:
public Intent (String action)
Make sure you are using the full custom action string, something like com.google.app.myapp.SecPage.
According to android documentation,
public Intent (String action)
Create an intent with a given action. All other fields (data, type,
class) are null. Note that the action must be in a namespace because
Intents are used globally in the system -- for example the system VIEW
action is android.intent.action.VIEW; an application's custom action
would be something like com.google.app.myapp.CUSTOM_ACTION.
Related
This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 4 years ago.
I am very new to Android Studio, and have found myself stuck in this concept. I am attempting to pass Price + Name data to a Cart activity upon a button press (Add to Cart).
After attempting intent methods, it seems that after pressing "Add to Cart", the cart is opened with the data, but the data is not saved in the new activity for more additions.
Right now I have the following:
Button button = (Button) findViewById(R.id.addcart);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//How to pass information here
}
});
I am hoping to pass textView6 and textView7 to the cart activity. If possible, I would be interested in passing the image as well! Any start on this would be appreciated. Thank you!
To pass data trhoug activities you can use the same intent you use to open the new activity. You can set extras like this:
Intent i = new Intent(context, CartActivity.class);
i.putExtra("price", textView6.getText().toString());
i.putExtra("name", textView7.getText().toString());
startActivity(i);
And then in onCreate() of the just created activity you can retrieve this data getting the intent used to open this activity and getting its extras:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent data = getIntent();
String price = data.getStringExtra("price");
String name = data.getStringExtra("name");
}
Hope this help.
When you create your Intent you have to do :
Intent i = new Intent(this, ToClass.class);
i.putExtra("someName", findViewById(R.id.textView6 ).getText().toString());
i.putExtra("someName2", findViewById(R.id.textView7 ).getText().toString());
startActivity(i);
And then in your second Activity, use :
Intent intent = getIntent();
String someName= intent.getStringExtra("someName");
String someName2= intent.getStringExtra("someName2");
You say your using an intent, but what are you doing with the values? Are you saving them somewhere in the CartActivity?
For the image just pass a reference or name of the image. No need to pass the whole PNG... and again use an intent putExtra() call.
I have set up parse push notifications and I had my app crash when I tried to open it, now I found a work around my making a new java class and overriding onPushOpen like this:
public class Receiver extends ParsePushBroadcastReceiver {
#Override
public void onPushOpen(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
But in order to still receive push notifications I still need this depreciated method in my MyApplication.java class PushService.setDefaultPushCallback(this, MainActivity.class);
How could I get rid of this depreciated method I have looked at this question where I got some help but it did not answer this part about the depreciated method. Exception when opening Parse push notification.
I was thinking that maybe this method could be over ridden but Im not sure if it acutely handles recvieving the push or more handles the push after it has been received?
#Override
public void onPushReceive(final Context c, Intent i) {
// Handle the received push
}
Thanks for the help in advance.
You are subclassing ParsePushBroadcastReceiver.
Then in manifest
<receiver
android:name=".Receiver " // your broadcastreceiver
android:exported="false" >
<intent-filter>
// youtr actions
</intent-filter>
</receiver>
In BroadCastReceiver
public class Receiver extends ParseBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
extras = intent.getExtras();
if(intent.hasExtra("com.parse.Data"))
{
try
{
json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
int notificationtype = json.getInt("notificationtype"); // this is send on the sender side
switch(notificationtype)
{
case 1:
// show your custom notification. Refer android notification guide
break;
case 2:
//rest of the code
Note : If either "alert" or "title" are specified in the push, then a Notification is constructed using getNotification. So no alert and title on the sender side.
Read Managing Push Lifecycle #
https://www.parse.com/docs/push_guide#receiving/Android
Reference
https://www.parse.com/questions/how-suppress-push-notification-from-being-displayed
Every time I try to start new activity, an error occurs (Java null pointer exception) and the app fc's: I tried every single way and it just doesn't work! Here's an example for the code : MainAct is the first activity and NewAct is the activity to launch...
1- manifest:
<activity
android:name="com.example.app.NewAct"
android:label="#string/title_activity_newact" >
<intent-filter>
<action android:name="android.intent.action.NewAct" />
</intent-filter>
</activity>
2-MainAct:
Button bt;
bt = (Button) findViewById (R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i= new Intent ("com.example.app.NewAct");
startActivity(i);
}
});
I also tried other forms of intent such as
Intent i= new Intent ("android.intent.action.NewAct");
Intent i= new Intent (this, NewAct.class);
Intent i= new Intent (view.getContext(), StudentInfoActivity.class);
Don't get the context from the "view" get the context from the Activity. Wherever you need a context use getApplicationContext(), getActivity(), or this. But if you are inside of an Anonymous Inner Class, such as a View.OnClickListener() (as you are in the code you posted) you cannot use this because this will be referring to the Anonymous Inner Class that you are in.
A common practice would be storing the Context away in a private member variable, do this inside of onCreate()...
Ex.
....
private Context mContext;
...
#Override
onCreate(Bundle savedInstanceState){
mContext = this;
}
^By doing this you have a context variable to use freely throughout your activity without having to worry.
Try something like this to start your new Activity, after you have your Context variable:
Intent i = new Intent(mContext,
NewAct.class);
startActivity(i);
^You can use this code from within your OnClickListener().
Intent i = new Intent(MainAct.this, NewAct.class);
startActivity(i);
I think the problem is not with your Intent.please check your onCreate() of the second Activity[NewAct.java].
So I want to launch a service from a shortcut. I know that this is not possible to do directly, so I've set up a activity with the sole purpose of starting the service.
The aim of my service is to send an intent to another app and then 5 seconds later send another so I've used a CountDownTimer to do this.
However, when I launch the Activity that starts the service from the shortcut (this is getting confusing) it launches the apps UI. I don't want this, as I want it to be a background service.
What am I doing wrong. I've only just got into development, so it could be something obvious, but I've been battling with this for a few days now.
For some reason when I run it from the service it just launches the app straight away...
When I run it straight from the invisible activity it runs properly for the 1st 5 seconds fine and then loads the app...
I can't figure out why it's loading the app at all.
I've included as much info as I can that would be relevant.
Any help is appreciated!
My service:
public class Pop1_5Service extends IntentService {
public Pop1_5Service() {
super("Pop1_5Service");
}
#Override
protected void onHandleIntent(Intent intent) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
new CountDownTimer(5000, 2500) {
public void onTick(long millisUntilFinished) {
Intent i = new Intent(INTENT_ACTION);
Bundle b = new Bundle();
b.putInt(BUNDLE_VERSION_CODE, 1);
b.putString(BUNDLE_STRING_NAME, "POP1");
b.putString(BUNDLE_STRING_VALUE, "1");
i.putExtra(BUNDLE_NAME, b);
sendBroadcast(i); }
public void onFinish() {
Intent i = new Intent(INTENT_ACTION);
Bundle b = new Bundle();
b.putInt(BUNDLE_VERSION_CODE, 1);
b.putString(BUNDLE_STRING_NAME, "POP1");
b.putString(BUNDLE_STRING_VALUE, "1");
i.putExtra(BUNDLE_NAME, b);
sendBroadcast(i); }
}
}.start();
}
}
Activity that launches service:
public class Pop1_5Activity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, Pop1_5Service.class);
startService(intent);
finish();
}
}
Subsection of Manifest:
<activity
android:name=".Pop1_5Activity"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".Pop1_5Service" />
And the 'Create a Shortcut' Activity:
public class CreateShortcutActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent shortcutintent = new Intent(this, Pop1_5Activity.class);
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutintent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Pop1_5");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
finish();
}
}
From the look of things, it looks like CreateShortcutActivity does nothing.
Your LAUNCHER is Pop1_5Activity, so when the user presses the app icon, this Activity will run, and it launches the Service.
All the code you have showed us are "invisible", the two Activities finish() themselves, and the Service is a Service.
You might want to look at how your BroadcastReceiver handles your broadcast. For instance, does it create another Activity through PendingIntent? Is the Activity created invisible?
Maybe you should try creating a pending Service instead of pending Activity in the BroadcastReceiver.
I get an ActivityNotFoundException when I use this code:
public void addListenerOnButton3(){
button3 = (Button) findViewById(R.id.btnSettings);
button3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Intent intentSettings = new Intent("net.stuffilike.kanaflash.Settings");
showToast("Settings clicked,");
try{
startActivity(intentSettings);
}
catch(Exception e){
showToastL("Exception" + e);
}
return;
}
});
}
Fair enough, except I can't tell how it wants me to tell it where the Activity is. Here is the relevant section of the Manifest:
<activity
android:name="net.stuffilike.kanaflash.Settings"
android:label="#string/settings" >
<intent-filter>
<action android:name="android.intent.action.SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
How can I be sure the compiler finds my Settings.java file?
Oh, my package is named
package net.stuffilike.kanaflash;
try this
#Override
public void onClick(View arg0) {
Intent intentSettings = new Intent(X.this,Settings.class);
showToast("Settings clicked,");
try{
startActivity(intentSettings);
}
catch(Exception e){
showToastL("Exception" + e);
}
return;
}
});
replace X with your current activity name ..
The constructor for new Intent(String action) takes action as paramter.
As per your manifest, the action you are using is android.intent.action.SETTINGS,
1.So your Intent should be as below
Intent intentSettings = new Intent("android.intent.action.SETTINGS");
or
2.You can directly invoke the activity by using the Activity name,
Intent intentSettings = new Intent(this, Settings.class);
or
3.You can also define a custom action like net.stuffilike.intent.action.SETTINGS and then use this to create your Intent like
Intent intentSettings = new Intent("net.stuffilike.intent.action.SETTINGS");
There's 2 ways you can do this.
Use the action String to let the system see what Activitys can resolve the action on the Intent. If there is more than one Activity that can resolve the action, the user will get an option to pick which one they want.
Intent intentSettings = new Intent("android.intent.action.SETTINGS");
Open the Activity using its class directly (can only be done if both Activitys are in the same app).
Intent intentSettings = new Intent(this, Settings.class);