I am creating a DialogFragment that I use for my login dialog. I want to have autocompletion for the email addresses that are used as user ids. I found a good tutorial here, but it didn't really deal with using a DialogFragment.
My current code is:
DialogFragment dialog = new LoginDialogFragment();
AutoCompleteTextView emailLoginId = (AutoCompleteTextView) findViewById(R.id.login_user_id);
if (null == emailLoginId) {
Log.d(tag, "EmailLoginId is null!");
}
// get list of drivers
ArrayAdapter<String> driverList = dbHelper.getAllDriverEmailStringAdapter();
Log.d(tag, "Driver List size is " + driverList.getCount());
emailLoginId.setAdapter(driverList);
dialog.show(getSupportFragmentManager(), "LoginDialogFragment");
As you can see, I'm creating the dialog, then attaching the list of names. Unfortunately, this gives me a null pointer exception on the line:
emailLoginId.setAdapter(driverList);
I'm sure this will probably be some embarrassingly simple oversight on my part, but that's okay, if it gets me moving forward.
I've tried this same code, with appropriate modifications, in the onStart() and onCreateView() portions of the LoginDialogFragment. In all cases, it can't find the fields, either the login id field, or the password field.
When calling from within the onStart() and onCreateView() routines, I used:
AutoCompleteTextView emailLoginId = (AutoCompleteTextView) getActivity().findViewById(R.id.login);
Is that wrong?
The only other thing I can figure that might be causing the problem is that the dialog isn't actually created until dialog.show(getSupportFragmentManager(), "LoginDialogFragment") is called, but that doesn't jibe with the documentation and you'd still think this would work in the onStart() routine at the very least.
Thanks, in advance, for your time.
Related
So I'm working on my first real attempt at an Android app, just a simple scorekeeper for softball games. I've got it tallying scores, outs, etc, and right now it just displays "Home" and "Away." I'd like to give users the chance to enter in actual team names. To this effect, I added a custom AlertDialog that pops up with an EditText, so when you hit "OK" it'll update the Home/Away team name.
The problem is I've been Googling this for most of the week and I've not found a single way to actually do this. I've tried tagging the fragment's layout XML so I can find the EditText, but it always gives me a null reference and crashes the app. I added a TextWatcher that presumably watched the fragment's text, but once changed and hit "OK," nothing happened. Tried adding the TextWatch to the fragment, that crashed I think, it was about two hours ago and I'm exhausted.
Really, I think I need a way to have the fragment find the TextView with the team name and change it to the value of the EditText when I positive click, but I don't know how to do that. Or maybe I've seen it and don't understand it, I've only been doing this about two months. I'd post my code, but I deleted out all the stuff that didn't work because it was taking up most of the screen real estate. Any ideas?
EDIT: So I followed advice below on defining views, that found the value of the EditText presumably, but hitting "OK" just made it set the TextView to a blank value. I think this is because the EditText's contents went away as the dialog was closed. Either that or this is all wrong. View dialogname = getActivity().getLayoutInflater().inflate(R.layout.fragment_home, null);
EditText mEtName = (EditText) dialogname.findViewById(R.id.homeName);
View mainAct = getActivity().getLayoutInflater().inflate(R.layout.activity_softball, null);
TextView oTextView = (TextView) mainAct.findViewById(R.id.teamOne);
newName = mEtName.getText().toString();
oTextView.setText(newName)
I think you are doing wrong at time of defining id for EditText. You need to give it dialog reference.
rather than doing
Edittext edittext = (EditText) findViewById(R.id.editText);
do like
Edittext edittext = (EditText) dialog.findViewById(R.id.editText);
Been through this issue a while ago. I created my own class which extended DialogFragment. When I tried to initialize EditText which was in the dialog, I got null like
mEtName = (EditText)dialog.findViewById(R.id.editText);
So, I initialized it in this way and it worked:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
View dialogName = getActivity().getLayoutInflater().inflate(R.layout.dialog_name, null);
mEtName = (EditText) dialogName.findViewById(R.id.dialog_etxt_name);
}
Where R.layout.dialog_name is my custom dialog layout.
Our app has several fragments. In one of them user fills several TextEdit fields. When he finishes he presses a button in the ActionBar to save the data. The Action just calls a private method named "saveData" that fetches all data from the fields and submit it to our server.
We have many stack traces from our users showing that getView() returns null in method saveData, but for just a small part of them. For most of them there is no problem at all. We cant reproduce the problem and we cant understand what might be causing it. The code is pretty simple:
View vw = this.getView();
EditText et;
et = (EditText)vw.findViewById(R.id.editEmail);
String email = et.getText().toString().trim();
et = (EditText)vw.findViewById(R.id.editPassword);
String password = et.getText().toString().trim();
The action is added in osResume, see below:
public void onResume() {
super.onResume();
MainActivity act = (MainActivity)this.getActivity();
act.bar.removeAllActions();
act.bar.addAction(new SaveAction());
}
Any ideas? How can we reproduce it?
Can you tell from your logs whether the problem is always for the same users / devices ?
I see from the code that you have submitted that the view is in the same fragment - is that actually the case ?
It's POSSIBLE that a fragment no longer in view can have their view destroyed in order to free up resources. e.g.
getView() returns null
If I suspected that this might be the case then I would attempt to recreate the problem on a phone / tablet / emulator with limited resources.
Good luck !
In my app I have a header with icon hidden, I have a adapter with a listview when I click the listview I go to a login screen using listener, when the login is successful is should come back to listview(adapter) and icon should get visible on header.
In the login activity I have the following code:
public void onClick(View v) {
String password = etPassword.getText().toString();
if(password.equals("guest")){
SearchAdapter.setImgVisibility();
} else {
//-----
}
finish();
}
In my adapter I am calling the setImgVisibility() as follows, but it is not working
public static void setImgVisibility() {
img.setVisibility(View.VISIBLE);
}
I am getting a Nullpointerexception near the line img.setVisibility(View.VISIBLE);
I am stuck here and don't know what I am doing wrong. Any suggestions or help is appreciated
I would imagine that img is null. You need to look at where this value is set and make sure happens before you call the method setImgVisibility.
Show more of your complete code for people to help further.
Additionally, i've just noticed you've used a static reference to your search adapter, you should be really careful using statics, especially where any referencing of images is concerned as images can be bound to the context, as such unless you nullify the static you will end up with a memory leak. (this used to be an old problem, not sure its still valid, but i would still avoid using a static reference).
Without more code we're not likely to be able to properly help you. For example are you switching activities when logging in? If you are, this won't work at all.
[given the comment below] If you switch activities then your activity containing the list view is going to be destroyed and then rebuilt then you navigate back to it. or it will at least go through the activity lifecycle. This means you can set the icon during the instantiation of the header img.
You could store your logged in state as a property of the Application or a preference. Grab this value when you set the header image and set the image accordingly.
your img object is null. Is your img object is same as View v then you can pass v in setImgVisibility() and then set v.setVisibility(View.VISIBLE)
In my application I have a list of questions stored in an ArrayList, and I want to display a dialog that shows one question, and then continues to the next one after the question is answered. The way that I'm currently doing it (iterating through a loop) hasn't been working because it just layers all of the dialogs on top of one another all at once which causes a host of other issues. What I'm looking for is a way to still iterate through the questions, but just change the layout of the dialog each time until it has finished each question in the list. Can anyone give me a good pointer for how to get this going?
You can make a function that takes title and message as parameters and shows a dialog.
showDialog(String title, String message){ // Show dialog code here}
Within that dialog's answer button's listener call another function (showQuestion(currentQuestion)) that iterates the arrayList till it is over
int currentQuestion=0;
ArrayList<QuestionObject> questionList;
showQuestion(int i){
if(i<questionList.size()){
showDialog(questionList.get(i).getTitle,questionList.get(i).getMessage);
currentQuestion++;
}else{
//quiz is over
}
}
I assume you mean that you just want to change 1 single layout(created within XML i.e main.xml). In order to do this, make sure that the class your working on is pointing to that layout. From there (assuming your using an Event listener for when the user submits an answer) you can change do as you want by the following:
TextView txt = (TextView) findViewById(R.id.textView); // references the txt XML element
and in your Event listener, if the answer is correct then change(Have i be a global variable thats initially set to 0).
if(i<arrayList.size()){
txt.setText(arrayList.get(++i));
}else{
txt.setText("You Finished");
}
From there, in the else statement, you can change arrayLists and reset i to 0;
If you are trying to use the positive, neutral, and negative buttons; then you may have problems with multiple dialogs. Try defining a customized layout with your own TextViews, ListViews, and Buttons. You can implement listeners and everything else like a regular layout. Then just pass your customized layout to the dialog through AlertDialog.Builder.setView().
PS If you include code examples of what you are currently doing we can provided answers that are less vague.
I have an application in the market with a bug which I can not seem to solve. Today I have tracked it down to the following method.
public void updateDealList(ArrayList<Deal> deals) {
// first call or showing bookmakrs (also gets called other times when adapter is null)
if (dealListAdapter == null || showingBookmarks || !dealListAdapter.moreDealsToDownload()) {
dealListAdapter = new EndlessDealListAdapter(getActivity(),
R.layout.deal_list_item, deals);
setListAdapter(dealListAdapter);
listView = getListView();
if (getActivity().findViewById(R.id.right_fragment_container)!=null){ // if tablet
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_LEFT);
}
showingBookmarks=false;
Log.d("UPDATE_DEAL_LIST", "Notified list created new" + dealListAdapter.getCount());
return; //PROBLEM OCCURS WHEN APP COMES IN HERE AFTER RESUMING
}
dealListAdapter.getDealListAdapter().clear();
for (Deal d: deals){
dealListAdapter.getDealListAdapter().add(d);
Log.d("UPDATE_DEAL_LIST", "added " + d.title);
}
dealListAdapter.notifyDataSetChanged();
Log.d("UPDATE_DEAL_LIST", "Notified list " + dealListAdapter.getCount());
}
This method is passed an arraylist of deal objects which are pulled down from the internet. When the application is first opened data is pulled from the internet and the above method is called which creates a new adapter which is set for the ListFragment. This method is also called when the user requests more deals.
The problem I am having is that the list sometimes thinks its empty dispite the adapter containing deals. This seems to occur when a use resume the application when their device is low on memory (I presume part of the application has been removed from ram). This method is called and the dealListAdapter is null so a new one is created and deals are added. Despite this happening the list remains empty and the user has to force close the app to get it working again.
The line below shows tat when the method is called it enters the if and 21 deals are added to the adapter. Unfortunately the list is empty to the user.
05-23 01:52:32.879: D/UPDATE_DEAL_LIST(3478): Notified list created new21
One idea (pretty long shot :)
If the app itself wasn't killed, just the activity, the system tries to recreate the activity's last state, calling the onRestoreInstanceState().
This method will not be called if the app was killed - so this is one of the big differences between the two.
The ListActivity overrides the onRestoreInstanceState(). It ensures, that the List exists, before it goes on. If the list does not exist, it inflates it from the default place.
If you're setting the contentView in the onResume() try to move it to onCreate(), that may solve the problem.
I can help more, if I see the activty's code.
You can try one or more of the following options
Try calling setListShown(false) just before setting the list adapter.
Otherwise, do
setListShown(false);
setListAdapter(null);
Otherwise, do requestLayout()