正在运行的应用程序抛出android.support.v7.app.AppCompatActivity,但使用“扩展活动”功能

时间:2019-01-31 23:12:56

标签: java android android-activity android-gradle

我被这个错误困住了,我不知道如何解决它。我有以下LoginActivity:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class LoginActivity extends AppCompatActivity {
    private String TAG = "LoginActivity";
    private int REQUEST_SIGNUP = 0;
    private EditText username;
    private EditText password;
    private Button login_btn;
    int attempt_counter = 5;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        LoginButton();
    }

    public void LoginButton() {
        username = (EditText)findViewById(R.id.input_email);
        password = (EditText)findViewById(R.id.input_password);
        login_btn = (Button)findViewById(R.id.btn_login);

        login_btn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(username.getText().toString().equals("user") &&
                                password.getText().toString().equals("pass")  ) {
                            Toast.makeText(LoginActivity.this,"User and Password is correct",
                                    Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(".MainActivity");
                            startActivity(intent);
                        } else {
                            Toast.makeText(LoginActivity.this,"User and Password is not correct",
                                    Toast.LENGTH_SHORT).show();
                        }

                    }
                }
        );
    }
}

如您所见,即时通讯扩展了“ AppCompactActivity”。如果我运行该应用程序,则会抛出该主题中提到的错误。如果我更新代码并将其更改为“扩展活动”,那么它将起作用。为什么会这样?

以下是我的清单文件,我认为它是正确的,否则即使我使用“扩展活动”,应用程序也永远不会运行:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity android:name=".LoginActivity">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
        </activity>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name_main" >
            <intent-filter>
                <action android:name=".MainActivity" />
            </intent-filter>
        </activity>
    </application>

成绩文件:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    //implementation 'com.android.support:multidex:1.0.3'
    //implementation "org.apache.poi:poi:3.17"
    //implementation "org.apache.poi:poi-ooxml:3.71"
    implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '4.0.1'
    //implementation files('libs/poishadow-all.jar')
    //implementation 'com.fasterxml:aalto-xml:1.0.0'
    implementation 'com.android.support:design:28.0.0'
    //implementation 'com.jakewharton:butterknife:8.5.1'
    //annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

和堆栈跟踪:

2019-02-01 00:02:49.522 18723-18723/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.test.proj, PID: 18723
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test.proj/com.test.proj.LoginActivity}: java.lang.ClassNotFoundException: Didn't find class "com.test.proj.LoginActivity" on path: DexPathList[[zip file "/data/app/com.test.proj-PMMv5uc0Z9RUZsO7uJBe_Q==/base.apk"],nativeLibraryDirectories=[/data/app/com.test.proj-PMMv5uc0Z9RUZsO7uJBe_Q==/lib/x86, /system/lib, /vendor/lib]]
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2718)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.test.proj.LoginActivity" on path: DexPathList[[zip file "/data/app/com.test.proj-PMMv5uc0Z9RUZsO7uJBe_Q==/base.apk"],nativeLibraryDirectories=[/data/app/com.test.proj-PMMv5uc0Z9RUZsO7uJBe_Q==/lib/x86, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1173)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
        Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/app/AppCompatActivity;
        at java.lang.VMClassLoader.findLoadedClass(Native Method)
        at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
                ... 12 more
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.app.AppCompatActivity" on path: DexPathList[[zip file "/data/app/com.test.proj-PMMv5uc0Z9RUZsO7uJBe_Q==/base.apk"],nativeLibraryDirectories=[/data/app/com.test.proj-PMMv5uc0Z9RUZsO7uJBe_Q==/lib/x86, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                ... 15 more

如果它还有助于我也包括activity-login.xml,我在其中注释了一些字段,因为我认为它们导致了紧凑错误,尽管并非如此:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="56dp"
        android:paddingLeft="24dp"
        android:paddingRight="24dp">

        <ImageView android:src="@drawable/ic_test_icon"
            android:layout_width="wrap_content"
            android:layout_height="72dp"
            android:layout_marginBottom="24dp"
            android:layout_gravity="center_horizontal" />

        <!-- Email Label -->
        <!--<android.support.design.widget.TextInputLayout-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_marginTop="8dp"-->
            <!--android:layout_marginBottom="8dp"-->
            <!--android:theme="@style/Theme.AppCompat">-->
            <!--<android.support.design.widget.TextInputEditText-->
                <!--android:id="@+id/input_email"-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:inputType="textEmailAddress"-->
                <!--android:hint="Email" />-->
        <!--</android.support.design.widget.TextInputLayout>-->

        <!-- Password Label -->
        <!--<android.support.design.widget.TextInputLayout-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_marginTop="8dp"-->
            <!--android:layout_marginBottom="8dp"-->
            <!--android:theme="@style/Theme.AppCompat">-->
            <!--<android.support.design.widget.TextInputEditText-->
                <!--android:id="@+id/input_password"-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:inputType="textPassword"-->
                <!--android:hint="Password"/>-->
        <!--</android.support.design.widget.TextInputLayout>-->

        <Button
            android:id="@+id/btn_login"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginBottom="24dp"
            android:padding="12dp"
            android:text="Login"/>

        <TextView android:id="@+id/link_signup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="24dp"
            android:text="No account yet? Create one"
            android:gravity="center"
            android:textSize="16dip"/>

    </LinearLayout>
</ScrollView>

编辑:

添加了styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

3 个答案:

答案 0 :(得分:0)

在导入部分,如果可以检查是否导入了Activity和AppCompactActivity。请从导入中删除该活动,然后重新构建并运行。

答案 1 :(得分:0)

检查您的styles.xml

<style name="AppTheme" parent="**Theme.AppCompat**.Light.DarkActionBar">

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

您必须使用Theme.AppCompat

答案 2 :(得分:0)

最后!设法解决了...原来是罪魁祸首在我的gradle文件中。以下导入导致Android Studio无法识别紧凑导入或忽略它们...

的溶液以除去以下依赖

implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '4.0.1'

,而是使用以下内容:

implementation 'org.apache.poi:poi-ooxml:3.9'