本质上:一个库项目声明一个Activity,一个实现项目使用(子类)该Activity,而一个测试项目则执行实现的版本。在本场景中,我们发现“无法解析类型。它是从所需的.class文件中间接引用的”。如果更改为ADT r13,则不会报告任何问题。
发布复制的代码:
在图书馆:

package com.r14.error.lib;
import android.app.Activity;
import android.os.Bundle;

public class R14ErrorLibActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


在实施中:

package com.r14.error.imp;
import com.r14.error.lib.R14ErrorLibActivity;

public class R14ErrorImpActivity extends R14ErrorLibActivity {
}


在测试中:

package com.r14.error.imp.test;
import com.r14.error.imp.R14ErrorImpActivity;
import android.test.ActivityUnitTestCase;

public class R14ErrorLibImpActivityTest extends ActivityUnitTestCase<R14ErrorImpActivity> {
    public R14ErrorLibImpActivityTest() {
        super(R14ErrorImpActivity.class);
    }
    private static final int ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING = 42;
    public void testTest() {
        assertEquals(ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING, 42);
    }
}


错误:

The type com.r14.error.lib.R14ErrorLibActivity cannot be resolved. It is indirectly referenced from required .class files


如果lib是手动添加的1(在较旧的adt版本中无需执行此操作),则不会显示生成错误,但测试失败,并显示以下内容:

java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:239)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Caused by: java.lang.reflect.InvocationTargetException
at com.r14.error.imp.test.R14ErrorLibImpActivityTest.<init>(R14ErrorLibImpActivityTest.java:10)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:263)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:185)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:336)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3982)
at android.app.ActivityThread.access$2900(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1901)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.r14.error.imp.R14ErrorImpActivity
... 19 more


我正在使用Eclipse Indigo和ADT 15。

谢谢你的帮助!

最佳答案

这已经是Android Open Handset Alliance Project上的问题(21343)。如果您有同样的问题,请加注星标。
注意:Android SDK工具的修订版17和Eclipse ADT插件的问题seems fixed(我自己的项目编译时没有出现骇客或错误)。

关于android - ADT中的Android库:无法解析类型错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7962184/

10-12 23:29