I have two activity as below in same project.
How do i launch the MainActivity from ServicesDemo? I have used the Intent but it does not launch MainActivity.
Mainfest i have only one:
<activity
android:name=".ServicesDemo" android:label="#string/app_name">
When the project launch it start this one:
public class ServicesDemo extends Activity implements OnClickListener {
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonpicture:
Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);
break;
}
}
}
ServiceDemo needs to launch this also:
public class MainActivity extends Activity implements OnClickListener {
}
EDIT:
Main fest: multiple activity listed
<activity
android:name=".ServicesDemo" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" android:label="#string/app_name">
</activity>
Called activity which is needed:
Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);
Add the activity you want to launch into the Manifest:
<activity android:name=".MainActivity" android:label="#string/app_name" />
Then you can launch it with an intent:
startActivity(new Intent(this, MainActivity.class));
startActivity(new Intent("your.package.MainActivity"));
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
finish();
then you must declare your activity in the manifest
<activity
android:name=".MainActivity" android:label="#string/app_name">
Related
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="App Name"
android:roundIcon="#drawable/logo"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:theme="#style/maintheme"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning"
tools:replace="android:icon, android:allowBackup"
>
this activity is splash screen activity below
<activity
android:name=""
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
this activity is Folder activity activity below
<activity
android:name= ""
android:theme= ""
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="true">
</activity>
<activity android:name="" android:theme=""/>
<activity android:name="" android:theme="" android:configChanges="orientation|keyboardHidden|screenSize"/>
</application>
the problem is when the app run it show the splash screen and then it
show the app keep closing,it doesn't start the main activity.
can i make the android manifest to make the splash screen show first and then the start main activity
You cannot use your manifest for this, you need to use an Intent.
Code Example that you need in your SplashScreen Activity:
Intent intent = new Intent(SplashScreen.this, YOURNEXTCLASS.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
You can switch activities only from the java files.
Implement this in your OnCreate method.
SplashActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
startActivity(intent);
finish();
}
}, 2000); ////wait 2s before doing the action
}
AndroidManifest.xml
<activity
android:name=".ui.activities.SplashActivity"
android:exported="true"
android:theme="#style/SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.activities.MainActivity"
android:exported="true"/>
the simple method to do is use this in splashscreenactivity.java
Intent intent=new Intent(SplashScreen.this, YourClass.class);
startActivity(intent);
finish();
like this
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, 3000);
}
}
Make Sure Your compileSdkVersion 31 and targetSdkVersion 30.
I developed an application and put that on app store. Afterwords i wanted to change the package name so i just changed the application ID in build.gradle so that it looks appropriate in link. I didn't change anything else not the package name, not the manifest file etc. Application worked fine but now it's showing an error of ActivityNotFound exception on the launcher activity which is called through a broadcast receiver although that activity is defined in manifest file. May i know where am i wrong at?
This is the manifest file coding:
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
<activity
android:name=".BatteryChargerFast"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:label="#string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And below is the coding of broadcast Receiver:
public class PowerConnectionReceiver extends BroadcastReceiver {
private String TAG="PowerConnectionReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setClassName("packagename",
"packagename.BatteryChargerFast");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("fast", true);
context.startActivity(i);
}
}
the error states:
java.lang.RuntimeException: Unable to start receiver
packagename.PowerConnectionReceiver:
android.content.ActivityNotFoundException: Unable to find explicit
activity class {packagename/packagename.BatteryChargerFast}; have you
declared this activity in your AndroidManifest.xml?
When you change applicationId in gradle, it overrides the Manifest's id.
so you need to change your code from:
Intent i = new Intent();
i.setClassName("packagename",
"packagename.BatteryChargerFast");
To:
Intent i = new Intent();
i.setClassName("your.new.app.id",
"packagename.BatteryChargerFast");
or may be even simpler where you don't need to consider all this:
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context.getApplicationContext(), BatteryChargerFast.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("fast", true);
context.startActivity(i);
}
enter code here
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newuser.applicationtwo">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:roundIcon="#mipmap/ic_launcher_round"
android:screenOrientation="landscape"
android:supportsRtl="true"
>
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
android:label="#string/app_name">
</activity>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</application>
<activity android:name=".SecondActivity" />
<activity android:name=".ThirdActivity" />
<activity android:name=".FourthActivity" />
<activity android:name=".FifthActivity" />
<activity android:name=".SixthActivity" />
</manifest>
-----------------------------------------------------------------------
package com.example.newuser.applicationtwo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import static android.content.ContentValues.TAG;
public class MainActivity extends Activity implements View.OnClickListener
{
private ImageButton playBtn, multiplayerBtn, settingsBtn;
public MainActivity(ImageButton playBtn, ImageButton multiplayerBtn) {
this.playBtn = playBtn;
this.multiplayerBtn = multiplayerBtn;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.playBtn:
Intent i = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(i);
onClick(playBtn);
Log.d(TAG, "Play Button Move");
break;
case R.id.TopicBtn8:
Intent j = new Intent(getApplicationContext(),
ThirdActivity.class);
startActivity(j);
onClick(multiplayerBtn);
Log.d(TAG, "Multiplayer Button Move");
break;
case R.id.settingsBtn:
Intent k = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(k);
onClick(settingsBtn);
Log.d(TAG, "Settings Button Move");
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playBtn = (ImageButton) findViewById(R.id.playBtn);
multiplayerBtn = (ImageButton) findViewById(R.id.multiplayerBtn);
settingsBtn = (ImageButton) findViewById(R.id.settingsBtn);
playBtn.setOnClickListener(MainActivity.this);
multiplayerBtn.setOnClickListener(MainActivity.this);
settingsBtn.setOnClickListener(MainActivity.this);
------------------------------------------------------------------------
package com.example.newuser.applicationtwo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import static android.content.ContentValues.TAG;
class SecondActivity extends AppCompatActivity implements
View.OnClickListener {
private Button topic1Btn, topic2Btn, topic3Btn, topic4Btn, topic5Btn,
topic6Btn, topic7Btn, topic8Btn;
public SecondActivity(Button topic1Btn, Button topic2Btn, Button topic3Btn,
Button topic4Btn,
Button topic5Btn, Button topic6Btn, Button topic7Btn,
Button topic8Btn) {
this.topic1Btn = topic1Btn;
this.topic2Btn = topic2Btn;
this.topic3Btn = topic3Btn;
this.topic4Btn = topic4Btn;
this.topic5Btn = topic5Btn;
this.topic6Btn = topic6Btn;
this.topic7Btn = topic7Btn;
this.topic8Btn = topic8Btn;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.topic1Btn:
Intent i = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(i);
onClick(topic1Btn);
Log.d(TAG, "Topic 1 Button Clicked");
break;
case R.id.topic2Btn:
Intent j = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(j);
onClick(topic2Btn);
Log.d(TAG, "Topic 2 Button Clicked");
break;
case R.id.topic3Btn:
Intent k = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(k);
onClick(topic3Btn);
Log.d(TAG, "Topic 3 Button Move");
break;
case R.id.topic4Btn:
Intent l = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(l);
onClick(topic4Btn);
Log.d(TAG, "Topic 4 Button Move");
break;
case R.id.topic5Btn:
Intent m = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(m);
onClick(topic5Btn);
Log.d(TAG, "Topic 5 Button Clicked");
break;
case R.id.topic6Btn:
Intent n = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(n);
onClick(topic6Btn);
Log.d(TAG, "Topic 6 Button Clicked");
break;
case R.id.topic7Btn:
Intent o = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(o);
onClick(topic7Btn);
Log.d(TAG, "Topic 7 Button Move");
break;
case R.id.topic8Btn:
Intent p = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(p);
onClick(topic8Btn);
Log.d(TAG, "Topic 8 Button Move");
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
topic1Btn = (Button) findViewById(R.id.topic1Btn);
topic2Btn = (Button) findViewById(R.id.topic2Btn);
topic3Btn = (Button) findViewById(R.id.topic3Btn);
topic4Btn = (Button) findViewById(R.id.topic4Btn);
topic5Btn = (Button) findViewById(R.id.topic5Btn);
topic6Btn = (Button) findViewById(R.id.topic6Btn);
topic7Btn = (Button) findViewById(R.id.topic7Btn);
topic8Btn = (Button) findViewById(R.id.topic8Btn);
topic1Btn.setOnClickListener(SecondActivity.this);
topic2Btn.setOnClickListener(SecondActivity.this);
topic3Btn.setOnClickListener(SecondActivity.this);
topic4Btn.setOnClickListener(SecondActivity.this);
topic5Btn.setOnClickListener(SecondActivity.this);
topic6Btn.setOnClickListener(SecondActivity.this);
topic7Btn.setOnClickListener(SecondActivity.this);
topic8Btn.setOnClickListener(SecondActivity.this);
Intent intent = new Intent(getApplicationContext(),
SixthActivity.class);
startActivity(intent);
}
}
----------------------------------------------------
I wonder if anyone can help? I am trying to build an quiz application and I
am having a problem with the main activity in the manifest file. It seems
to be asking for a constructor and when I run the code it is telling me the
following:Error running SecondActivity: The activity 'SecondActivity' is
not declared in AndroidManifest.xml . Not sure how I would change this as
MainActivity or what else to add for the SecondActivity, unfortunately
reading through similar questions on MainActivity I cannot resolve it.
I attach the Manifest file and both the MainActivity and the SecondActivity
Move
</application>*
right before
</manifest>
because now only first activity is in < application > scope
First remove all constructor which is not needed here and you are not adding all activity under application tag in manifest also you defining the launcher filter out of activity tag add it under activity tag, change manifest as below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newuser.applicationtwo">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:roundIcon="#mipmap/ic_launcher_round"
android:screenOrientation="landscape"
android:supportsRtl="true"
>
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
<activity android:name=".ThirdActivity" />
<activity android:name=".FourthActivity" />
<activity android:name=".FifthActivity" />
<activity android:name=".SixthActivity" />
</application>
</manifest>
I also recommend you to go through basic android development tutorial
https://developer.android.com/training/index.html
activity tags belong inside the application tag.
This should work:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newuser.applicationtwo">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:roundIcon="#mipmap/ic_launcher_round"
android:screenOrientation="landscape"
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
<activity android:name=".ThirdActivity" />
<activity android:name=".FourthActivity" />
<activity android:name=".FifthActivity" />
<activity android:name=".SixthActivity" />
</application>
</manifest>
Appart the fakt that all the activities you declare in your androidmanifest file, should be included in the <application> </application> tag, also if you want to start one exact activity first (meaning that when the app launches that exact activity will start first, for example if you want a logo image, create an activity for that) , you can change the category name to LAUNCHER, and if you want it not to be the first activity starting when the application launches, put it to DEFAULT like this:
<category android:name="android.intent.category.DEFAULT" />
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
My Application stops working after the Splash Screen - keep in mind that this is the very first Android App I have ever developed. Help would be greatly appreciated.
SplashScreen.java:
package tomperry.goodlife;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timerThread = new Thread() {
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent();
startActivity(intent);
}
}
};
timerThread.start();
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
AndroidManifist.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tomperry.goodlife">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity android:name=".SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"
></action>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="tomperry.goodlife.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Your Intent is empty. You did not tell where to go. Replace it on your finally block -
Intent intent = new Intent(SplashScreen.this, NextActivity.class); //Change "NextActivity" as your need
startActivity(intent);
finish();
You haven't given reference to main activity.
try this
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
instead of
Intent intent = new Intent();
startActivity(intent);
Remove intent filter from Main Activity in Manifest file
<intent-filter>
<action android:name="tomperry.goodlife.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Just replace your finally block with this:
startActivity(new Intent(Splashscreen.this,MainActivity.class));
You did not add the destination where to go. In finally block your Intent is empty. So change this code line into finally block:
Intent intent = new Intent();
to
Intent intent = new Intent( SplashScreen.this, MainActivity.class);
I want to run an implicitly tried with the following code:
MenuActivity.java
public class MenuActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button btn = (Button) findViewById(R.id.btnVideo);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri intentUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pp);
intent.setDataAndType(intentUri,"audio/mp3");
startActivity(intent);
}
});
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/iconopp14"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.powerpump.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MenuActivity" />
</application>
You need to specify more parameters in AndroidManifest.xml?
Thanks.
I guess you simply have to register your MenuActivity in you AndroidManifest.xml as demonstrated here.
Looking at your code, it looks like you created a MenuActivity class after creating an android project with default settings and MenuActivity is your main activity(Launcher) class.
Just replace the MainActivity with MenuActivity in your AndroidManifest.xml file and delete
<activity android:name=".MenuActivity" />
OR
Replace the class name to MainActivity from MenuActivity in your java code as follows
public class MainActivity extends Activity {
......
OR
use this as your android manifest.xml file
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/iconopp14"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.powerpump.MenuActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>