How to get a view id created programmatically in another class? - java

I want to find the id of a view created programmatically from a runnable set in another class and passed to the searched view's class.
The idea is to lock an application as long as the right password isn't inputed. So when on the main menu the user press "Unlock" button which prompt an overlay dialog from ViewDialog class.
showUnlockDialog() from ViewDialog is the function creating and adding a password EditText to a basic dialog set in R.layout.custom_dialog.
The user enters the password and click the button which run the runnable fetching the EditText content to retrieve the password entered.
public class MainMenu extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
....
Runnable runnable;
View v;
runnable = new Runnable(){
public void run(){
Runnable runnable = new Runnable(){
public void run(){
EditText mdp = findViewById((int)1);
Log.d("test", mdp.getText().toString());
}
};
ViewDialog alert = new ViewDialog();
alert.showUnlockDialog(MainMenu.this, runnable);
}
};
v = findViewById(R.id.button);
v.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
runnable.run();
}
});
....
}
}
public class ViewDialog extends AppCompatActivity{
Button positiveButton;
Button negativeButton;
....
public void showUnlockDialog(Activity activity, Runnable callback){
final Dialog dialog = new Dialog(activity);
dialog.setCancelable(true);
dialog.setContentView(R.layout.custom_dialog);
....
ConstraintLayout constraintLayout = new ConstraintLayout(activity);
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
constraintLayout.setLayoutParams(params);
EditText password = new EditText(activity);
password.setId((int)1);
constraintLayout.addView(password);
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.customDialogLinLayout);
layout.addView(constraintLayout);
setDialog1Button(activity, dialog, callback);
dialog.show();
}
public void setDialog1Button(Activity activity, final Dialog dialog, final Runnable callback){
positiveButton = new Button(activity);
positiveButton.setText("OK");
positiveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(callback != null) {
callback.run();
}
dialog.dismiss();
}
});
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.customDialogLinLayout);
layout.addView(positiveButton);
}
....
}
When clicking the validation button, password is null in
EditText password = findViewById((int)1);
Log.d("test", password.getText().toString());
and the following error is prompted :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.megacom.inventaire, PID: 18759
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.megacom.inventaire.MainMenu$1$1.run(MainMenu.java:55)
at com.megacom.inventaire.ViewDialog$1.onClick(ViewDialog.java:107)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

You can pass that View using intent or you can make that view to constant to change the view value on other activity or class

Related

Android app crashes when activity is initialised from fragment [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 9 months ago.
I am currently trying to create a calendar app in android studio. The app contains a calendar with different "Views" e.g. monthly view, weekly view, and daily view.
The app uses fragments as pages and each view is an activity.
This is the code for the fragment and buttons to initialise each view
public class CalendarFragment extends Fragment {
#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 v = inflater.inflate(R.layout.fragment_calendar, container, false);
Button btn1 = (Button) v.findViewById(R.id.openCalendar);
btn1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
Intent intent = new Intent(getActivity(),CalendarActivity.class);
startActivity(intent);
}
});
Button btn2 = (Button) v.findViewById(R.id.openWeek);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent = new Intent(getActivity(), WeekViewActivity.class);
startActivity(intent);
}
});
return v;
}
The first button which displays the monthly view of the calendar called "CalendarActivity" in code works fine but when the second button is clicked which displays the weekly view of the calendar, causes the app to crash and gives the following error in the logcat
2022-05-13 14:44:59.642 15183-15183/com.example.finalyearproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.finalyearproject, PID: 15183
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.finalyearproject/com.example.finalyearproject.WeekViewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.time.LocalDate.format(java.time.format.DateTimeFormatter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.time.LocalDate.format(java.time.format.DateTimeFormatter)' on a null object reference
at com.example.finalyearproject.CalendarUtils.monthYearFromDate(CalendarUtils.java:16)
at com.example.finalyearproject.WeekViewActivity.setWeekView(WeekViewActivity.java:40)
at com.example.finalyearproject.WeekViewActivity.onCreate(WeekViewActivity.java:29)
at android.app.Activity.performCreate(Activity.java:8054)
at android.app.Activity.performCreate(Activity.java:8034)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loopOnce(Looper.java:201) 
at android.os.Looper.loop(Looper.java:288) 
at android.app.ActivityThread.main(ActivityThread.java:7842) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
I am not sure what the problem is as I have been following a tutorial and the other features work. I have also included the code for each activity below.
Monthly View
public class CalendarActivity extends AppCompatActivity implements CalendarAdapter.OnItemListener {
private TextView monthYearText;
private RecyclerView calendarRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
initWidgets();
CalendarUtils.selectedDate = LocalDate.now();
setMonthView();
}
private void initWidgets()
{
calendarRecyclerView = findViewById(R.id.calendarRecyclerView);
monthYearText = findViewById(R.id.monthYearTV);
}
private void setMonthView()
{
monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate));
ArrayList<LocalDate> daysInMonth = daysInMonthArray(CalendarUtils.selectedDate);
CalendarAdapter calendarAdapter = new CalendarAdapter(daysInMonth, this);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarRecyclerView.setAdapter(calendarAdapter);
}
public void nextMonth(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.plusMonths(1);
setMonthView();
}
public void previousMonth(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.minusMonths(1);
setMonthView();
}
Weekly View
public class WeekViewActivity extends AppCompatActivity implements CalendarAdapter.OnItemListener {
private TextView monthYearText;
private RecyclerView calendarRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_week_view);
initWidgets();
setWeekView();
}
private void initWidgets()
{
calendarRecyclerView = findViewById(R.id.calendarRecyclerView);
monthYearText = findViewById(R.id.monthYearTV);
}
private void setWeekView()
{
monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate));
ArrayList<LocalDate> days = daysInWeekArray(CalendarUtils.selectedDate);
CalendarAdapter calendarAdapter = new CalendarAdapter(days, this);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarRecyclerView.setAdapter(calendarAdapter);
}
public void previousWeek(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.minusWeeks(1);
setWeekView();
}
public void nextWeek(View view)
{
CalendarUtils.selectedDate = CalendarUtils.selectedDate.plusWeeks(1);
setWeekView();
}
Any suggestions would be greatly appreciated.
My suggestions:
Firstly you didn't initialized CalendarUtils.selectedDate
Cover code with breakpoints for. debugging or with Log.d() messages to find, which variable is null

I have the following error:android.os.Bundle android.content.Intent.getExtras()' on a null object reference

I have a MainActivity with a list and when it occurs
A certain condition opens an alert dialog.
The problem is that when you open the alert dialog you have to bring
the data of a variable and put it in a textview that I have in the alertDialog
the case is that it gives me this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
The MainActivity code where I do the Intent is:
Intent intent = new Intent(MainActivity.this,ConfirmacionAlarma.class);
intent.putExtra("notificar",totales);
And just after I call alertDialog
new ConfirmacionAlarma(contexto);
The AlertDialog code is this:
public class ConfirmacionAlarma extends AppCompatActivity {
public ConfirmacionAlarma(Context contexto)
{
final Dialog dialogo = new Dialog(contexto);
dialogo.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogo.setCancelable(false);
dialogo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialogo.setContentView(R.layout.dialogo_alarma);
final TextView notiAlarm = (TextView) dialogo.findViewById(R.id.notificacionAlarma);
TextView descAlarm = (TextView) dialogo.findViewById(R.id.DescAlarma);
TextView contAla = (TextView) dialogo.findViewById(R.id.ContadorAlarmas);
EditText fechaAlar = (EditText) dialogo.findViewById(R.id.Fecha);
ImageView camapana = (ImageView) dialogo.findViewById(R.id.image1);
Button ConfirmarAviso = (Button) dialogo.findViewById(R.id.Aceptar);
ConfirmarAviso.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialogo.dismiss();
}
});
Bundle nuevoTotales = getIntent().getExtras();
String nom = nuevoTotales.getString("notificar");
notiAlarm.setText(nom);
dialogo.show();
}
It tells me that what the object brings is Null but I don't understand why.
You are trying to get data from the constructor of the Activity. You should get data in onCreate method
#Override
protected void onCreate(Bundle savedInstanceState){
String value;
Intent intent=this.getIntent();
if(intent != null){
value = intent.getStringExtra("notificar");
}
}

App crashes if I try going to a specific activity

So I am making a simple app. It's just basically making a list of win with a custom list view at the end.
It starts off on the main screen where there are two buttons, one is an "Add" button which takes you to the Add activity. If you push this it'll take you to a page where you type in the name,price, description of the wine and then hit a submit button to the list. The other button on the main screen is a "Go to List" button which just takes you directly to the list activity.
If I go through the Add button, add a wine, and then go to the list, it works fine. I can see the list. It even works if I don't add anything to the list. I can see the empty list activity.
When I push the "Go to List" button on the main screen though, it crashes and says "The application has stopped".
I don't get why I can go through the Add button to get to the list fine, but this button doesn't work at all.
Could I get some help?
Here are the three relevant activities, the AddActivity, the ListActivity, and the MainActivity.
AddActivity:
public class AddActivity extends AppCompatActivity {
EditText editWineName;
EditText editWinePrice;
EditText editWineDescription;
Button btnSubmit;
Button btnGoToList;
String stringWineName;
String stringWinePrice;
String stringWineDescription;
ArrayList<String> listWineName = new ArrayList<>();
ArrayList<String> listPrice = new ArrayList<>();
ArrayList<String> listWineDescription = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
setVariables();
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setVariables();
listWineName.add(stringWineName);
listPrice.add(stringWinePrice);
listWineDescription.add(stringWineDescription);
Toast.makeText(AddActivity.this, stringWineName + " was added to the list.", Toast.LENGTH_SHORT).show();
clearEditText();
}
});
btnGoToList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intentGoToList = new Intent(AddActivity.this,ListActivity.class);
intentGoToList.putStringArrayListExtra("WINENAME", listWineName);
intentGoToList.putStringArrayListExtra("WINEPRICE", listPrice);
intentGoToList.putStringArrayListExtra("WINEDESCRIPTION", listWineDescription);
startActivity(intentGoToList);
}
});
}
private void setVariables(){
editWineName = (EditText) findViewById(R.id.editName);
editWinePrice = (EditText) findViewById(R.id.editPrice);
editWineDescription = (EditText) findViewById(R.id.editDescription);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnGoToList = (Button) findViewById(R.id.btnGoToList);
stringWineName = editWineName.getText().toString();
stringWinePrice = "$" + editWinePrice.getText().toString();
stringWineDescription = editWineDescription.getText().toString();
}
private void clearEditText() {
editWineName.getText().clear();
editWinePrice.getText().clear();
editWineDescription.getText().clear();
}
}
ListActivity:
public class ListActivity extends AppCompatActivity {
ListView wineList;
ArrayAdapter adapter;
Button btnBacktoMain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
setVariables();
ArrayList<String> listWineName = getIntent().getStringArrayListExtra("WINENAME");
ArrayList<String > listWinePrice = getIntent().getStringArrayListExtra("WINEPRICE");
ArrayList<String> listWineDescription = getIntent().getStringArrayListExtra("WINEDESCRIPTION");
adapter = new CustomAdapter(this, listWineName, listWinePrice, listWineDescription);
wineList.setAdapter(adapter);
btnBacktoMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intentBackToMain = new Intent(ListActivity.this,MainActivity.class);
startActivity(intentBackToMain);
}
});
}
private void setVariables (){
btnBacktoMain = (Button) findViewById(R.id.btnBackToMain);
wineList = (ListView) findViewById(R.id.listWine);
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
Button btnAdd;
Button btnList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVariables();
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { //Goes to the add activity
Intent intentAdd = new Intent(MainActivity.this, AddActivity.class);
startActivity(intentAdd);
}
});
btnList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { //Goes to the list activity
Intent intentList = new Intent(MainActivity.this, ListActivity.class);
startActivity(intentList);
}
});
}
private void setVariables(){
btnAdd = (Button) findViewById(R.id.btnAddWine);
btnList = (Button) findViewById(R.id.btnViewList);
}
}
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:344)
at android.widget.ListView.setAdapter(ListView.java:493)
at com.example.jeremy.mywine.ListActivity.onCreate(ListActivity.java:33)
Your crash indicates that the data in the adapter given to the ListView in ListActivity is null. Make it not null. Start at ListActivity.java at line 33 and go backwards to find where you are (or this case are not) initializing the data in the list adapter.
In you case, you are expecting data in your intent. OK, where is your intent set up? In your MainActivity click. Well, there you just launch the activity without passing any data in the intent extras, hence there is nothing to pull out from the intent in ListActivity, hence your crash.
So you need to initialize the data in MainActivity and set it as extras in the Intent you use to launch ListActivity.
Since the ListActivity is expecting this:
ArrayList<String> listWineName = getIntent().getStringArrayListExtra("WINENAME");
ArrayList<String > listWinePrice = getIntent().getStringArrayListExtra("WINEPRICE");
ArrayList<String> listWineDescription = getIntent().getStringArrayListExtra("WINEDESCRIPTION");
You would update your MainActivity to do something like this (where getDescriptions() is a fictitious method you would create to return a list of strings)
btnList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Goes to the list activity
Intent intentList = new Intent(MainActivity.this, ListActivity.class);
intentList.putExtra("WINENAME", new ArrayList<String>()); // Empty list
intentList.putExtra("WINEPRICE", Arrays.asList("Foo", "Bar")); // Explicit list named items
intentList.putExtra("WINEDESCRIPTION", getDescriptions()); // Get list from private method
startActivity(intentList);
}
});
Also check this post my be useful for learning how to read and understand a crash log.
And check the documentation on how to start activities.
Hope that helps!

Getting view of DialogFragment

I have created Dialog Fragment with custom layout and want to set text for some textboxes. But when trying to get view in my adapter image click I'm getting null pointer exception.
I would like to know is there any best way to implement it?
Thanks in advance.
Below is my code:
public class AlertDialogFragment extends DialogFragment
{
private View dialogView;
private static IDialogButtonsClicks buttonsClicks;
private AlertDialog.Builder alertDialog;
public static AlertDialogFragment newInstance(int title, int icon, int positive, int negative, int view, int message, IDialogButtonsClicks clicks)
{
AlertDialogFragment dialogFragment = new AlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
args.putInt("icon", icon);
args.putInt("pos", positive);
args.putInt("neg", negative);
args.putInt("view", view);
args.putInt("message", message);
buttonsClicks = clicks;
dialogFragment.setArguments(args);
return dialogFragment;
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
alertDialog = new AlertDialog.Builder(getActivity());
int dView = getArguments().getInt("view");
int mess = getArguments().getInt("message");
if (dView != -1)
{
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
dialogView = layoutInflater.inflate(dView, null);
alertDialog.setView(dialogView);
}
if (mess != -1)
{
alertDialog.setMessage(mess);
}
alertDialog.setTitle(getArguments().getInt("title"));
alertDialog.setIcon(getArguments().getInt("icon"));
alertDialog.setPositiveButton(getArguments().getInt("pos"), new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
buttonsClicks.OnPositiveButtonClick(dialogView);
}
});
alertDialog.setNegativeButton(getArguments().getInt("neg"), new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
buttonsClicks.OnNegativeButtonClick();
}
});
return alertDialog.create();
}
public View getDialogView()
{
return dialogView;
}
#Nullable
#Override
public View getView()
{
return super.getView();
}
}
holder.imgCatEdit.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
AlertDialogFragment editCatDialog = AlertDialogFragment.newInstance(R.string.add_category_title, R.drawable.ic_edit, R.string.edit_cat_button_save, R.string.edit_cat_button_cancel, R.layout.edit_category, -1, new IDialogButtonsClicks()
{
#Override
public void OnPositiveButtonClick(View view)
{
}
#Override
public void OnNegativeButtonClick()
{
}
});
editCatDialog.show(((MyBusinessMainActivity)baseContext).getSupportFragmentManager(), "EditCatDialog");
View dialogView = editCatDialog.getView();//It seems here I'm getting null
EditText ed_edit_cat_name = (EditText)dialogView.findViewById(R.id.ed_edit_cat_name);
EditText ed_edit_cat_descr = (EditText)dialogView.findViewById(R.id.ed_edit_cat_descr);
ed_edit_cat_name.setText(cat_name);
ed_edit_cat_descr.setText(cat_descr);
}
});
11-23 12:40:49.708 19702-19702/am.nkr.mybusiness E/AndroidRuntime: FATAL EXCEPTION: main
Process: am.nkr.mybusiness, PID: 19702
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at am.nkr.mybusiness.CategoriesAdapter$2.onClick(CategoriesAdapter.java:165)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
It is because when you do editCatDialog.getView(); the view have not been yet initializied (onCreateDialog() was not called).
The best way would be to add listeners in dialog in onViewCreated method. Then passing those events to your main fragment (e.g. via buttonClicks variable).
If you want to make it in a quick and hacky way you could write getSupportFragmentManager().executePendingTransaction() after show method
Yeah, you got that error because when you call ed_edit_cat_name.setText(cat_name);, your DialogFragment callback onCreateView is not finished! To overcome this problem, I think you can declare some variables to hold your cat_name and cat_des in your DialogFragment as below:
public class AlertDialogFragment extends DialogFragment
{
private String catName;// getter - setter
private String catDes;//getter-setter
// TODO: find your view and set cat name, description in onCreateView callback.
}
Then, you can set your cat name, cat des when you create new instance of DialogFragment.

Create Custom AlertDialog requestFeature exception [duplicate]

This question already has answers here:
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
(12 answers)
Closed 8 years ago.
I'm trying to create my own custom AlertDialog in a DialogFragment for a simple alarm clock application, and the code for the onCreateDialog method is below.
public class CreateAlarmSetting extends DialogFragment implements View.OnClickListener, AlertDialog.OnClickListener, TimePicker.OnTimeChangedListener, NumberPicker.OnValueChangeListener {
/*
the isEdit variable is set to true when an existing alarm time is clicked, this will add the
"delete" button to the dialog, to possibly delete the item from the listview
*/
private boolean isEdit = false;
public static final String ISEDIT_PARAM = "isEdit";
public static final String ALARMSETTING_PARAM = "AlarmSetting";
public AlarmSetting alarmSetting;
private TimePicker timePicker;
private NumberPicker numberPicker;
//private Button cancelButton, createButton, deleteButton;
//View of the dialog layout that will be inflated
private View main;
private OnFragmentInteractionListener mListener;
public CreateAlarmSetting() {
}
/*
Below are a few of the Fragment lifecycle methods
*/
public void setArguments(Bundle bundle) {
isEdit = bundle.getBoolean(ISEDIT_PARAM);
alarmSetting = (AlarmSetting) bundle.get(ALARMSETTING_PARAM);
}
#Override
public void onCreate(Bundle savedInstanceState) {
getActivity().requestWindowFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
/*
Not doing anything yet, because findViewById() doesn't work at this point in the lifecycle
*/
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO: check if this is to edit a current alarmSetting and add the save button and delete button
main = inflater.inflate(R.layout.fragment_create_alarm_setting, null);
//Initialize views and their listeners
timePicker = (TimePicker) main.findViewById(R.id.alarm_fragment_dialog_timepicker);
numberPicker = (NumberPicker) main.findViewById(R.id.alarm_fragment_dialog_numberpicker);
// cancelButton = (Button) main.findViewById(R.id.alarm_fragment_dialog_cancel_button);
// createButton = (Button) main.findViewById(R.id.alarm_fragment_dialog_create_button);
timePicker.setOnTimeChangedListener(this);
numberPicker.setOnValueChangedListener(this);
//cancelButton.setOnClickListener(this);
//createButton.setOnClickListener(this);
numberPicker.setMaxValue(45);
numberPicker.setMinValue(0);
//create display based on given parameters
/*
if (isEdit) {
// ViewGroup parent= (ViewGroup) main.findViewById(R.id.alarm_fragment_dialog_button_container);
deleteButton= (Button) main.findViewById(R.id.alarm_fragment_dialog_delete_button);
deleteButton.setText("Delete");
deleteButton.getLayoutParams().height= LinearLayout.LayoutParams.WRAP_CONTENT;
deleteButton.getLayoutParams().width= LinearLayout.LayoutParams.WRAP_CONTENT;
deleteButton.setOnClickListener(this);
createButton.setText("Save");
//TODO: Set Display Settings to current alarm settings, includes having cancel, delete, save buttons
timePicker.setCurrentHour(alarmSetting.getHours());
timePicker.setCurrentMinute(alarmSetting.getMinutes());
//Replace create button with the save add the delete button
ViewGroup parent= (ViewGroup) main.findViewById(R.id.alarm_fragment_dialog_button_container);
ViewGroup v=(ViewGroup) edit.findViewById(R.id.miscellaneous_items_container);
//v.removeView(edit.findViewById(R.id.alarm_fragment_dialog_delete_button));
parent.addView(edit.findViewById(R.id.alarm_fragment_dialog_delete_button), 1);
//Replace create button with save button
parent = (ViewGroup) createButton.getParent();
int index = parent.indexOfChild(createButton);
parent.removeView(createButton);
parent.addView(parent.findViewById(R.id.alarm_fragment_dialog_save_button), index);
}*/
return main;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
if (main == null) {
main = getActivity().getLayoutInflater().inflate(R.layout.fragment_create_alarm_setting, null);
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(main);
builder.setNegativeButton("Cancel", this);
builder.setPositiveButton("Create", this);
builder.setTitle("Create a new Alarm Setting");
AlertDialog alertDialog=builder.create();
//show or create?
return alertDialog;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/*
Below are listeners for the actions in the dialog
*/
#Override
public void onClick(DialogInterface dialogInterface, int id) {
switch (id) {
case DialogInterface.BUTTON_NEGATIVE:
case DialogInterface.BUTTON_NEUTRAL:
case DialogInterface.BUTTON_POSITIVE:
}
}
#Override
public void onValueChange(NumberPicker picker, int val, int num) {
//Do something
}
#Override
public void onTimeChanged(TimePicker picker, int hours, int minutes) {
if (alarmSetting != null) {
alarmSetting.setHours(hours);
alarmSetting.setMinutes(minutes);
} else alarmSetting = new AlarmSetting(hours, minutes, false);
}
#Override
public void onClick(View v) {
//do stuff for each button
switch (v.getId()) {
}
}
When I run this code in the emulator, I receive an exception saying
01-22 20:32:39.489 1883-1883/com.stanfordude.ryan.snooze E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:215)
at com.android.internal.app.AlertController.installContent(AlertController.java:234)
at android.app.AlertDialog.onCreate(AlertDialog.java:336)
at android.app.Dialog.dispatchOnCreate(Dialog.java:351)
at android.app.Dialog.show(Dialog.java:256)
at android.app.DialogFragment.onStart(DialogFragment.java:490)
at android.app.Fragment.performStart(Fragment.java:1570)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
at android.app.BackStackRecord.run(BackStackRecord.java:635)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
How do I fix this?
Btw, sorry for my messy code and comments, I'm new to java
Its not the code as I can see from the error, its the requestWindowsFeature or something you have called before setContentView on the top. You should call the request before setting the contentView like this :
super.onCreate(savedInstanceState);
// Hiding title bar using code
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Hiding status bar using code
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);

Categories