Getting view of DialogFragment - java

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.

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

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

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

Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager

I am trying to implement RecyclerView in one of my custom alert dialogue. Its crashing with error called
Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
on this line
hsv_font_bartextview.setLayoutManager(layoutManager);
My function code of custom dialogue is like below
private void showGotoPageDialog() {
final Dialog mDialog = new Dialog(SettingsActivity.this);
mDialog.setContentView(R.layout.font_dialogue);
mDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayoutManager layoutManager
= new LinearLayoutManager(SettingsActivity.this, LinearLayoutManager.HORIZONTAL, false);
hsv_font_bartextview=(RecyclerView)findViewById(R.id.hsv_font_bartextview);
hsv_font_bartextview.setLayoutManager(layoutManager);
TextAdapterTextview textAdaptertextview = new TextAdapterTextview(SettingsActivity.this, Globle.getFontArray());
hsv_font_bartextview.setAdapter(textAdaptertextview);
textAdaptertextview.setOnClickLIstner(new OnTClickLIstner() {
#Override
public void onClick(View v, String image, int position) {
Toast.makeText(SettingsActivity.this,image,Toast.LENGTH_SHORT).show();
}
});
mDialog.show();
TextView dismiss = (TextView) mDialog.findViewById(R.id.dialog_dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDialog.dismiss();
}
});
}
Let me know if someone can help me for solve my issue. Thanks
hsv_font_bartextview=(RecyclerView)findViewById(R.id.hsv_font_bartextview);
findViewById() returns null, because your activity does not have a widget with that ID. Your dialog might, but the dialog is not the activity. Call findViewById() on the dialog if that is where the hsv_font_bartextview is.

Android Nullpointerexception on Recyclerview after switching fragments back and forth [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have a navigation drawer app with couple fragments. One of them utilizes recyclerviewadapter/recyclerviewholder to display items. Switching between fragments works well but AFTER switching, clicking on an item with listener causes nullpointerexception. Now, I tested it without the listener and it works fine. I'm guessing that the listener isn't refreshed? when fragment is swiping? Can someone help me out please? Been searching for days and yet I can't fix it.
Process: com.mikepenz.materialdrawer.app.debug, PID: 30803
java.lang.NullPointerException: Attempt to invoke interface method
'void
com.mikepenz.materialdrawer.app.RecyclerViewAdapter$OnItemClickListener.onItemClick(java.lang.String)'
on a null object reference
at
com.mikepenz.materialdrawer.app.RecyclerViewAdapter$1.onClick(RecyclerViewAdapter.java:43)
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)
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<ItemObject> itemList;
private Context context;
private OnItemClickListener listener;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
this.context = context;
}
#Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView, context);
return rcv;
}
#Override
public void onBindViewHolder(RecyclerViewHolders holder, final int position) {
holder.countryName.setText(itemList.get(position).getName());
// holder.countryPhoto.setImageResource(itemList.get(position).getPhoto());
holder.countryName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// listener.onItemClick(itemList.get(position).getName());
Log.d("HELLO ",itemList.get(position).getName());
if (position ==1)
{
listener.onItemClick("GA");
}
}
});
}
public interface OnItemClickListener{
void onItemClick(String textName);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
#Override
public int getItemCount() {
Log.e("itemlist size ", this.itemList.size() + "");
return this.itemList.size();
}
}
replace your following code
listener.onItemClick("GA");
with
if(listener!=null){
listener.onItemClick("GA");
}
it may be work for you, may be your listener null when you try to set onItemClick

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