Вопрос

FrameLayout content = (FrameLayout) findViewById(android.R.id.content); 

This gives me an error

error: cannot find symbol
    FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
                                          ^
symbol:   method findViewById(int)

I have already imported the required R package

Это было полезно?

Решение

It seems like you are trying to access the layout of your current Activity from a different class. Instead of trying to find your FrameLayout in the different class, save the reference to the FrameLayout inside of your Activity, and pass the FrameLayout to your seperate class (the class where you are currently seeing this issue).

E.g.

Activity Class:

...
OtherObject myOtherObject = new OtherObject();
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.my_frame_layout);
myOtherObject.doStuffWithFrameLayout(frameLayout);
...

OtherObject Class:

...
public void doStuffWithFrameLayout(FrameLayout frameLayout) {
    //You can use the FrameLayout here and do stuff with it.

    //You will likely also want to pass in a Context object if you want to
    //create a LayoutInflater or do other Context-dependent stuff
}
...

Другие советы

Try: FrameLayout content = (FrameLayout) findViewById(R.id.content);

In case this does not work remove the import of yourPackage.R and hit the button 'fix imports' not sure the import you did is correct. I always get 2 different options.

You can just call setContentView() again, but keep in mind that this will invalidate all of your View references, so make sure that you initialize them again.

There's almost never a reason to do this, so I would suggest you look into using Fragments, and just swap out the Fragments instead.

To use android.R.id.content, you must import android.R, not yourAppPackage.R .

And to use multiple layouts within one activity, you have to use ViewFlipper or ViewAnimator (or call setContentView multiple times, but it's resource expensive if you have huge layouts).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top