0

这是我的代码。我正在尝试设置线性布局的背景。但是这个 xml (rowback) 不是我的主要活动内容,所以会出错。如何从其内容中没有该 XML 的类中设置一些 XML?

setContentView(R.layout.activity_quick_search_products);
...
rowb = (LinearLayout)findViewById(R.id.rowback);
...
final Drawable d = getResources().getDrawable(R.drawable.denemebut3);
d.setAlpha(100);
rowb.setBackgroundDrawable(d);

日志猫:

03-30 02:52:50.822: D/AndroidRuntime(14677): Shutting down VM
03-30 02:52:50.822: W/dalvikvm(14677): threadid=1: thread exiting with uncaught exception (group=0x40c841f8)
03-30 02:52:50.822: E/AndroidRuntime(14677): FATAL EXCEPTION: main
03-30 02:52:50.822: E/AndroidRuntime(14677): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vitrinn/com.vitrinn.QuickSearchProducts}: java.lang.NullPointerException
03-30 02:52:50.822: E/AndroidRuntime(14677):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at android.os.Looper.loop(Looper.java:137)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at android.app.ActivityThread.main(ActivityThread.java:4507)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at java.lang.reflect.Method.invokeNative(Native Method)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at java.lang.reflect.Method.invoke(Method.java:511)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at dalvik.system.NativeStart.main(Native Method)
03-30 02:52:50.822: E/AndroidRuntime(14677): Caused by: java.lang.NullPointerException
03-30 02:52:50.822: E/AndroidRuntime(14677):    at com.vitrinn.QuickSearchProducts.onCreate(QuickSearchProducts.java:69)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at android.app.Activity.performCreate(Activity.java:4469)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
03-30 02:52:50.822: E/AndroidRuntime(14677):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
03-30 02:52:50.822: E/AndroidRuntime(14677):    ... 11 more
4

1 回答 1

0

如果R.id.rowback不属于您的主要活动 xml 资源,那么您应该使用以下方法手动扩充该资源:

LinearLayout rowb = (LinearLayout) getLayoutInflater().inflate(XML file that contains R.id.rowback, null);

那么你可以安全地做

rowb.setBackgroundDrawable(d);

另外,如果您在rowback中有嵌套元素,那么您可以调用:

rowb.findViewById(R.id.Something) safely.

或者,如果您的类没有从任何 Activity 扩展,您始终可以在其构造函数中接收 Context 对象,然后使用

LayoutInflater.from(context)

获取一个 LayoutInflater 对象,这样您就可以对您需要的任何 XML 资源进行膨胀,而这些资源实际上并不在您的主要内容中。

希望这能澄清...

于 2013-03-30T01:18:30.910 回答