繁体   English   中英

引用Android库的资源

[英]Quote the resource of Android library

我通过android studio模块为我的应用程序导入了一个android库,但是发生了一些错误。我在模块中定义了一个自定义视图,并使用了getResource()方法。应用程序上下文而不是我的模块上下文。因此,我无法获取模块中定义的Resources文件,那么该怎么办才能解决该问题。

//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);

当我在应用程序中使用自定义视图时,当代码到达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

下载以上应用程序。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM