i am trying to fetch the value in src and dest once the user clicks the navigate button but its always null. can some one plz help
static String source = "currentLocation";
static String destination = null;
private void getSrcDest(){
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.srcdest, null);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setIcon(R.drawable.srcdest_icon);
alert.setTitle("Enter Source & Destination");
alert.setView(textEntryView);
final EditText sourceText = (EditText) findViewById(R.id.sourcetext);
final EditText destinationText = (EditText) findViewById(R.id.desttext);
alert.setPositiveButton("Navigate", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try{
Toast.makeText(getApplicationContext(), sourceText.getText().toString()+destinationText.getText().toString(),
Toast.LENGTH_SHORT).show();
source = sourceText.getText().toString();
destination = destinationText.getText().toString();
System.out.println("----------------Source:"+source);
System.out.println("-----------Destination:"+destination);
}
catch(Exception e){
System.out.println("----------------Source:"+source);
System.out.println("-----------Destination:"+destination);
//if(destination.equals(null)){
final AlertDialog.Builder wrongAddressAlert = new AlertDialog.Builder(MainActivity.this);
wrongAddressAlert.setTitle("Destination Address Cannot be Null");
wrongAddressAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
getSrcDest();
}
});
wrongAddressAlert.show();
//}
}
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
alert.show();
}
I am assuming that you are having those edit text fields in srcdest.xml Layout. If yes, then you are initializing your edit texts in a wrong way. Try to initialize them this way:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.srcdest, null);
final EditText sourceText = (EditText) textEntryView.findViewById(R.id.sourcetext);
final EditText destinationText = (EditText) textEntryView.findViewById(R.id.desttext);
Related
I want to update TextView after Button OK on alertdialog clicked but can't.
As from the code, I am using ListView and using method OnItemClickListener() to handle the ItemClick from ListView.
My Code is as below :
lvMainChat.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
LayoutInflater li = LayoutInflater.from(context);
final View promptsView = li.inflate(R.layout.deskripsi, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText ciperteks2 = (EditText) promptsView
.findViewById(R.id.ciperteks2);
final EditText plainteks2 = (EditText) promptsView
.findViewById(R.id.plainteks2);
final EditText keyOfDeskripsi = (EditText) promptsView
.findViewById(R.id.keyOfDeskripsi);
final Button deskripsi = (Button) promptsView
.findViewById(R.id.btDeskripsi);
ciperteks2.setText((CharSequence) (((TextView) view.findViewById(R.id.txtMsg)).getText()));
result1 = (TextView) findViewById(R.id.txtMsg);
// set dialog message
alertDialogBuilder
.setTitle("Deskripsi Pesan")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
String name2 = plainteks2.getText().toString();
if(name2.length() == 0){
Toast.makeText(getApplicationContext(), "You need to type something in the editText", Toast.LENGTH_LONG).show();
}else{
result1.setText(name2);//why it not work
dialog.dismiss();
}
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
deskripsi.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String ciperteks = ciperteks2.getText().toString().trim();
String key2 = keyOfDeskripsi.getText().toString().trim();
String plaintext = plainteks2.getText().toString().trim();
String deKata = "";
if(!plaintext.equals(null)) {
try {
plainteks2.setText("");
deKata = CopyOfZIgZagCode1.RFDecryptionWork(ciperteks, key2);
} catch(Exception e) {}
plainteks2.setText(deKata);
}
if ( key2.isEmpty()) {
Toast.makeText(getApplicationContext(), "Textbox key can not be empty ", 1).show();
}
}
});
}
});
I am getting a null pointer exception when I click the OK button. and I am trying to get the value form an EditText in an AlertDialog. I am calling the AlertDialog from a fragment.
I believe this line is the problem
View promptView = layoutInflater.inflate(R.layout.passchange_dialog, null);
I think it is because I am passing null but I am not sure what eles to pass. Please note I am calling the AlertDialog from a fragment not an Activity.
public class fragment_account extends myFragment {
private final String firebase = "https://xxxxxxxx.firebaseio.com";
private String email = "";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_account, container, false);
SharedPreferences sp = getActivity().getPreferences(0);
TextView address = (TextView) view.findViewById(R.id.textViewEmailAddress);
email = sp.getString("email", "");
address.setText(email);
ImageView imageChangePass = (ImageView) view.findViewById(R.id.imageChangePass);
imageChangePass.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
showInputDialog(v);
}
}
);
return view;
}
protected void showInputDialog(final View view) {
try
{
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View promptView = layoutInflater.inflate(R.layout.passchange_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext());
alertDialogBuilder.setView(promptView);
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
EditText edittextOldPass = (EditText) view.findViewById(R.id.edittextOldPass);
EditText edittextNewPass = (EditText) view.findViewById(R.id.edittextNewPass);
EditText edittextConfirmPass = (EditText) view.findViewById(R.id.edittextConfirmPass);
final String oldPass = edittextOldPass.getText().toString().trim();
final String newPass = edittextNewPass.getText().toString().trim();
final String confirmPass = edittextConfirmPass.getText().toString().trim();
Firebase ref = new Firebase(firebase);
ref.changePassword(email, oldPass, newPass, new Firebase.ResultHandler() {
#Override
public void onSuccess() {
}
#Override
public void onError(FirebaseError firebaseError) {
}
});
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
catch (Exception e)
{
Log.i("myStuff", e.getMessage());
}
}
}
Any help or suggestions would be greatly appreciated. Thanks
I think you should call findViewById on promptView instead of on view variable
I guess you should initialize all those textview's before setting the layout of your dialog instead of inside your onClick method. As you have to access their values, you'll have to declare them final in order to get what the user typed in inside of your onClick function.
Why dialog.dismiss() doesn't work after click on item inside the AlertDialog ?
I have also tried alert.dismiss but i get the error dialog cannot be resolved. How can i solve that?
Here's the dialog
protected void myMarkersDialog() {
final String name = prefs.getString("Name", "");
String nameTwoo = prefs.getString("NameTwoo", "");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.my_markers_listview, null);
builder.setView(convertView);
builder.setTitle("List");
lv = (ListView) convertView.findViewById(R.id.listView1);
if (prefs.contains("Name") || prefs.contains("NameTwoo"))
{
String[] values = new String[] {name,nameTwoo};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values);
lv.setAdapter(adapter);
}
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg) {
String selected;
selected = lv.getItemAtPosition(position).toString();
if(selected.equalsIgnoreCase(name))
{
mapView.getMapViewPosition().setCenter(myMarkerGeopoint);
dialog.dismiss();
}
}
});
builder.setNeutralButton(getResources().getString(R.string.no_dialog), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // I get error!
}
});
AlertDialog alert = builder.create();
alert.show();
}
Declare the AlertDialog before build it and use it to dismiss.
AlertDialog alert;
protected void myMarkersDialog() {
final String name = prefs.getString("Name", "");
String nameTwoo = prefs.getString("NameTwoo", "");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.my_markers_listview, null);
builder.setView(convertView);
...
builder.setNeutralButton(getResources().getString(R.string.no_dialog), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
alert = builder.create();
alert.show();
}
I'm just trying to show an android Dialog but my activity crash every times.
i want to show a dialog which contains 2 EditText fields and 2 buttons.
I'm trying like this, what am i doing wrong or may be there is an easiest way to do that?
public class OsmRoadTaskActivity extends Activity {
Button Chercher, Annuler;
EditText startpoint, endpoint;
Context ctx;
#SuppressLint("CutPasteId")
#SuppressWarnings("unused")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this.getApplicationContext();
setContentView(R.layout.activity_main);
Chercher = (Button) findViewById(R.id.chercher);
Annuler = (Button) findViewById(R.id.annuler);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final AlertDialog alertd = alert.create();
alert.setTitle("Itineraire");
LayoutInflater inflater = this.getLayoutInflater();
View layout = inflater.inflate(R.layout.activity_main, null, false);
/*
* final EditText startpoint = new EditText(OsmRoadTaskActivity.this);
* LinearLayout.LayoutParams lp = new
* LinearLayout.LayoutParams(LinearLayout
* .LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
* startpoint.setLayoutParams(lp); alert.setView(startpoint);
*/
// final EditText startpoint =
// (EditText)layout.findViewById(R.id.endpoint);
final EditText startpoint = new EditText(this);
final EditText endpoint = (EditText) layout.findViewById(R.id.endpoint);
alert.setView(endpoint);
alert.setView(startpoint);
alert.setView(layout);
alert.setView(Annuler);
alert.setView(Chercher);
alert.setPositiveButton("Chercher",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
new OsmRoadTask(ctx, startpoint.getText(), endpoint
.getText()).execute();
}
});
alert.setNegativeButton("Annuler",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
alertd.dismiss();
}
});
alert.show();
}
}
Create the AlertDialog object after initializing the builder.
public class OsmRoadTaskActivity extends Activity {
Button Chercher, Annuler;
EditText startpoint, endpoint;
Context ctx;
#SuppressLint("CutPasteId")
#SuppressWarnings("unused")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this.getApplicationContext();
setContentView(R.layout.activity_main);
Chercher = (Button) findViewById(R.id.chercher);
Annuler = (Button) findViewById(R.id.annuler);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Itineraire");
LayoutInflater inflater = this.getLayoutInflater();
View layout = inflater.inflate(R.layout.activity_main, null, false);
/*
* final EditText startpoint = new EditText(OsmRoadTaskActivity.this);
* LinearLayout.LayoutParams lp = new
* LinearLayout.LayoutParams(LinearLayout
* .LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
* startpoint.setLayoutParams(lp); alert.setView(startpoint);
*/
// final EditText startpoint =
// (EditText)layout.findViewById(R.id.endpoint);
final EditText startpoint = new EditText(this);
final EditText endpoint = (EditText) layout.findViewById(R.id.endpoint);
alert.setView(endpoint);
alert.setView(startpoint);
alert.setView(layout);
alert.setView(Annuler);
alert.setView(Chercher);
alert.setPositiveButton("Chercher",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
new OsmRoadTask(ctx, startpoint.getText(), endpoint
.getText()).execute();
}
});
alert.setNegativeButton("Annuler",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
alertd.dismiss();
}
});
final AlertDialog alertd = alert.create();
alertd.show();
}
}
I am creating a chat app so I need to implement the broadcast feature, I have placed an AlertDialog within a button of another AlertDialog - so that when the user wants to add contact(s) or select all the contact names from the simple cursor adapter they are returned.
My problem is that when the contacts button is clicked the ListView shows the contact names correctly but when I click both the "Done" or "Select All" buttons, they produce a NullPointerException error instead of the checked names. Please help - thanks in advance.
Code is below for the callBroadcast() function
void callBroadcast()
{
// get broadcast.xml view
LayoutInflater li = LayoutInflater.from(context);
View broadPop = li.inflate(R.layout.broadcast, null);
contacts = (Button) broadPop.findViewById(R.id.contacts);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(broadPop);
final EditText userInput = (EditText) broadPop.findViewById(R.id.editTextDialogUserInput);
alertDialogBuilder.setCancelable(false)
.setPositiveButton("Send", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
result.setText(userInput.getText());
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// creating an alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// showing
alertDialog.show();
contacts.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
LayoutInflater li = LayoutInflater.from(context);
final View contactList = li.inflate(R.layout.be_sponge_contacts, null);
SimpleCursorAdapter listAdapter;
ListView contact_list = (ListView) findViewById( R.id.contactList );
String[] selectionArgs = null;
String[] projection = new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME+" COLLATE LOCALIZED ASC";
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP+"='"+("1")+"'";
ContentResolver cr = getContentResolver();
final Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
String[] from = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
int[] to = new int[] {R.id.rowTextViewChecked};
listAdapter = new SimpleCursorAdapter(Chats.this, R.layout.simplerow_checked, cur, from, to);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setAdapter(listAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertDialogBuilder.setCancelable(false)
.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
selected = (CheckBox) contactList.findViewById(R.id.rowTextViewChecked);
if (selected.isChecked())
{
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(Chats.this, name+" was chosen", Toast.LENGTH_SHORT).show();
}
}
})
.setNeutralButton("Select All", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
selected = (CheckBox) contactList.findViewById(R.id.rowTextViewChecked);
//setting all to be checked
selected.setChecked(true);
if (selected.isChecked())
{
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(Chats.this, name+" was chosen", Toast.LENGTH_SHORT).show();
selected.setChecked(true);
}
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// creating an alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// showing
alertDialog.show();
}
});
test = (TextView) broadPop.findViewById(R.id.namesChosen);
}