android 上的 ClassNotFoundException 空白 activity
ClassNotFoundException on android blank activity
我在使用 Android Studio(1.3 开发频道)时遇到问题。我从菜单中创建了默认空白 activity(文件->新建->新建项目,然后 API 16 作为最小 SDK,空白 Activity,默认设置)。我想通过如此简单的设置来测试我是否可以从我的包中加载名为 MainActivity
的 class。为此,我简单地在 OnCreate
方法中添加了以下内容:
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();
}
在运行时,第一次尝试(即 java/lang/String
)成功,而第二次尝试失败,即 com.alecive
yarpdroid`。这是堆栈跟踪:
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.access0(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)
为了完整起见,这是我的 MainActivity.java
(如果您还需要其他文件,请告诉我,但除此文件外,我没有触及任何其他文件):
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);
}
}
这是我的 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>
尝试用这个 this.getClass().getClassLoader()
更改 class 加载程序 ClassLoader.getSystemClassLoader()
我在使用 Android Studio(1.3 开发频道)时遇到问题。我从菜单中创建了默认空白 activity(文件->新建->新建项目,然后 API 16 作为最小 SDK,空白 Activity,默认设置)。我想通过如此简单的设置来测试我是否可以从我的包中加载名为 MainActivity
的 class。为此,我简单地在 OnCreate
方法中添加了以下内容:
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();
}
在运行时,第一次尝试(即 java/lang/String
)成功,而第二次尝试失败,即 com.alecive
yarpdroid`。这是堆栈跟踪:
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.access0(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)
为了完整起见,这是我的 MainActivity.java
(如果您还需要其他文件,请告诉我,但除此文件外,我没有触及任何其他文件):
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);
}
}
这是我的 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>
尝试用这个 this.getClass().getClassLoader()
ClassLoader.getSystemClassLoader()