java - 引用Android库的资源

标签 java android

我通过 android studio 模块为我的应用程序导入了一个 android 库。但是发生了一些错误。我在模块中定义了一个自定义 View ,它使用 getResource() 方法。因为我在应用程序中使用了该 View 。所以context 是我的应用程序上下文而不是我的模块上下文。所以我无法获取模块中定义的资源文件。那么我能做些什么来解决这个问题。

//It is in my custom view in android library
TextView textView = new TextView(context);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(params);
textView.setText(content);
textView.setTextSize(20);
textView.setSingleLine();
textView.setTextColor(getResources().getColor(R.color.title));
textView.setPadding(0, 70, 0, 30);
textView.setGravity(Gravity.CENTER);
this.addView(textView);

当我在应用程序中使用自定义 View 时。当代码到达 textView.setTextColor(getResources().getColor(R.color.title)); 方法时,它将崩溃

02-16 16:10:03.449  25438-25438/fyales.com.lovehistory E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: fyales.com.lovehistory, PID: 25438
    java.lang.RuntimeException: Unable to start activity ComponentInfo{fyales.com.lovehistory/fyales.com.lovehistory.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f08003d
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f08003d
            at android.content.res.Resources.getValue(Resources.java:1233)
            at android.content.res.Resources.getColor(Resources.java:887)
            at fyales.com.parser.widget.MDLayout.drawData(MDLayout.java:75)
            at fyales.com.lovehistory.fragment.DynastyListFragment.onCreateView(DynastyListFragment.java:74)
            at android.app.Fragment.performCreateView(Fragment.java:2053)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
            at android.app.BackStackRecord.run(BackStackRecord.java:833)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
            at android.app.Activity.performStart(Activity.java:5948)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

非常感谢任何帮助。

最佳答案

https://play.google.com/store/apps/details?id=com.desarrollodroide.repos&hl=en

下载上述应用程序。

您肯定会获得相同的源代码

关于java - 引用Android库的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28536517/

相关文章:

Android Camera setDisplayOrientation 不起作用

java - 为什么当我在根文件夹下按掩码搜索文件时会看到 NullPointerException?(windows)

java - 删除项目后 Recyclerview 未更新

java - 如何配置 Google App Engine 上的应用程序以接收任何电子邮件地址的电子邮件?

android - 在android中触摸事件后启动字幕

android - 找不到项目的属性

android - 如何为android应用程序创建配置文件?

java - 页眉和页脚和freemarker

java - 连接到本地计算机中的 MS SQL 数据库

android - 我可以从我的 Cordova config.xml 控制 AndroidManifest.xml 文件的 <supports-screen> 设置吗?