1

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.

PROBLEM

The problem that I have is the next one. I want to set a .PNG image in my principal activity called "*activity_principal*". 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. 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:

@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...

I hope you can help me. Thank you!

1 Answer 1

1

The problem is that findViewById(R.layout.activity_principal); is going to return null.

You are passing a layout ID (R.layout.activity_principal) instead of a View ID.

It should look something like findViewById(R.id.fondo);.

Sign up to request clarification or add additional context in comments.

8 Comments

Sorry but I can't see the relation between the .java file and the activity interface. What I mean is that that has no sense from my point of view: "fondo" is the variable name for RelativeLayout I've put to call the XML file "activity_principal" in order to set options on it by java code.
Your RelativeLayout in your XML needs to have an android:id="@+id/my_relative_layout" attribute. This generates an ID for that View in R. You can then reference that ID with R.id.my_relative_layout. If you are still having trouble, update your question with your layout XML.
YEEAAh!! jajaj that was the solution! I don't know why, but the background keeps being black, but the worst problem is solved. Thank you Tanis.7x. Now I will be so grateful if you could help me about the black background. You know the code, and the RelativeLayout has the next attribute:android:background="@android:color/white. It's like it only takes one of the attributes, but it has to got a solution... I guess
Is your RelativeLayout set to fill the parent's width and height?
No, it has set to match it. Why could be the problem? Just curious
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.