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.
Related
I have a graph in main activity also I have a recycler view in main activity. Custom adapter is used for recyclerview. I have a check box and swipe layout in list item layout. in swipe layout there is a delete button.
I want to reset the graph of main activity when I check the check box or when I delete any item.
For this I created one method in main activity. And called this method in adapter onCheckedChangeListener and on click of delete.
But I am getting a null pointer exception on mBarChart. i.e . graph. I have instantiated in mBarChart in setUI method and this is called in onCreate of an activity.
resetMethod
public void resetGraph(Context context)
{
mBarChart.invalidate();
}
in adapter :
Context conext;
MainActivity mainActivity;
mainActivity = new MainActivity();
mainActivity.resetGraph(conext);
How to do this? Please help.. Thank you..
In Adapter call your resetMethod this way
((MainActivity)context).resetGraph(context);
Create a interface that implement Activity, Main activity in your case and override method and perform operation.
//Interface
public interface OnRefreshViewListner{
public void refreshView();
}
//Main Activity
MainActivity extends Activity implements OnRefreshViewListner
{
//Other methods
#Override
public void refreshView(){
// write refresh code here
}
}
//Initialize Interface in adapter constructor
public class YourAdapter extends BaseAdapter {
private OnRefreshViewListner mRefreshListner;
public YourAdapter (Context context) {
mRefreshListner = (OnRefreshViewListner)context;
}
//call MainActivity method
mRefreshListner.refreshView();
}
In the adapter, you should not create a new instance of MainActivity and call resetGraph(). You should use the instance of MainActivity, that created the adapter. Send the instance of MainActivity to the adapter, new Adapter(this) and save it in adapter.
You can change a view from the context of an adapter like this :
cast context to activity.
use findviewbyid method to find the view you want.
initiliaze it to a variable.
View v = ((Activity)getContext()).findViewById(WHATEVER_VIEW_COMPONENT_YOU_WANT);
change the variable as you want.
note. Don't forget to use the type of view that you want and cast the findview method to it.
If you want to call a method just cast the context to MainActivity and call it.
I'm getting the android xml id in first Activity
(e.g)
setContentView(R.layout.sample);
button1=(Button)findViewById(R.id.b_one);
button2=(Button)findViewById(R.id.b_two);
Now the problem is how can i use the same button in second Activity without using findViewById().
(i.e) I don't want to access xml in second Activity rather than that i have to access those button ID from the first Activity itself.
I'm trying this to create a common header.
Please help me.
You can't, because your buttons from two different activities will not share the same instance.
What you can do instead, if you want to have some common code for your header is to delegate this code in a static method of a third class. For example :
public class MyHeader {
private Button button1,button2;
public MyHeader(Activity source) {
this.button1 = (Button)source.findViewById(R.id.b_one);
this.button2 = (Button)source.findViewById(R.id.b_two);
// ...
}
public Button getHeaderButtonOne() { return button1; }
// And so on...
}
and in your activities, keep an instance of this class (initialized with :
private MyHeader header;
// ... in onCreate() method
header = new MyHeader(this);
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);
}
i have public class ExperimentAllInOneActivity extends Activity implements OnClickListener on each button click listener i want to go some other class like public class TemperatureStrategy and perform all logic calculation in this class but main thing is that i have class like public class DialogDisplay extends Dialog implements OnItemSelectedListener in this class a custom dialog is created. Now I have question is that on each button onclick listener I want to call(display) this custom dialog class in, spinner class and like that type of element, and all logic performed with class TemperatureStrategy. How to build constor and pass context of all class?
you can do it in 2 ways AFAIK:
1. Add a context argument in your function like this:
public static void display (Context context) {
// Do your action using action...
2. Add a context argument to your class constructure and use this context in all your functions.
If your onClickListeners are declared inside the Activity (I suppose they are), then you can call getApplicationContext() anywhere inside the Activity class, as opposed to use the 'this' keyword that would refer to the onClickListener itself.
You can place the context in the .Tag property that you pass into your button event and pull it back out.
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.