I've got an activity where there is a button which opens an AlertDialog.
My dialog works and I decided to add a layout to it which contains a spinner.
So I have 3 documents :
mainActivity.java : Its role is to open a Dialog
activity_main.xml
dialog_main.xml : The dialog's layout containing the spinner
I try to retrieve in mainActivity.java the spinner declared in dialog_main.xml (in order to add it an adapter) :
Spinner mySpinner = (Spinner)findViewById(R.id.mySpinner);
However mySpinner = NULL, I can't find mySpinner. What is the problem ?
findViewById
as a method of the Activity class finds Views in your Activity's layout. When you are showing a Dialog, it is not part of your layout, so you have to findViewById in the Dialog's layout.
You are probably inflating a View for the Dialog that you show, you cou can use this view to find your Spinnner.
It probably looks like
View view = layoutInflater.inflate (...);
then you do
view.findViewById(...);
Post the code of how how show the Dialog and I'll show you if you can't do it.
Related
I have an activity in my app, where I want the user to pick the date from a DatePicker, which is contained in an AlertDialog. In the AlertDialog, I have set a view, to an xml layout file (, which contains only a LinearLayout, with a single DatePicker.
The code is really simple, and looks like this, just below onCreate().
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(R.layout.activity_alertdialog_date);
DatePicker datePicker = (DatePicker) findViewById(R.id.Activity_AlertDialog_SetStartDate);
// ... The rest of the AlertDialog, with buttons and all that stuff
alert.create().show()
The layout shows up in the AlertDialog, and that part works great.
However, when I try to add this line, I get a null object reference error.
datePicker.setMinDate(System.currentTimeMillis() - 1000);
Here is the error message.
Attempt to invoke virtual method 'void
android.widget.DatePicker.setMinDate(long)' on a null object reference
How can I fix this, or improve my code in another way?
I really appreciate all the help I can get. Thanks!
Your problem is that your findViewById is looking in the wrong place for the DatePicker view. Calling findViewById in an activity will call it on the Activity's layout hierarchy, not the dialog's layout. You need to inflate the layout for the alert dialog first, and then get a reference to the view. This can be acheived in a couple ways.
Probably the easiest is to inflate the view and get the reference before showing the dialog:
View dialogView = LayoutInflater.from(this).inflate(R.layout.activity_alertdialog_date, false);
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.Activity_AlertDialog_SetStartDate);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(dialogView);
// ... The rest of the AlertDialog, with buttons and all that stuff
alert.create().show();
You could also get the view from the alert dialog after it's been created:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(R.id.Activity_AlertDialog_SetStartDate);
// ... The rest of the AlertDialog, with buttons and all that stuff
AlertDialog dialog = alert.create();
dialog.show();
DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.Activity_AlertDialog_SetStartDate);
This is what I'm hoping to achieve:
Step 1: Click on the Green Button to open View1.xml on the ViewPager. (Works)
Step 2: Click on the Orange Button to open SubView.xml on the ViewPager. (Doesn't work)
So, I have set the onClick event for the orange button the following:
public void openForm(View view) {
ViewGroup item = (ViewGroup)findViewById(R.id.vp_horizontal_ntb); //this is the view pager
View child = getLayoutInflater().inflate(R.layout.activity_post_form, null);
item.addView(child);
}
Results:
Nothing. Nothing happens.
If I switch viewpager for linearlayout it opens (and breaks tab navigation). What I wanna do is open it on the viewpager, so it looks and behaves as if it's on a tab, until it's removed by clicking on another tab.
I also thought about putting a linear layout there and make viewPager gone while the linearLayout is set to visible. But I feel like there's a better solution than this.
I have created a dialog box like so using a custom layout:
dialog = new Dialog(FetchMenu.this, R.style.CustomDialogTheme);
dialog.setContentView(R.layout.custom_dialog_iab);
dialog.show();
I am now trying to edit a textbox within 'layout.custom_dialog_iab' for example:
TextView text = (TextView) findViewById(R.id.all_topics_unlock_button);
text.setText("Purchased");
My Question: How do I get the right context to be able to edit the textboxes?
P.S. I have tried 'dialog.getContext()' but still keep throwing null pointers?
You need to use:
TextView text = (TextView) dialog.findViewById(R.id.all_topics_unlock_button);
Note the dialog. in front of findViewById
regular findViewById() will search your activity layout rather than the dialog layout.
In my application I have a listview with products. Above the listview in the actionbar I have a button "edit list". When you click on the button I want to add an image to each listviewitem in the listview. When you click on the image you delete it from the list. Now I added an imageview to the listviewitem and set die visibility to gone.
But I don't know how to change the visibility with a click on a button.
So the question: How do you add an imageview in listviewitem with click on button? Or how do you change the visibility property of an imageview in an Listviewitem?
You can use a static parameter like int uiUpdate=0 and use an adapter to add data to list view and check uiUpdate in getView of your adapter. When user click on button set uiUpdate=1 and reffresh your list with this : yourlist.setListAdapter(new EfficientAdapter);
in getView of EfficientAdapter check value of uiUpdate if it's 1 set visibility VISIBLE : yourimageview.setVisibility(View.VISIBLE); efficientAdapter is something like this : http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
I have an android app that extends a MapActivity, so I can't extend TabActivity.
I am inflating the content of the tabs dynamically, and everything looks good.
The problem that I'm running in to is that I get a NullPointerException when trying to bind event listeners to the views that were inflated into a tab past the first tab.
Example:
myButtonInTab2 = (Button)this.findViewById(R.id.mybuttonintab2);
myButtonInTab2.setOnClickListener(new OnClickListener(){
public void(View v){
///blah blah blah
}
});
If you are "inflating the content of the tabs dynamically", you should be calling findViewById() on the result of the inflate() call, for any widgets in that tab.