I recently downloaded Android Studio 2.0 and create a new startup app and did not add anything code by myself. After running the app, android studio installs the APK on emulator successfully but does not launch the app instead it gives the following error:
$ adb shell am start -n "com.example.muhammad.firstapp/com.example.muhammad.firstapp.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Unexpected error while executing: am start -n "com.example.muhammad.firstapp/com.example.muhammad.firstapp.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Error while Launching activity
I searched it on google and found that it was asked before but the provided solution isn't working for me either. I also did not add anything in the AndroidManifest.xml file.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.muhammad.firstapp">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.muhammad.firstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.muhammad.firstapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
To fix this annoying problem I followed the three following steps:
Included the -r flag in the Install Flags. This may be accomplished by clicking on Run -> Edit Configurations. Now Select the General tab and type -r in the Install Flags text area.
Click on File and select Invalidate Caches / Restart.
Uninstall earlier versions of the app. By this I mean to remove it by accessing General -> Apps and uninstall the app. Dragging and dropping the icon of the app to the trash in the home screen will not fix the issue.
There's a simple solution to this problem if you're running the app on an Emulator. This happens because of Storage space constraints. Increasing the storage space of the Emulator should solve this issue.
You can either free up some storage space from the Emulator itself or increase the storage space from the AVD Manager.
I had exactly this same problem today [Android Studio 2.3.3, Android 8.0 on the phone]
This is what I discovered. The app that I was trying to install was not shown in the android Application window [the one where you have them all]. But once I went in: Settings->Apps&Notifications->AppInfo I could see this screenshot where it was said that this app was "not installed for this user":
At that point I entered the app->clicked on the three points in the top right corner->Uninstall for all the users
Now when I tried to run the project through Android Studio the error message disappeared and the app was correctly installed
Hope this helps, have a good day
Antonino
After struggling for hours I came to conclusion that the problem is with the user files stored in AVD. Whenever I choose Wipe Data of an AVD the app works like a charm and does not show the error. But its really painful for developers to Wipe Data everytime before running the app as Wipe Data will only work if your emulator is not running.
In another test phase, I wiped the data of AVD and then started the app, it ran successully; but when I tried to run the app second time it shows me that error again.
Then I opened 'Manage Apps' and delete my app from emulator. But it did not work either.
Workaround
Another Workaround (But that does not work for me)
Final Conclusion
I think the problem is with somewhere when we tried to 'Run' the app on second time. It does not uninstall the previous version of the app so when it found the package name already there it does not install the app then; results in error launching the activity.
downgrading android version is not the curect way to fix the problem..if you like that you will lose new features of testing and developing in android studio.same kinda problem i faced previously when i upgrade to android 2.1.but i resolve my problem through restarting the android studio by selecting option of "setting->invalid caches/restart". Even though your problem is not fixed then try to create another avd with suitable system image. make sure that you have downloaded a suitable system image for your emulator to run in cpu.
i solved my emulator launching problem in android 2.1 by downloading system image x86-64 with api level 23.try to download this system image if you are using 64 bit.
I had this problem on LG-K220. It turned out that I have previously deinstalled this app, but this phone model had a system app which does not deinstall the app at first, but keeps it like in a recycle bin. No wonder Android Studio had troubles installing a new one. I completely removed the app and then it worked.