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].
Related
I am trying this intent so I can navigate from one activity to another activity but I'm getting this error `
final Button btnAdd = findViewById(R.id.addEmp);
btnAdd.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(v.getId() == R.id.addEmp){
Intent intent = new
Intent(getCallingActivity(),AddEmployee.class);
startActivity(intent);
}
}
});`
there is a red line under
(getCallingActivity(),AddEmployee.class);
there error says
cannot resolve constructor
Is anything wrong with this
(getCallingActivity(),AddEmployee.class);
statement?
You need to change your line from
Intent intent = new Intent(getCallingActivity(),AddEmployee.class);
to this:
Intent intent = new Intent(YourActivityName.this,AddEmployee.class);
OR
Intent intent = new Intent(getApplicationContext(),AddEmployee.class);
Edit
Whats wrong with getCallingActivty()?
getCallingActivity() returns ComponentName while intent constructor requires Context as a first argument.
Hope this will work
I have a "Play Now" button for a simple android game. When I click the button it calls start, but it doesn't do anything.
Here is start():
public void start(View view) {
Intent myIntent = new Intent(this, Game.class);
startActivity(myIntent);
}
and Game.java:
public class Game extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
}
Also, I didn't forget to put it into the manifest
<activity android:name=".Game"></activity>
I'm new to android and this is all very confusing. I tried putting an intent filter although I probably did it wrong.
I looked at this How to switch between screens? but it didn't work for me.
You are finishing the activity just when you create it (onCreate). Try deleting or commenting finish(); and good luck!
remove following lines, we use them with startActivityForResult , after removing it should work other than this everything is fine
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
Actually,your start function is working fine.But the problem is with onCreate() method in Game activity.You are calling finish() method in this which is killing the activity.Get rid of this method and then check.One thing more,I don't understand what is the purpose of setResult in your context.It is actually used for startActivityForResult() method.Refer to this link for further information:
https://developer.android.com/training/basics/intents/result.html
public class Game extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
//Intent intent = new Intent();
//setResult(RESULT_OK, intent);
//finish();
}
}
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.
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);