Error while Launching activity

2020-02-01 03:45发布

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>

20条回答
可以哭但决不认输i
2楼-- · 2020-02-01 04:14

I had the same problem and solved it by following these steps:

  1. Uninstall the app, in my case, from the actual hardware that is connected over the USB.

  2. This is the key step! Uninstall the app from the "Recently uninstalled apps"

  3. Rebuild the app.

  4. Run it.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-02-01 04:14

This happened to me after changing manifest permissions and trying to restart the app using AndroidStudio 3.5.1. The fix was to uninstall the app using adb and restart.

adb uninstall <your app package name>
查看更多
登录 后发表回答