ClassNotFoundException on android blank activity

221 views Asked by At

I am having a problem with Android Studio (1.3 dev channel). I created the default blank activity from the menu (File->New->New project, and then API 16 as minimum SDK, Blank Activity, default settings). I wanted to test, with such a simple setup, if I can load the class called MainActivity from my package. To this end, I simply added the following to the OnCreate method:

    ClassLoader cl = ClassLoader.getSystemClassLoader();

    try {
        Class stringCla = ClassLoader.getSystemClassLoader().loadClass("java/lang/String");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    try {
        Class civeCLass = ClassLoader.getSystemClassLoader().loadClass("com/alecive/yarpdroid/MainActivity");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

At runtime, the first try (i.e. with java/lang/String) is successful, wheras it fails with the second one, i.e. com.aleciveyarpdroid`. This is the stackTrace:

06-19 09:12:35.646  32405-32405/com.alecive.yarpdroid I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
06-19 09:12:35.646  32405-32405/com.alecive.yarpdroid W/dalvikvm﹕ VFY: unable to resolve virtual method 408: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
06-19 09:12:35.646  32405-32405/com.alecive.yarpdroid I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
06-19 09:12:35.646  32405-32405/com.alecive.yarpdroid W/dalvikvm﹕ VFY: unable to resolve virtual method 430: Landroid/content/res/TypedArray;.getType (I)I
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/dalvikvm﹕ dvmFindClassByName rejecting 'com/alecive/yarpdroid/MainActivity'
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ java.lang.ClassNotFoundException: Didn't find class "com/alecive/yarpdroid/MainActivity" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at com.alecive.yarpdroid.MainActivity.onCreate(MainActivity.java:23)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5451)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at android.app.ActivityThread.access$900(ActivityThread.java:175)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at android.os.Looper.loop(Looper.java:146)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5602)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
06-19 09:12:35.676  32405-32405/com.alecive.yarpdroid W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

For completeness, here is my MainActivity.java (please tell me if you need anything else, but I did not touch any other file apart from this one):

package com.alecive.yarpdroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

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

        ClassLoader cl = ClassLoader.getSystemClassLoader();

        try {
            Class stringCla = ClassLoader.getSystemClassLoader().loadClass("java/lang/String");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            Class civeCLass = ClassLoader.getSystemClassLoader().loadClass("com/alecive/yarpdroid/MainActivity");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

This is my AndroidManifest.xml:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.alecive.yarpdroid.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>
1

There are 1 answers

0
Gabriella Angelova On BEST ANSWER

Тry to change the class loader ClassLoader.getSystemClassLoader() with this one this.getClass().getClassLoader()