在AndroidManifest.xml中,packagename.activity无法分配给“ android.app.Activity”

时间:2019-04-27 17:17:07

标签: java android

最近几天我读了几篇文章,但似乎无法弄清楚我的问题是什么。在测试应用程序时,它运行正常,但在android studio上我的所有活动下都显示红线,表示“该活动不可分配给android.app.Activity”

当我将我的活动扩展到Activity而不是AppCompatActivity时,红线消失了,但是我真的不想弄乱代码

这是我的AndroidManifest.xml

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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


        <meta-data
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true"/>


        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".chap1" />
        <activity android:name=".chap2" />
        <activity android:name=".chap3" />
        <activity android:name=".chap4" />
        <activity android:name=".chap5" />
        <activity android:name=".chap6" />
        <activity android:name=".chap7" />
        <activity android:name=".chap8" />
        <activity android:name=".chap9" />
        <activity android:name=".chap10" />
        <activity android:name=".chap11"/>
    </application>

</manifest>

这是build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion '28.0.3'
    defaultConfig {
        applicationId "package.name"
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 5
        versionName "5"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:customtabs:27.1.1'
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.android.support:support-media-compat:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.android.gms:play-services-ads:17.2.0'
    implementation 'com.github.chrisbanes:PhotoView:1.2.6'
    testImplementation 'junit:junit:4.12'
}

和MainActivity.java

package package.name;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

import package.name.R;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        MobileAds.initialize(this, "ca-app-pub-xxxxx~xxxxx");
        ///// admob banner/////////////////////////////////

        AdView adView = (AdView) findViewById(R.id.adViewBanner);
        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();
        adView.loadAd(adRequest);

        /////////////// BUTTON  /////////////////////////////////////////////////////

        final Button btn1=(Button)findViewById(R.id.button1);
        final Button btn2=(Button)findViewById(R.id.button2);
        final Button btn3=(Button)findViewById(R.id.button3);
        final Button btn4=(Button)findViewById(R.id.button4);
        final Button btn5=(Button)findViewById(R.id.button5);
        final Button btn6=(Button)findViewById(R.id.button6);
        final Button btn7=(Button)findViewById(R.id.button7);
        final Button btn8=(Button)findViewById(R.id.button8);
        final Button btn9=(Button)findViewById(R.id.button9);
        final Button btn10=(Button)findViewById(R.id.button10);
        final Button btn11=(Button)findViewById(R.id.button11);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, chap1.class);
                startActivity(intent);

            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, chap2.class);
                startActivity(intent);

            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, chap3.class);
                startActivity(intent);

            }
        });

        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, chap4.class);
                startActivity(intent);

            }
        });

        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, chap5.class);
                startActivity(intent);

            }
        });

        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, chap6.class);
                startActivity(intent);

            }
        });

        btn7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, chap7.class);
                startActivity(intent);

            }
        });

        btn8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, chap8.class);
                startActivity(intent);

            }
        });

        btn9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, chap9.class);
                startActivity(intent);

            }
        });

        btn10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, chap10.class);
                startActivity(intent);

            }
        });
        btn11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, chap11.class);
                startActivity(intent);

            }
        });

    }
}

2 个答案:

答案 0 :(得分:0)

这是一个Android Studio错误。

转到文件>使缓存无效并重新启动。

清理并重建项目。

答案 1 :(得分:0)

最后,经过大量研究,我可以解决问题

解决方案非常简单:

退出项目 我只是转到项目的文件夹-> idea,然后删除了libraries文件夹中的所有文件 再次打开Android Studio,一切都很好