我最近下载了 Android Studio 2.0 并创建了一个新的启动应用程序,并没有自己添加任何代码。运行应用程序后,android studio 成功在模拟器上安装了 APK,但没有启动应用程序,而是出现以下错误:
$ 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
我在谷歌上搜索过,发现之前有人问过,但提供的解决方案也不适用于我。我也没有在 AndroidManifest.xml 文件中添加任何内容。
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>
最佳答案
我今天也遇到了同样的问题 [Android Studio 2.3.3,Android 8.0 在手机上]
这是我发现的。我试图安装的应用程序没有显示在 android 应用程序窗口 [你拥有它们的那个] 中。但是一旦我进入: Settings->Apps&Notifications->AppInfo 我可以看到这个屏幕截图,上面说这个应用程序“没有为这个用户安装”:
此时我进入应用->点击右上角的三个点->为所有用户卸载
现在,当我尝试通过 Android Studio 运行项目时,错误消息消失并且应用已正确安装
希望对你有帮助,祝你有个美好的一天
安东尼诺
关于android - 启动 Activity 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36802588/