Instead of using EditTexts, I have elected to use clickable categories that prompt a Dialog that accepts a user’s input. This reusable Dialog class is stored in an AllMethods.class. The initial theory was that I pass a bunch of text (for description, title, and such) and a target TextView (to setText that the user inputted in the Dialog). However, upon execution, I get the nullpointer exception when trying to do the TextView.setText(str) line of code.
So, My question: How do I successfully change the textView OR pass a string back only after the user clicks the OK button?
Also note, that the TextView had been declared and instantiated. I assume the nullpointer is because it is in a remote class. Here is the code:
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class AllMethods {
//fieldRequest is a reusable prompt to get a user's input for a field without taking up so much space
/* Legend of inputs:
* requestingActivity=the activity which is prompting the popup
* dialogMessage = the description of the editText being requested
* dialogTitle = the title to be displayed on the dialogbox
* editTextHint = the Hint attribute that will give the user an example of the expected input
* inputType = numeric, text, phone number, etc
*/
public static void fieldRequest(Activity activity, String dialogMessage, String dialogTitle, final TextView outputText, int inputType, String optionalFieldSuffix){
final Dialog dialog= new Dialog(activity);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle(dialogTitle);
//If the message/description exists, put it in
if(dialogMessage!=null||dialogMessage!=""){TextView description = (TextView) dialog.findViewById(R.id.dialog_description);
description.setText(dialogMessage);
}
//Identify the editText, set the input type appropriately and fill in the hint if applicable
final EditText inputField = (EditText) dialog.findViewById(R.id.dialog_inputbox);
if(Integer.toString(inputType)!=null){inputField.setInputType(inputType);}
if(optionalFieldSuffix!=null){TextView suffix = (TextView) dialog.findViewById(R.id.dialog_optionalFieldSuffix);
suffix.setText(optionalFieldSuffix);}
ImageButton dialogCancel = (ImageButton) dialog.findViewById(R.id.dialog_cancel);
ImageButton dialogDone = (ImageButton) dialog.findViewById(R.id.dialog_done);
dialogCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialogDone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
outputText.setText(inputField.getText().toString());
dialog.dismiss();
}
});
dialog.show();
}
}
Implement the interface in your class like this, as shown below. You do not need to pass the textview as input to this function.
Usage: