When you use findViewById, what is the object that invokes this method? - java

I am programming for android devices on Android Studio.
To my understanding, when I declare and assign a Button like this in my MainActivity.java:
mTrueButton = (Button) findViewById(R.id.true_button);
The compiler automatically substitutes this:
mTrueButton = (Button) com.testapp.mytestapp.MainActivity.findViewById(R.id.true_button);
But if I actually type in the second statement, I get a warning about non-static methods cannot be referenced from a static context.
What am I not understanding?

Please look at its signature: public View findViewById (int id)
As it's not static, you should call it from an instantiated class and this is the reason of the message you're getting.
To answer your question, the method is part of Activity. In the end, you can call it on an instance of Activity.
In your case you're inside an Activity instance, so you can refer to this instance with the keyword this.

I think retrieve it with View.findViewById() or Activity.findViewById().
View
Activity

In your MainActivity you have not imported R.java file
SO its taking full reference
Import R.java(your project's not android.R) of your package will solve your problem.

The method called is getWindow().findViewById(id);. To call this method in an activity, you first have to set it's view with setContentView(view). Or you can inflate a view yourself, and call the method findViewById() on the view you inflated.

Related

Why can't I call context.setRequestedOrientation(int)?

Using java with android studio, I'm trying to be able to change the app's orientation from places besides the MainActivity. I can pass the context to a (non-static) field of another class, and call it's methods such as getSystemService(), etc, but it won't let me call setRequestedOrientation() from anywhere but the main activity and I don't understand why. Does anyone know an answer?
For example:
from the mainactivity which extends AppCompatActivity i can call this:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
And if I pass the context like so from mainactivity:
U.Orientate hey=new U.Orientate(this);
I cannot call context.setRequestedOrientation(int) from Orientate's constructor, the method is not even there.

Can't call methods from other views in AndroidStudio

So I got two views that are both in the same activity. I'm getting view1 in view2 with uiView = findViewById(R.id.uiSurfaceView); I then cannot, however, call a public method defined in uiView...
I'm fairly new to AndroidStudio and all I found here is about different classes, not views.
Thank you very much!
What is the type of your uiView variable? It does not look like you have cast the results of the findViewById method to a specific view type that you have defined. You will need to cast this to the correct type.

Button.findViewById vs Button = findViewById

Hey guys I am new in programming. Trying out something in Android Studio (Kotlin)
I have looked for this but didn't find an answer. Button
Mostly I use Button.findViewById<Button>(R.id.Button) but sometimes it gives me error and I have to write it like Button = findViewById(R.id.Button) as Button
Can someone tell me where (or what) is the difference?
with kotlin : you don't must use findViewById. you can use direct id ex: btnSave.text="abc"
If code show error, you select [btnSave] and click [Alt + Enter] to import lib.
findViewById search a View that has the Id you give inside the view you call this method with.
So when you do Button.findViewById(R.id.btn) it should never work because Button is a class and not an instance of view.
When you do myButton.findViewById(R.id.btn) it looks inside myButton, that is an instance of view, if there is a view having btn has id. If there is it return it, otherwise it returns null.
When you do findViewById(R.id.btn) You call this method directly from inside a custom view code. Often it's inside an Activity. Then it looks in the layout of this activity if there is a View having btn has ID.If there is it return it, otherwise it returns null.

Converting Activity to Fragment

I am converting an activity to a fragment so that I can use it within the Android Studio Navigation Drawer template.
I have changed all my activities to extend from fragment, and ensured that I am importing v4 support for fragments.
I have resolved most of the issues that Android Studio has raised...but I am really stuck on the final issue:
I have a fragment that used to be the mainActivity (now called liveview_fragment.java), which calls a class called ApplicationController in a file called ApplicationController.java. However, the line in liveview_fragment that calls ApplicationController gives the following error:
I am new to android/java, with most of my learning coming from tutorials and trial/error. Please could someone point out what I have missed in order to resolve this issue?
Thanks
You'll probably need to pass a context to the ApplicationController. Try with getActivity() or getContext() instead of this
The problem is that liveview_fragment is now a Fragment instead of an Activity. In order to get the hosting Activity, you can call getActivity(). This will probably fix the error.
Always use the getActivity() method to get the context of your attached activity, but always remember one thing: Fragments are slightly unstable and getActivity returns null some times, so for that, always check the isAdded() method of fragment before getting context by getActivity() refer Using context in a fragment

Issue with setContentView() in AsyncTask on fragment view

I was trying to build a typical ListView using the default fragment view in Android - so I need to use setContentView() first to after get the appropriate ListView in that layout. However, I keep getting this error:
Cannot make a static reference to the non-static method
setContentView(int) from the type Activity
I understand this error but do not know how to fix it here (I cannot just go and transform it to static). I am sure my layout name is correct (R.layout.menuList). I am executing this in an AsyncTask under the onPostExecute() section (so it is the same thread as the UI). What am I missing here?
My first thought: Although you've not mentioned but since its complaining about static reference, I assume that you are trying YourActivity.setContentView(R.layout.menuList); ?
Instead try using YourActivity.this.setContentView(R.layout.menuList);. You need correct context.

Categories