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.
Related
i want to create add data to SQL lite with pop up dialog, in dialog just have one textedit and buttom save. in Snackbar usually use setAction(). but in dialog I don't know how to call "AddData".
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.list_category_expanses, container, false);
mTextMessage = (TextView) view.findViewById(R.id.message);
textSubcategoryExpanses = (AppCompatTextView) view.findViewById(R.id.textSubcategoryExpanses);
recyclerViewCategoryExpanses = (RecyclerView) view.findViewById(R.id.recyclerViewCategoryExpanses);
Button btn1 = view.findViewById(R.id.buttonListCategoryExpanses);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog fbDialogue = new Dialog(getActivity());
fbDialogue.setContentView(R.layout.layout_add_category_expanses);
fbDialogue.setCancelable(true);
fbDialogue.show();
}
});
initObjects();
return view;
}
and i want to call this code AddData to onCreate, this code to save text in sqlLite.
public View.OnClickListener AddData = new View.OnClickListener() {
#Override
public void onClick(View view) {
View customLayoutView = View.inflate(getActivity(), R.layout.layout_add_category_expanses, null);
final EditText edNim = customLayoutView.findViewById(R.id.inputcategoryexpanses);
final AlertDialog.Builder builder;
builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(R.string.strTitleAlert);
builder.setView(customLayoutView);
builder.setPositiveButton(R.string.btnKlikstr, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String nim = edNim.getText().toString();
if (databaseHelper.addCategoryExpanses(new CategoryExpanses(nim))) {
getDataFromSQLite();
Toast.makeText(getActivity(), "Data berhasil disimpan", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Data gagal disimpan", Toast.LENGTH_LONG).show();
}
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
};
Please help me solve this problem. thank you.
You must do some like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.list_category_expanses, container, false);
mTextMessage = (TextView) view.findViewById(R.id.message);
textSubcategoryExpanses = (AppCompatTextView) view.findViewById(R.id.textSubcategoryExpanses);
recyclerViewCategoryExpanses = (RecyclerView) view.findViewById(R.id.recyclerViewCategoryExpanses);
Button btn1 = view.findViewById(R.id.buttonListCategoryExpanses);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View customLayoutView = View.inflate(getActivity(), R.layout.layout_add_category_expanses, null);
final EditText edNim = customLayoutView.findViewById(R.id.inputcategoryexpanses);
final AlertDialog.Builder builder;
builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(R.string.strTitleAlert);
builder.setView(customLayoutView);
builder.setPositiveButton(R.string.btnKlikstr, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String nim = edNim.getText().toString();
if (databaseHelper.addCategoryExpanses(new CategoryExpanses(nim))) {
getDataFromSQLite();
Toast.makeText(getActivity(), "Data berhasil disimpan", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Data gagal disimpan", Toast.LENGTH_LONG).show();
}
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
initObjects();
return view;
}
I have a dialog method in my fragment, which I want to call from my main activity FAB.
I tried to call it, but got this error;
Attempt to invoke virtual method 'android.view.LayoutInflater android.support.v4.app.FragmentActivity.getLayoutInflater()' on a null object reference
editProfileDialog() is the dialog method in my fragment that I tried to call in my main activity.
public class Edit_fragment extends Fragment {
private EditPresenter mEditPresenter;
public Edit_fragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.edit_profile_fragment, container, false);
ButterKnife.bind(this, view);
mEditPresenter = new EditPresenter(this);
mEditPresenter.onCreate();
return view;
}
public void editProfileDialog(){
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.edit_profile_dialogue, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(dialoglayout);
builder.setCancelable(true);
TextView edit_profile = (TextView)dialoglayout.findViewById(R.id.edit_me);
Typeface Sms = Typeface.createFromAsset(getActivity().getAssets(), "fonts/FTLTLT.TTF");
edit_profile.setTypeface(Sms);
TextView new_profile_photo = (TextView)dialoglayout.findViewById(R.id.addAvatar);
new_profile_photo.setTypeface(Sms);
TextView edit_username = (TextView)dialoglayout.findViewById(R.id.editUsernameBtn);
edit_username.setTypeface(Sms);
TextView Change_bio = (TextView)dialoglayout.findViewById(R.id.statusLayout);
Change_bio.setTypeface(Sms);
final AlertDialog dialog = builder.create();
new_profile_photo.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dialog.dismiss();
editPhotoDialog();
}
});
edit_username.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Change_bio.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
private void editPhotoDialog(){
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.edit_photo_dialogue, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(dialoglayout);
builder.setCancelable(true);
FrameLayout cameraBtn = (FrameLayout)dialoglayout.findViewById(R.id.cameraBtn);
FrameLayout galleryBtn = (FrameLayout)dialoglayout.findViewById(R.id.galleryBtn);
AppCompatTextView cameraBtnTxt = (AppCompatTextView)dialoglayout.findViewById(R.id.cameraBtnTxt);
Typeface Sms = Typeface.createFromAsset(getActivity().getAssets(), "fonts/FTLTLT.TTF");
cameraBtnTxt.setTypeface(Sms);
AppCompatTextView galleryBtnText = (AppCompatTextView)dialoglayout.findViewById(R.id.galleryBtnText);
galleryBtnText.setTypeface(Sms);
final AlertDialog dialog = builder.create();
cameraBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dialog.dismiss();
setCameraBtn();
}
});
galleryBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dialog.dismiss();
setGalleryBtn();
}
});
dialog.show();
}
private void setGalleryBtn() {
if (PermissionHandler.checkPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
AppHelper.LogCat("Read data permission already granted.");
new PickerBuilder(getActivity(), PickerBuilder.SELECT_FROM_GALLERY)
.setOnImageReceivedListener(imageUri -> {
Intent data = new Intent();
data.setData(imageUri);
AppHelper.LogCat("new image SELECT_FROM_GALLERY" + imageUri);
mEditPresenter.onActivityResult(this, AppConst.SELECT_PROFILE_PICTURE, RESULT_OK, data);
})
.setImageName(getActivity().getString(R.string.app_name))
.setImageFolderName(getActivity().getString(R.string.app_name))
.setCropScreenColor(R.color.colorPrimary)
.withTimeStamp(false)
.setOnPermissionRefusedListener(() -> {
PermissionHandler.requestPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
})
.start();
} else {
AppHelper.LogCat("Please request Read data permission.");
PermissionHandler.requestPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
}
}
private void setCameraBtn() {
if (PermissionHandler.checkPermission(getActivity(), Manifest.permission.CAMERA)) {
AppHelper.LogCat("camera permission already granted.");
new PickerBuilder(getActivity(), PickerBuilder.SELECT_FROM_CAMERA)
.setOnImageReceivedListener(imageUri -> {
AppHelper.LogCat("new image SELECT_FROM_CAMERA " + imageUri);
Intent data = new Intent();
data.setData(imageUri);
mEditPresenter.onActivityResult(this, AppConst.SELECT_PROFILE_CAMERA, RESULT_OK, data);
})
.setImageName(getActivity().getString(R.string.app_name))
.setImageFolderName(getActivity().getString(R.string.app_name))
.setCropScreenColor(R.color.colorPrimary)
.withTimeStamp(false)
.setOnPermissionRefusedListener(() -> {
PermissionHandler.requestPermission(getActivity(), Manifest.permission.CAMERA);
})
.start();
} else {
AppHelper.LogCat("Please request camera permission.");
PermissionHandler.requestPermission(getActivity(), Manifest.permission.CAMERA);
}
}
}
On my fab.setOnClickListener() that is in my main_activity. I called editProfileDialog() like this;
mEdit_profile_fragment.editProfileDialog();
AND THAT ERROR SHOWED UP IN THE LOGCAT.
LayoutInflater inflater=(LayoutInflater)getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialoglayout = inflater.inflate(R.layout.edit_profile_dialogue, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(dialoglayout);
builder.setCancelable(true);
Try this way..
Edit_fragment homeFragment = (Edit_fragment) getSupportFragmentManager().findFragmentByTag(Edit_fragment.class.getSimpleName());
if(fragment != null){
fragment.editProfileDialog();
}
Use this,
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
Change your method to this:
public void editProfileDialog(Context context){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialoglayout = inflater.inflate(R.layout.edit_profile_dialogue, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(dialoglayout);
builder.setCancelable(true);
This is because getActivity() is not available in activity class.
I have an application with a listview and a button on screen that opens a dialog box. Dialog box has one EditText field where user can input text and click Add. onClickListener for Add button makes a async call and passes text to api. Problem is EditText value is not being saved. I cant even get it to show in the Log. Im sure Im missing something simple. Your help is imprecated. Here is the code.
SignInActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
Log.v("SignInActivity","Activity has began");
final DancerAdapter adapter = new DancerAdapter(this,oneDancerArrayList,1);
Log.v("SignInActivity","DancerAdapter");
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
getGirlList();
nameInput = (EditText) findViewById(R.id.cr_room_name);
loginDancer = (Button) findViewById(R.id.addDancer);
loginDancer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DialogFragment newFragment = new AddDancerDialog();
newFragment.show(getFragmentManager(), "newDancer");
}
});
static public class AddDancerDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(R.layout.signin_dialog);
LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
//final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);
builder.setMessage("Login New Dancer")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final ProgressBar newProgress = (ProgressBar)dialogview.findViewById(R.id.progressBar);
final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);
String nameSubmit = dancerName.getText().toString();
Log.v("SignInActivity-Dialog",nameSubmit);
newProgress.setVisibility(View.VISIBLE);
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("action", "addDancer");
params.put("name",nameSubmit);
Log.v("SignInActivity-Dialog", "Add Dancer Function");
Log.v("SignInActivity-Dialog",dancerName.getText().toString());
client.post("http://peekatu.com/apiweb/girlList.php", params,
new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
Log.v("response", response);
responseString2 = response;
//parseDancerList(response);
Log.v("SignInActivity",response);
}
#Override
public void onFailure(Throwable error, String content) {
Log.v("response", "response failed network error");
//waitncall(true);
}
});
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
}
}
Problem is here:
Here you set a view to dialog
builder.setView(R.layout.signin_dialog);
Then, you search a EditText in this view:
LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
final EditText dancerName = (EditText)dialogview.findViewById(R.id.etDancerName);
That dialogView was inflated but it is not the view that was added to
dialog. They are different..
I think you can fix as follows:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getContext());
final View dialogview = inflater.inflate(R.layout.signin_dialog, null);
builder.setView(dialogview);
...
}
I have a listview setonlongclicklistner to open a custom dialog showing delete and cancel.
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor) sp.getItem(position);
int c_id = c.getInt(0);
String userid = c.getString(1) + c_id;
frag = new UserListDeleteFragment();
Bundle bundle = new Bundle();
bundle.putString("id", "" + c_id);
bundle.putString("userid", userid);
frag.setArguments(bundle);
frag.setCancelable(false);
frag.show(Lenderlist.this.getSupportFragmentManager(), "f");
return true;
}
});
Below is the code for custom dialog which will delete the selected list item
public class UserListDeleteFragment extends DialogFragment {
TextView tv1,tv2;
Dialog d;
String user_id,ids;
MyDatabase m;
public UserListDeleteFragment() {
// Required empty public constructor
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
d=null;
m=new MyDatabase(getActivity());
AlertDialog.Builder ab=new AlertDialog.Builder(getActivity());
View v=getActivity().getLayoutInflater().inflate(R.layout.fragment_user_list_delete, null);
tv1= (TextView) v.findViewById(R.id.textviewbutton1);
tv2= (TextView) v.findViewById(R.id.textviewbutton2);
Bundle b=getArguments();
user_id = getArguments().getString("userid");
ids=getArguments().getString("id");
tv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean b;
m.open();
b=m.deleteTitle(ids);
m.close();
m.open();
m.deletesublist_user(user_id);
m.close();
if(b==true)
{
Toast.makeText(getActivity(),"Deleted",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getActivity(),"Not Deleted",Toast.LENGTH_SHORT).show();
}
d.dismiss();
}
});
tv2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
ab.setView(v);
d=ab.create();
return d;
}
}
i want to update my listview from first fragment when the dialog dismiss.Any one have solution for this. am not good in android. also sorry for bad english. thanks in advance.
Override the onDismiss method for your UserListDeleteFragment to call notifyDataSetChanged of your list's adapter
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();
}
}
});
}
});