简体   繁体   English

在Java-Android中设置背景图片的问题

[英]Problems with setting a background image in Java-Android

First of all, I want to tell you that I'm new here and I'm from Spain and my english level isn't so good, so please... try to understand me haha. 首先,我想告诉你,我是新来的,我来自西班牙,我的英语水平不是很好,所以请...尝试理解我哈哈。 Also, you must know that I'm a telematic engineering student and programming is just my favourite hobby. 另外,您必须知道我是一名远程信息工程专业的学生,​​而编程只是我最喜欢的爱好。 With this info, I just want you to be comprehensive with my question: I know that it's a rookie question and it's worse when I'm pretending to create an Android game... But I'm learning bit by bit. 有了这些信息,我只想让您全面回答我的问题:我知道这是一个菜鸟问题,当我假装创建Android游戏时更糟……但是我正在一点一点地学习。

PROBLEM 问题

The problem that I have is the next one. 我的问题是下一个。 I want to set a .PNG image in my principal activity called "*activity_principal*". 我想在我的主要活动“ * activity_principal *”中设置一个.PNG图像。 This image has lot of empty space and the background is initially black (due to the theme selected at the beginning I guess). 这张图片有很多空白,背景最初是黑色的(由于我猜想是一开始就选择了主题)。 Well, this image has some details in black and them merges with the background colour. 好吧,这张图片上有一些黑色的细节,它们与背景色融合在一起。

My first solution was trying to set the two backgrounds from the XML corresponding code, but rapidly I realized it was impossible to use it twice in a same layout. 我的第一个解决方案是尝试从XML对应的代码设置两个背景,但是很快我意识到不可能在同一布局中两次使用它。 So I thought that it would be fixed by the next way: I set the " android:background="@android:color/white" " in the XML file and in the .java file I set the other resource to the background: 因此,我认为可以通过以下方式解决此问题:我在XML文件中设置了“ android:background =“ @ android:color / white” “,在.java文件中,我将其他资源设置为了背景:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_principal);

    RelativeLayout fondo = (RelativeLayout) findViewById(R.layout.activity_principal);
    fondo.setBackgroundResource(R.drawable.prototipoestructurapantalla);
}

I used to use this structure and never had problems but today, trying to do this, I noticed something strange. 我曾经使用过这种结构,从来没有遇到过问题,但是今天,在尝试这样做时,我发现了一些奇怪的地方。 Due not to find the error, I put two breakpoints at the lines under "setContentView(..." and debugging, when the cursor reached the last code line "fondo.setBack..." the variable "fondo" was "null" and I think that there is the problem, so when I resume the debugging the app crashes... 由于找不到错误,我在“ setContentView(...)”下的行中放置了两个断点并进行调试,当光标到达最后一个代码行“ fondo.setBack ...”时,变量“ fondo”为“ null”而且我认为这是问题所在,所以当我继续调试时,应用崩溃了……

I hope you can help me. 我希望你能帮助我。 Thank you! 谢谢!

The problem is that findViewById(R.layout.activity_principal); 问题是findViewById(R.layout.activity_principal); is going to return null. 将返回null。

You are passing a layout ID ( R.layout.activity_principal ) instead of a View ID. 您正在传递布局 ID( R.layout.activity_principal ),而不是视图ID。

It should look something like findViewById(R.id.fondo); 它看起来应该像findViewById(R.id.fondo); .

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

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