如何在Java Jar文件中包含XML布局文件

时间:2014-04-03 08:17:14

标签: java android xml jar

我想在我的Java Jar文件中包含一个XML布局文件,我想将它用于Android。我想在我的库jar文件中做这样的事情。

public class Library {

    Activity activity;

    public Library(Activity activity){ 
        this.activity = activity;
    }

    public void ABC() {
        Toast.makeText(activity,"ABC started",Toast.LENGTH_LONG).show();
        istream = getClass().getResourceAsStream("/res/layout/activity.xml");
        // xAxis is inside my activity.xml file
        TextView tvX= (TextView)this.activity.findViewById(R.id.xAxis);
    }
}

但是当我在我的下面的android应用程序中导入上面的jar文件并像这样使用时,它显示错误     java.lang.NoClassDefFoundError:com.example.library.Library.ABC中的com.example.library.R $ id“。

我真的没有得到什么问题?

public class CheckLibrary extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_library);
        Library lib = new Library(this);
        lib.ABC();
    }
}

1 个答案:

答案 0 :(得分:2)

无法在Java .jar中包含资源。这就是为什么即使是拥有资源的支持库也不是作为jar而是作为库项目的原因。

只需将您的项目更改为图书馆项目即可。有关它们的更多信息,请参阅文档:http://developer.android.com/tools/projects/index.html