为什么这段代码似乎找不到MainActivity?

时间:2016-06-21 22:59:13

标签: java android classnotfoundexception oncreate main-activity

我们正在编写一个基于android的应用程序,它跟踪几个手机传感器,现在我试图在突然发生这种情况时编译代码

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.max.pswi, PID: 14291
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.max.pswi/com.example.max.pswi.MainActivity}:
java.lang.ClassNotFoundException: Didn't find class "com.example.max.pswi.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.max.pswi-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2555)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
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:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.max.pswi.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.max.pswi-1/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:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2545)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) 
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5951) 
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:1388) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183) 
Suppressed: java.lang.ClassNotFoundException: com.example.max.pswi.MainActivity
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 13 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

我不知道为什么会出现这种错误

我在Android Studio上编程

以下是创建错误的MainActivity-Code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_main);

    int currentVersionCode = 0;

    try {
        currentVersionCode = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode;
    } catch (PackageManager.NameNotFoundException e1) {
        e1.printStackTrace();
    }
    final ImageButton export = (ImageButton) findViewById(R.id.export);
    final ImageButton clear = (ImageButton) findViewById(R.id.clear);
    final ToggleButton toggle = (ToggleButton) findViewById(R.id.anaus);
    final ImageButton settingsBtn = (ImageButton) findViewById(R.id.settings);
    final ImageButton aktualisieren = (ImageButton) findViewById(R.id.aktualisieren);
    final ListView liste = (ListView)findViewById(R.id.infos);
    if (CheckIfServiceIsRunning())
        toggle.setChecked(true);
    Boolean status = CheckIfServiceIsRunning();
    aktualisieren.setVisibility(View.VISIBLE);
    export.setVisibility(View.VISIBLE);
    toggle.setVisibility(View.VISIBLE);
    settingsBtn.setVisibility(View.VISIBLE);
   /* clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SensorManager.getInstance().clearValues(SensorManager.getInstance().getContext());
        }
    });
    export.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SensorManager.getInstance().exportValues(SensorManager.getInstance().getContext());
        }
    });
    settingsBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            startActivity(intent);
        }
    });
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        Boolean status = CheckIfServiceIsRunning();
        ComponentName cn = new ComponentName(getApplicationContext(), NLService.class);
        String flat = Settings.Secure.getString(getApplicationContext().getContentResolver(), "enabled_notification_listeners");
        final boolean enabled = (flat != null && flat.contains(cn.flattenToString()));

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // The toggle is enabled
                if (!enabled) {
                    final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                    alertDialog.setCancelable(false);
                    alertDialog.setTitle("Notificationlistener aktivieren");
                    alertDialog.setMessage("Bitte aktivieren sie den Notificationlistener für die App PSWI");
                    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Aktivieren", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            alertDialog.cancel();
                            Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
                            startActivity(intent);
                            Intent i = new Intent(getApplicationContext(), MainService.class);
                            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            getApplicationContext().startService(i);
                            status = true;
                        }
                    });
                    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "zurueck", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            alertDialog.cancel();
                        }
                    });

                    alertDialog.show();
                } else {
                    Intent i = new Intent(getApplicationContext(), MainService.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getApplicationContext().startService(i);
                    status = true;
                }
            } else {
                // The toggle is disabled
                Intent i = new Intent(getApplicationContext(), MainService.class);
                getApplicationContext().stopService(i);
                status = false;
            }
        }
    });
   // SensorManager.getInstance().populateListView(liste);
    settings = PreferenceManager.getDefaultSharedPreferences(this);
   if (settings.getBoolean("first_start", true)) {
        // set initial version code, set first_start true and create ID
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("first_start", false);
        editor.putBoolean("SMSSensor:ObserverStarted", false);
        editor.putString("CallSensor:lastIntent", null);
        editor.putBoolean("serviceStarted", false);
        editor.putString("version", "" + currentVersionCode);
        editor.putString("ID", ""
                + ((TelephonyManager) getApplicationContext()
                .getSystemService(TELEPHONY_SERVICE))
                .getDeviceId().hashCode());
        editor.apply();
    }
   // getLoaderManager().initLoader(0, null, (android.app.LoaderManager.LoaderCallbacks<Cursor>) this);
   */
}

如果有人能看一眼我会变得绝望,我会很高兴的。

2 个答案:

答案 0 :(得分:0)

您是否在AndroidManifest文件中声明了此MainActivity?

答案 1 :(得分:0)

检查AndroidManifest.xml

您应该在application标记中看到您的活动,如下所示:

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