我遇到了Android Studio(1.3开发频道)的问题。我从菜单中创建了默认的空白活动(File-> New-> New project,然后API 16作为最小SDK,空白活动,默认设置)。如果我可以从我的包中加载名为MainActivity
的类,我想用这么简单的设置进行测试。为此,我只是将以下内容添加到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`。这是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)
为了完整性,这是我的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>
答案 0 :(得分:1)
使用此ClassLoader.getSystemClassLoader()
this.getClass().getClassLoader()