Accessing the setters and getters methods - android

I have an A activity that uses a setContentView(R.layout.activityA) method to set a layout. An activityA layout consists of a customView. My customView has a bunch of setters and getters. How can I access them from A activity? When I create an instance of customView in an acitivity A then it works but the customView is created twice: once from the setContentView and the second time when I create a new instance of it. Is there another way of accessing those method? Please advise. Thanks.

Have you tried something like this in your Activity's code:
#Override
public void onCreate(Bundle state){
super.onCreate(state);
setContentView(R.layout.activityA);
CustomView customView = (CustomView)findViewById(R.id.customviewId);
Something x = customView.someGetterMethodX();
...etc...
}

You can use Java Reflection to read attributes and call methods.

You don't have to create it twice, simply find your custom view id and assign it to a CustomView reference. Something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.theLayout);
CustomView customView = (CustomView) findViewById(R.id.customViewId);
customView.setSomething(someValue);
}

Related

How do I go about accessing a view when the activity is created? [duplicate]

Quick notice: I am using SharedPreferences so that I can reload data when I re-open the app.
Problem
I have a LinearLayout in the main fragment of my application. Everything runs smoothly until I re-open the app and try to reinitialize the LinearLayout.
I am trying to initialize the LinearLayout with findViewById(). I have put the function in many different places. Currently I am trying to get it to work in onCreate and a function that is called from onCreate. Here is my code so far:
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.linearLayout);
// this is where is load the SharedPreferences
// this is where I implement them back into the app ('reload' the app)
reload();
}
public void reload() {
// bunch of other irrelevant stuff
linearLayout = findViewById(R.id.linearLayout);
linearLayout.addView(/*other view*/); // this is where it complains
}
// the is for when the button is clicked
public void submitEntry(View view) {
// this is fine according to Logcat
linearLayout = findViewById(R.id.linearLayout);
}
}
I would expect after initializing it twice, or at least trying to, that is would've caught on but no. Logcat complains that linearLayout is a null object reference. I don't know what to do at this point but it's probably something simple that I've overlooked. Any help would be appreciated.
LinearLayout linearLayout= new LinearLayout(context);
Initialize like this it will help you!

How to initialize LinearLayout variable?

Quick notice: I am using SharedPreferences so that I can reload data when I re-open the app.
Problem
I have a LinearLayout in the main fragment of my application. Everything runs smoothly until I re-open the app and try to reinitialize the LinearLayout.
I am trying to initialize the LinearLayout with findViewById(). I have put the function in many different places. Currently I am trying to get it to work in onCreate and a function that is called from onCreate. Here is my code so far:
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.linearLayout);
// this is where is load the SharedPreferences
// this is where I implement them back into the app ('reload' the app)
reload();
}
public void reload() {
// bunch of other irrelevant stuff
linearLayout = findViewById(R.id.linearLayout);
linearLayout.addView(/*other view*/); // this is where it complains
}
// the is for when the button is clicked
public void submitEntry(View view) {
// this is fine according to Logcat
linearLayout = findViewById(R.id.linearLayout);
}
}
I would expect after initializing it twice, or at least trying to, that is would've caught on but no. Logcat complains that linearLayout is a null object reference. I don't know what to do at this point but it's probably something simple that I've overlooked. Any help would be appreciated.
LinearLayout linearLayout= new LinearLayout(context);
Initialize like this it will help you!

How should we set widgets values in Android?

I was looking at my code and I realized that there are at least 3 ways of getting widget's reference in code:
First one (before onCreate):
private TextView textView= (TextView) findViewById(R.id.textView);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
Second one (in onCreate):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
final TextView textView= (TextView) findViewById(R.id.textView);
}
Third one (creating out and setting in onCreate):
private TextView textView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
textView= (TextView) findViewById(R.id.textView);
}
What is the difference between this 3 methods? When should I use them?
You must call setContentView() prior calling findViewById(), therefore on 1st approach will give you null all the times. 2nd and 3rd are the same with the exception for final keyword, but this is Java feature, not Android's.
The first does not guarantee that your widget is actually instantiated, it is not within the onCreate.
Second will be instantiated, but its value can not be changed because it becomes a constant to be the final.
Third, it is a global variable that will be instantiated in onCreate and you can use it in any other part of your code.
If you need to call findViewById() then the call should be anywhere after setContentView. Not before that like in your first option. Your third option creates an instance variable, use it only if the textview will be accessed a lot throughout the class, otherwise just call findViewById wherever you need it.

Android findViewById throws NullPointerException

I have a ViewFlipper defined in a separate class. It looks like this:
public class Flipper extends Activity {
public ViewFlipper view_flipper;
/* Constructor */
public Flipper(int flipper_id) {
view_flipper = (ViewFlipper) findViewById(flipper_id);
}
...
Then in another Activity's onCreate() method I am instantiating the Flipper like this:
private Flipper flipper;
flipper = new Flipper(R.id.login_screen_flipper);
However, I get a NullPointerException in the constructor of Flipper. When I debug, I see that flipper_id has a valid id. What am I doing wrong?
You can't create Activities like this.
To create an activity you must do it using Intents. And you have to define the layout in an XML file and attach it using setContentView(R.layout.layout_name);
Have a look at this to start learning: https://developer.android.com/training/index.html
UPDATE:
OK, it's unclear of what you are accomplishing with the info of your question.
If you are extending the behaviour of ViewFlipper I suggest you extend it:
package your.package;
public class MyFlipper extends ViewFlipper {
// ...
}
Then use it in your xml like this:
<your.package.ViewFlipper
[...] parameters [...] />
And finally when you implement it you can do it like this:
public class YourActivity extends Activity {
public ViewFlipper viewFlipper;
#Override
protected void onCreate (Bundle savedInstanceState) {
viewFlipper = (ViewFlipper) findViewById(R.id.login_screen_flipper);
}
// ... more activity stuff
}
PS: It's good custom to use the lowerCamelCase notation in Java member variables (or fields in Java)
When you instantiate Flipper in another activity, findViewById is called in the constructor of Flipper. It is a method from Flipper class as it extends Activity and surely its call is not valid in the constructor because you do not set content view as it is normally done in Activity.onCreate. Flipper should not extend Activity and you should pass ViewFlipper in a constructor not id.

onClick attribute in XML linking to method in Activity class

There are quite a few questions about this subject, but could not find any with the specific problem I have...
In my layout.xml, I use the android:onClick tag for a Button to call the right onClickListener. I get the error :
java.lang.IllegalStateException: Could not find a method handle_cancel(View) in the activity class com.matthieu.HelloWorldApplication for onClick handler on view class android.widget.Button with id 'button_cancel'
I have that method implemented in the Activity, but it is looking for it in the class that extends Application... I don't understand why. The View and all that is setup only in the Activity.
If anyone needs, here is the declaration of that method (in my activity, NOT in HelloWorldApplication):
public void handle_cancel(View v) {
// do something useful here
}
Edit (from adamp request)... and probably answering my own question :
Here is part of the code where that layout is used...
public class AddVocabularyActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.top); // that layout contains an empty LinearLayout id/main_content
}
private some_other_function() {
LinearLayout main_content = (LinearLayout) findViewById(R.id.main_content);
main_content.removeAllViews();
View.inflate(getApplicationContext(), R.layout.hello, main_content); // layout.hello is the one containing the button
}
// some other stuff
}
While copy/paste this code, I am guessing the problem is that I used getApplicationContext to inflate the View with that Button...
As mentioned in my edit, changing the getApplicationContext() with the Activity context fixes it...
The convention works like this:
In the layout xml file, you give this attribute:
android:onClick:"methodname"
Then, inside a class, you define a method like this:
public void methodname(View v){
//your method code
}
Any other way of doing this is not documented. If you need parameters, just call another method inside that method.

Categories

Resources