【问题标题】:Beginner Android java.lang.RuntimeException: Unable to instantiate activity初学者 Android java.lang.RuntimeException:无法实例化活动
【发布时间】:2015-10-08 05:03:59
【问题描述】:

我是初学者,所以不明白我如何解决这个问题。我的 XML 代码没问题。但是当运行模拟器然后点击应用程序它说“不幸的是......已经停止”

错误信息:

    java.lang.RuntimeException: Unable to instantiate activity
 ComponentInfo{com.example.imran.justjava/com.example.imran.justjava.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.imran.justjava.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.imran.justjava-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
    at android.app.ActivityThread.access$800(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.imran.justjava.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.imran.justjava-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
    at android.app.ActivityThread.access$800(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
    Suppressed: java.lang.ClassNotFoundException: com.example.imran.justjava.MainActivity

    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

这是我的java代码

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.imran.justjava.R;

/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {

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

/**
 * This method is called when the order button is clicked.
 */
public void submitOrder(View view) {
    display(1);
}

/**
 * This method displays the given quantity value on the screen.
 */
private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(
            R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}
}

这里是 Xml 代码

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="quantity"
        android:textAllCaps="true"
        android:padding="16dp"
        android:textSize="16sp"
        />
    <TextView
        android:id="@+id/quantity_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:padding="16dp"
        android:textSize="16sp"
        android:textColor="@android:color/black"

        />
    <Button
        android:id="@+id/order_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="order"
        android:onClick="submitOrder"


        />

</LinearLayout>

AndroidManifast.xml 代码

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

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

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

</manifest>

【问题讨论】:

  • 错误信息未完成..!!
  • 您使用的是哪个 IDE?
  • Android 工作室 @RajeshJadav
  • 请提供您的清单代码..

标签: android android-intent


【解决方案1】:

我在最新的 AndroidStudio 中遇到了同样的问题。在更高版本的 Android 中,一切都可以正常编译和运行。

但是,一旦我在带有 Android 4.x 的模拟器上运行该应用程序,它就会崩溃,并出现与本文中提到的相同的 ClassNotFoundException。

在尝试了许多在线建议的解决方案后,事实证明问题与将 multiDexEnabled 设置为 true 有关。我可以发誓这之前工作正常。禁用 multidex 解决了这个问题。

按照https://developer.android.com/studio/build/multidex.html#mdex-gradle 的指南应该也可以修复它,尽管我自己还没有尝试过。

【讨论】:

  • 在 build.gradle -> android -> defaultConfig 中设置“multiDexEnabled false”解决了这个问题,谢谢。
  • 如果您在开发 Flutter 应用程序时遇到同样的问题,请尝试此答案。 stackoverflow.com/a/54680454/6303688
【解决方案2】:

你的 logcat 抛出了什么

原因:java.lang.ClassNotFoundException:找不到类 "com.example.imran.justjava.MainActivity"

ClassNotFoundException : ClassNotFoundException 在类加载器在类路径中找不到所需类时发生。所以,基本上你应该检查你的类路径并将类添加到类路径中。

I guess your activity in manifest is not correct.

发布您的 manifest.xml

看这里 https://developer.android.com/intl/es/samples/BasicContactables/AndroidManifest.html

示例

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

【讨论】:

  • 等待@IntelliJ Amiya
【解决方案3】:

错误:

java.lang.RuntimeException:无法实例化活动 组件信息{com.example.imran.justjava/com.example.imran.justjava.MainActivity}: java.lang.ClassNotFoundException: 找不到类

我也遇到过这个问题。

您的MainActivity.java 被排除在编译之外,因此该类不包含在.apk

.idea/compiler.xml 文件的excludeFromCompile 部分删除以下行

<file url="file://$PROJECT_DIR$/src/com/example/imran/justjava/MainActivity.java" />

【讨论】:

    【解决方案4】:

    我认为您的清单文件中有错误。 首先确保您在AndroidManifest.xml 中正确声明了MainActivity

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 2016-11-17
    相关资源
    最近更新 更多