Android Studio无法找到默认活动 [英] Android Studio cannot find default activity

查看:4519
本文介绍了Android Studio无法找到默认活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经挣扎了两天了。无论我做什么,在Windows 10 64上运行的Android Studio 2.1.1一直在抛出

I've struggled with this for two days now.No matter what I do, Android Studio 2.1.1 , running on windows 10 64 keeps throwing

无法识别启动活动:未找到默认活动
启动活动时出错

Could not identify launch activity: Default Activity not found Error while Launching activity .

此外,语法突出显示对于java文件也不起作用,即我的MainActivity.java文件根本没有语法检查。

Also, syntax highlighting for java files isn't working either, i.e. my MainActivity.java file has no syntax checking at all.

我对任何SO答案都没有运气,主要包括以下一个或多个:

I've had no luck with any SO answers which mainly included one or more of the below:

我试过

1-使缓存无效并重新启动android studio

1- Invalidating caches and restarting android studio

2-清洁/重建

3-重启窗户

4-完全删除android studio并从头开始重新安装

4- Completely removing android studio and reinstalling from scratch

5- Gradle resync

5- Gradle resync

6-尝试创建具有不同SDK的新项目只是为了获得至少一个构建 - 没有吗

6- Trying to create new projects with varying SDKs just to get atleast one to build- none does

任何想法为什么会发生这种情况?作为参考,我的文件在

Any ideas why this is happening? For reference , my files are below

MainActivity.java:
package com.example.simpleapp.simpleapp;

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);
    }
}

AndroidManifest.xml (.MainActivity是红色,它也找不到)

AndroidManifest.xml ( the .MainActivity is in red, it can't find that either)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simpleapp.simpleapp">

    <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>

Gradle项目文件

Gradle Project File

 buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.1.0'

            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }

    allprojects {
        repositories {
            jcenter()
        }
    }

    task clean(type: Delete) {
        delete rootProject.buildDir
    }

Gradle模块文件

Gradle Module file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.simpleapp.simpleapp"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
}

谢谢

推荐答案

在经历了许多令人沮丧的时间之后,我发现以某种方式MainActivity.java被添加为可识别的文件类型。不知道怎么做,但是随着 Add-> New Activity 向导添加的所有活动的默认名称是MainActivity,android studio始终将MainActivity.java视为jsp。因此,每次添加新活动时,永远不会找到主要活动!

OK after many frustrating hours, I discovered that somehow "MainActivity.java" got added as a recognized file type. No idea how, but as the default name for all activities added with Add->New Activity wizard is "MainActivity", android studio always treated "MainActivity.java" as jsp. So every time I added a new activity , it could never find the main activity!

我转到文件 - >设置 - >编辑器 - >文件类型,并在识别的文件类型下的右侧窗格中删除了MainActivity.java。

I went to File -> Settings -> Editor -> FileTypes and removed "MainActivity.java" in the right hand pane under "Recognized File Types".

这解决了问题。希望它可以帮助别人。

This fixed the problem. Hope it helps someone else.

这篇关于Android Studio无法找到默认活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆