Changing First Activity in App - android

I'm trying to change the first Activity of my app and I believe I would just have to change in the Manifest under the Activity tag.
Right now I'm trying to change it to Main2Activity.
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
When I run this I get the error Error:Execution failed for task ':app:processDebugManifest'.
Manifest merger failed with multiple errors, see logs
I'm unsure how to go about fixing this. It works fine if I leave it as MainActivity though.
Class I'm trying to add
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void multiplayerView(View view) {
startActivity(new Intent(this, MainActivity.class));
}
public void campaignView(View view) {
startActivity(new Intent(this, MainActivity.class));
}
}

This works for me. Pay attention to 2 "Activity" tags. I think your manifest should look like this.
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"></activity>
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Activity (as you mentioned. nothing special):
package ir.webarena.test01;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void multiplayerView(View view) {
startActivity(new Intent(this, MainActivity.class));
}
public void campaignView(View view) {
startActivity(new Intent(this, MainActivity.class));
}
}

Related

How to add an activity before MainActivity.java

How do you add an activity (SplashActivity.java in my case) that launches automatically before the MainActivity? I've added in the below code but the activity is still being skipped over. I've already tried going to Edit Configurations -> Launch -> Default Activity. And the Splash Activity is the only activity with an intent filter.
SplashActivity.java
package com.example.simplesplashscreen;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.widget.AppCompatImageView;
public class SplashActivity extends Activity implements InterfaceSplash {
private AppCompatImageView image;
private AnimatedVectorDrawable animation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
image = (AppCompatImageView) findViewById(R.id.splashobject);
launchSplashScreen();
dismissSplashScreen();
}
#Override
public void launchSplashScreen() {
super.onStart();
Drawable d = image.getDrawable();
if (d instanceof AnimatedVectorDrawable) {
Log.d("testanim", "onCreate: instancefound" + d.toString());
animation = (AnimatedVectorDrawable) d;
animation.start();
}
}
#Override
public void dismissSplashScreen() {
// Start Main Activity
startActivity(new Intent(SplashActivity.this,MainActivity.class));
finish();
}
}
AndroidManifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simplesplashscreen">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.SimpleSplashScreen">
<activity
android:name=".SplashActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
</activity>
<activity
android:name=".SecondActivity"
android:exported="true">
</activity>
</application>
</manifest>

The activity must be exported or contain an intent-filter error

When I try to run the code I keep getting this error "The activity must be exported or contain an intent-filter".
I have set the small drop down beside the green run button to "app" and I have also put an intent-filter in my activity in the manifest. But this doesn't help. Hopefully someone can help me.
The code below is of my manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.brian.project">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
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=".LoginSignupActivity.LoginActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
</activity>
<activity android:name=".HomeActivity"></activity>
<activity android:name=".LoginSignupActivity.SignupActivity"></activity>
<!--<activity android:name="WeightActivity"></activity>-->
</application>
</manifest>
My MainActivity code:
package com.example.brian.project;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.brian.project.LoginSignupActivity.LoginActivity;
import com.example.brian.project.LoginSignupActivity.SignupActivity;
public class MainActivity extends AppCompatActivity {
Button Login, Create_Account;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Login=(Button) findViewById(R.id.btn_login);
Button Create_Account=(Button) findViewById(R.id.btn_create_account);
Login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
openLoginActivity();
}
});
Create_Account.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
openSignupActivity();
}
});
}
public void openLoginActivity() {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
public void openSignupActivity() {
Intent intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(intent);
}
}
Try to use android:exported="true" in the <activity> tag:
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Android MediaPlayer sound delayed on start

I was having a play with Android Studio to create a birthday card and have come to a slight problem with the audio file playing before MainActivity is even on screen. The audio file plays while the splash screen is still on the screen.
I have two Java files:
MainActivity.class
package com.example.android.happybirthday;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
MediaPlayer mySoundfile;
#Override
protected void onPause() {
super.onPause();
mySoundfile.release();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySoundfile = MediaPlayer.create(this, R.raw.music);
mySoundfile.setLooping(true);
mySoundfile.setVolume(100, 100);
mySoundfile.seekTo(0);
mySoundfile.start();
}
}
and Splash.class
package com.example.android.happybirthday;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread mythread = new Thread() {
#Override
public void run() {
try {
sleep(3000); // 3 second delay for cold start
Intent startMainApp = new Intent(getApplicationContext(), MainActivity.class); // initiate MainActivity
startActivity(startMainApp); // open MainActivity
finish(); // close this activity
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
mythread.start();
}
}
This is the AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.happybirthday">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Splash"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Could you kindly give some support?
Thanks :)
Thanks #Sergey and #You Kim, I managed to get it done via adding a delay in onStart within the MainActivity.class
you need to run the media player when the activity is started, not on the create.
try running media player on onStart() or onResume().
Called when activity resume is complete
protected void onPostResume()

Android Applications Linking Activity

I have two different activities in my android applications. The first(MainActivity) has ImageButton through which onClick it's getting navigate two second activity(Numbers). This is my code of MainActivity
package com.android.learning_numbers;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.ImageButton;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.button1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, Numbers.class);
startActivity(intent);
}
});
}
}
Now here there is no error in error log but when application is getting launched on click of imagebutton it is getting crashed. & showing error in LogCat. What i do now. Please help..
It would seem that you haven't declared the Numbers Activity in the Android Manifest.
If an Activity is not declared in the Manifest, as far as the application is concerned, the Activity does not exist which causes the application to crash.
<application>
<activity
android:name="com.android.learning_numbers.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.android.learning_numbers.Numbers"/>
</application>
Try this.. in your manifest before </application>
<activity
android:name="com.android.learning_numbers.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Numbers" />
<activity
android:name="com.android.learning_numbers.Numbers"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar"
android:windowSoftInputMode="adjustResize" >

Activities and the manifest in Android

I know there are plenty of questions about this but I just can't work out why my program keeps loading the wrong activity even though I've made the correct one the default in the manifest. Here's some code for you all:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="swin.examples"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="swin.examples.TemperatureConvertor"
android:label="#string/app_name">
</activity>
<activity android:name="swin.examples.FeetToCmConvertor"
android:label="#string/app_name">
</activity>
<activity android:name="swin.examples.Main" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And here's where I think the problem will be if it's not in the manifest:
package swin.examples;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeUI();
}
private void initializeUI()
{
Button btnCm = (Button)findViewById(R.id.btnFeet);
btnCm.setOnClickListener(btnFeetListener);
Button convertButton = (Button)findViewById(R.id.btnTemp);
convertButton.setOnClickListener(btnTempListener);
}
/** Handle convert button click */
private OnClickListener btnTempListener = new OnClickListener()
{
public void onClick(View v)
{
convertButtonClicked();
}
};
private OnClickListener btnFeetListener = new OnClickListener()
{
public void onClick(View v)
{
btnCmClicked();
}
};
private void btnCmClicked()
{
// set the sender and the receiver of the intent
Intent intent = new Intent();
intent.setClass(getApplicationContext(), swin.examples.FeetToCmConvertor.class);
startActivity(intent); // transmit your intent
}
private void convertButtonClicked()
{
// set the sender and the receiver of the intent
Intent intent = new Intent();
intent.setClass(getApplicationContext(), swin.examples.TemperatureConvertor.class);
startActivity(intent); // transmit your intent
}
}
I can't work it out and I hope you can help me! Thank you so much in advance!
EDIT: Code updated, but still doesn't work. If this helps, this is the log messages:
[2013-04-20 22:44:20 - CombinedConvertor] Success!
[2013-04-20 22:44:21 - CombinedConvertor] Starting activity swin.examples.TemperatureConvertor on device emulator-5554
[2013-04-20 22:44:23 - CombinedConvertor] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=swin.examples/.TemperatureConvertor }
Proper IntentFilter for the main Activity is
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Weirdest bug ever. Worked for everyone else, just not me. Remade temperatureconverter activity and it works now. Thank you everyone for your help!

Categories

Resources