直到昨天为止一切正常,但是今天当我尝试运行该应用程序时,它显示“错误运行应用程序:未找到默认活动”
尝试通过在“编辑配置”选项中指定活动路径来解决此问题,但随后提示
AndroidManifest.xml中未声明活动“ MainActivity”
尝试文件->使缓存无效/重新启动,但无效
彻底检查了清单以查找语法错误,但清单中显然没有此类错误
确保build.gradle文件中的版本相同
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<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:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
修改
MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
尝试卸载并重新安装Android Studio 3.4,但仍然无法正常工作
答案 0 :(得分:1)
听起来像Android Studio的错误。我本人也遇到过几次。删除缓存并重新启动Studio。在此处查找更多参考资料:' https://stackoverflow.com/a/54321295/5182150
答案 1 :(得分:-1)
在build.gradle(module)文件中添加代码
sourceSets {
main.java.srcDirs += 'src/main/<YOUR DIRECTORY>'
}
答案 2 :(得分:-1)