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.
Related
I understand professionals use the former to tailor the onClick method to their exact specification, but cant I just use the onClick attribute and specify the same desired actions within a method.
i.e. [In xml file:] onClick="doSomething" & then
[in MainActivity.java:]
public void doSomething(View view){
//define and start intent
//Show a toast, etc
}
Why not just use the latter option? Thanks
Why not just use the latter option?
Go right ahead.
On bigger projects, it is unlikely that the activity will be managing the button directly:
You might be using fragments, in which case the fragment is more likely to manage the button than is the activity
You might be using MVP, MVVM, MVI, etc. GUI architectures, in which case some other "presenter" object is more likely to manage the button than is the activity
You might be using the data binding framework, in which case you might still be using android:onClick in a layout resource, but directing the event to something other than the activity (e.g., a fragment, a presenter)
And so on
Yes you can do so,both are doing the same thing but using the onClickListener is a better choice ,you can set the listener when you want that button to be active thus your Button and Activity are in cooordinaton.
If you use onClick in your xml file ,it will always call that method doesn't matter what is happening in your activity.
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
So I have re-write it.
The problems are:
If a fragment is declared in xml, then you can't call replace on it. Why?
If you want to put a fragment into a FrameLayout(id, frame_layout), then call
add(R.id.frame_layout, fragment) will result in "No View exist Error". There is a way around this by calling add(android.R.id.content, fragment).
The problem is, what if the R.id.frame_layout isn't the base layout for your activity?
Also, in dynamic fragment dispatch(using replace and add), maybe only one container could contain one fragment rather than two?
I have browsed a lot...
Q1. If a fragment is declared in xml, then you can't call replace on it. Why?
Because that's a static fragment. Android system would always stick to it. There is no way to remove or replace it. Any new fragment that's "add" or "replace" on the same id would be placed on top of each other.
Q2 If you want to put a fragment into a FrameLayout(id, frame_layout), then call add(R.id.frame_layout, fragment) will result in "No View exist Error". There is a way around this by calling add(android.R.id.content, fragment). The problem is, what if the R.id.frame_layout isn't the base layout for your activity?
This is not true. Depending on situations. Generally speaking, the id in the function call "add(id)" only means the container of the fragment or the view which is to be replaced by fragment. android.R.id.content represents the buttom layer of the views in the activity.
Also, in dynamic fragment dispatch(using replace and add), maybe only one container could contain one fragment rather than two?
Well, it depends. Just for the sake of clarity(if you want co-workers to understand your code), it's good habbit to make sure that only one container contains one fragment. That's dynamic fragment, not the rule of static fragment.
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.
Currently I have an AsyncTask declared both in my main activity class and in my widget class. The code is the same except for the result handling in onPostExecute (in activity the result retrieved goes to a textview within activity and in the widget it goes to a textview in my widget).
I'd like to move the AsyncTask to an external java file (to avoid code duplication) and somehow call it from other different classes (like my widget and main activity) with different result handling. I guess I can't just pass a method name as a parameter to call it from onPostExecute, but is there something else I can do?
The short answer to your question is NO, you cannot do anything about this. In general, AsyncTasks are Activity-specific, and are defined as inner classes inside different Activitys so that they have access to methods defined in that Activity.
You can go ahead and define the same AsyncTask inside the Activity and the widget.