Date and Time picker issue - java

I previously had this code working, but now it doesn't for some strange reason.
Code for Date And Time Picker
btnDatePicker=(Button)view.findViewById(R.id.btn_date);
btnTimePicker=(Button)view.findViewById(R.id.btn_time);
txtDate=(EditText)view.findViewById(R.id.in_date);
txtTime=(EditText)view.findViewById(R.id.in_time);
btnDatePicker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(),
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
btnTimePicker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get Current Time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(getContext(),
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
txtTime.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
});
xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="6dp">
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/in_date"
android:layout_marginTop="82dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT DATE"
android:id="#+id/btn_date"
android:layout_alignBottom="#+id/in_date"
android:layout_toRightOf="#+id/in_date"
android:layout_toEndOf="#+id/in_date" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/in_time"
android:layout_below="#+id/in_date"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT TIME"
android:id="#+id/btn_time"
android:layout_below="#+id/btn_date"
android:layout_alignLeft="#+id/btn_date"
android:layout_alignStart="#+id/btn_date" />
<TextView
android:id="#+id/medOptions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/btn_time"
android:layout_marginTop="10dp"
android:padding="6dp"
android:text="Medication:"
android:textColor="#color/Black"
android:textSize="16sp" />
</RelativeLayout>
Error Log
android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class android.widget.DatePicker
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:112)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:82)
at com.example.shakeel.appidea2.calendar.CalendarFragment$1.onClick(CalendarFragment.java:118)
at android.view.View.performClick(View.java:5210)
at android.view.View$PerformClick.run(View.java:21328)
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:5551)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class android.widget.DatePicker
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:112) 
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:82) 
at com.example.shakeel.appidea2.calendar.CalendarFragment$1.onClick(CalendarFragment.java:118) 
at android.view.View.performClick(View.java:5210) 
at android.view.View$PerformClick.run(View.java:21328) 
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:5551) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:112) 
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:82) 
at com.example.shakeel.appidea2.calendar.CalendarFragment$1.onClick(CalendarFragment.java:118) 
at android.view.View.performClick(View.java:5210) 
at android.view.View$PerformClick.run(View.java:21328) 
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:5551) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
Caused by: android.view.InflateException: Binary XML file line #23: Binary XML file line #36: Error inflating class TextView
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.widget.DatePickerCalendarDelegate.<init>(DatePickerCalendarDelegate.java:118)
at android.widget.DatePicker.createCalendarUIDelegate(DatePicker.java:160)
at android.widget.DatePicker.<init>(DatePicker.java:140)
at android.widget.DatePicker.<init>(DatePicker.java:126)
at android.widget.DatePicker.<init>(DatePicker.java:122)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:112) 
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:82) 
at com.example.shakeel.appidea2.calendar.CalendarFragment$1.onClick(CalendarFragment.java:118) 
at android.view.View.performClick(View.java:5210) 
at android.view.View$PerformClick.run(View.java:21328) 
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:5551) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class TextView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.widget.DatePickerCalendarDelegate.<init>(DatePickerCalendarDelegate.java:118) 
at android.widget.DatePicker.createCalendarUIDelegate(DatePicker.java:160) 
at android.widget.DatePicker.<init>(DatePicker.java:140) 
at android.widget.DatePicker.<init>(DatePicker.java:126) 
at android.widget.DatePicker.<init>(DatePicker.java:122) 
at java.lang.reflect.Constructor.newInstance(Native Method) 
at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:112) 
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:82) 
at com.example.shakeel.appidea2.calendar.CalendarFragment$1.onClick(CalendarFragment.java:118) 
at android.view.View.performClick(View.java:5210) 
at android.view.View$PerformClick.run(View.java:21328) 
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:5551) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=17; index=268
at android.content.res.StringBlock.get(StringBlock.java:65)
at android.content.res.XmlBlock$Parser.getPooledString(XmlBlock.java:458)
at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:1151)
at android.content.res.TypedArray.getText(TypedArray.java:158)
at android.widget.TextView.<init>(TextView.java:1005)
at android.widget.TextView.<init>(TextView.java:703)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:60)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:963)
at android.support.v7.app.AppCompatDeleg
I'm not too sure on how to fix it, it was working perfectly before but I dont know what happened. I just updated android studio, then everything stopped working in my code, i've tried clean/rebuild and resync but still nothing.

I Solved my problem.
The problem was located within my styles.xml file. I was playing about with changing the colour of the app, but by adding a colour accent to a theme caused everything to break. Not too sure why. If someone can explain why adding a colour accent would cause such a problem, that would be great.

Try altering this with getActivity instead of getContext this is most likely whats causing the inflation issue.
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener() {

DATE PICKER
package course.examples.UI.datepicker;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class DatePickerActivity extends Activity {
private TextView mDateDisplay;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// capture our View elements
mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
mPickDate = (Button) findViewById(R.id.pickDate);
// add a click listener to the button
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date (this method is below)
updateDisplay();
}
// updates the date in the TextView
private void updateDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
}
TIME PICKER
package course.examples.UI.timepicker;
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class TimePickerActivity extends Activity {
private TextView mTimeDisplay;
private Button mPickTime;
private int mHour;
private int mMinute;
static final int TIME_DIALOG_ID = 0;
// the callback received when the user "sets" the time in the dialog
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateDisplay();
}
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// capture our View elements
mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
mPickTime = (Button) findViewById(R.id.pickTime);
// add a click listener to the button
mPickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
// get the current time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// display the current date
updateDisplay();
}
// updates the time we display in the TextView
private void updateDisplay() {
mTimeDisplay.setText(new StringBuilder().append(pad(mHour)).append(":")
.append(pad(mMinute)));
}
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,
false);
}
return null;
}
}

Related

Trouble Trying to play a song from a playlist

Kind of a newbie here, but I am trying to create a music app for my project in Android Studio and I keep running into an error every time I try to play a song from my playlist.
Here's the error on Logcat
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6597) 
at android.view.View.performClickInternal(View.java:6574) 
at android.view.View.access$3100(View.java:778) 
at android.view.View$PerformClick.run(View.java:25885) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.spectrumv1.Song.getId()' on a null object reference
at com.example.spectrumv1.SongCollection.searchSongById(SongCollection.java:30)
at com.example.spectrumv1.PlaySongActivity.handleSelection(PlaySongActivity.java:40)
at java.lang.reflect.Method.invoke(Native Method) 
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) 
at android.view.View.performClick(View.java:6597) 
at android.view.View.performClickInternal(View.java:6574) 
at android.view.View.access$3100(View.java:778) 
at android.view.View$PerformClick.run(View.java:25885) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
And here are the two classes mentioned by Logcat with errors
SongCollection.java
public class SongCollection {
public Song[] songs = new Song[2];
public SongCollection() {
Song godSpeed = new Song(
"S1001",
"(1) Godspeed",
"Frank Ocean",
"https://p.scdn.co/mp3-preview/6548bf4d7ad5b17d7602528182e586b86db8e23b?cid=2afe87a64b0042dabf51f37318616965",
2.97,
"https://i.scdn.co/image/ab67616d0000b273c5649add07ed3720be9d5526");
songs[0] = godSpeed;
}
public Song getCurrentSong(int currentSongId) {
return songs[currentSongId];
}
public int searchSongById(String Id) {
for (int index = 0; index < songs.length; index++) {
Song tempSong = songs[index];
if (tempSong.getId().equals(Id)) {
return index;
}
}
return -1;
}
public int getNextSong(int currentSongIndex) {
if (currentSongIndex >= songs.length - 1) {
return currentSongIndex;
} else {
return currentSongIndex + 1;
}
}
public int getPrevSong(int currentSongIndex) {
if (currentSongIndex <= 0) {
return currentSongIndex;
} else {
return currentSongIndex - 1;
}
}
}
And PlaySongActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class PlaySongActivity extends AppCompatActivity {
SongCollection songCollection = new SongCollection();
static ArrayList<Song> playList = new ArrayList<Song>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_song);
ImageButton S1001 = findViewById(R.id.songBG);
Picasso.with(this).load(songCollection.songs[0].getCoverArt()).into(S1001);
}
public void sendDataToActivity(int index)
{
Intent intent = new Intent(this, SongPlayer.class );
intent.putExtra("index", index);
startActivity(intent);
}
public void handleSelection(View view)
{
String resourceId = getResources().getResourceEntryName(view.getId());
int currentArrayIndex = songCollection.searchSongById(resourceId);
Log.d("temasek", "the current array position is: " + currentArrayIndex);
sendDataToActivity(currentArrayIndex);
}
}
any help would be appreciated
I think there's two issues causing your error. I think the first problem is that you created an array and didn't fill it with values
public Song[] songs = new Song[2];
...
Song godSpeed = new Song( ... )
songs[0] = godSpeed; // !! songs[1] isn't defined
And I think that if (tempSong.getId().equals(Id)) must not be working as intended, which is causing it to miss the correct song (godSpeed in songs[0]) and check the undefined element ( songs[1] )
I would recommend some sort of logging to check if it's working correctly.

Invalid Hijrah Day of Month : 31

I tried to create a simple activity with a button and a textview that showing the date. The button should show the HijriDatePicker dialog when clicked. I tried to make it using ummalqura-calendar and by the dialog, i tried use the HijriDatePicker to create the dialog.
However, after many attempts, it doesn't work. Sometimes the activity will stop when I click the button, and sometimes it just crash. I tried already for many fail attempts.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 13658
com.github.msarhan.ummalqura.calendar.DateTimeException: Invalid Hijrah day of month: 31
at com.github.msarhan.ummalqura.calendar.HijrahChronology.getEpochDay(HijrahChronology.java:255)
at com.github.msarhan.ummalqura.calendar.HijrahChronology.toGregorian(HijrahChronology.java:697)
at com.github.msarhan.ummalqura.calendar.UmmalquraCalendar.set(UmmalquraCalendar.java:259)
at net.alhazmy13.hijridatepicker.date.hijri.HijriDatePickerDialog.getEndDate(HijriDatePickerDialog.java:1028)
at net.alhazmy13.hijridatepicker.date.hijri.MonthAdapter.getCount(MonthAdapter.java:146)
at android.widget.ListView.setAdapter(ListView.java:493)
at net.alhazmy13.hijridatepicker.date.hijri.DayPickerView.refreshAdapter(DayPickerView.java:142)
at net.alhazmy13.hijridatepicker.date.hijri.DayPickerView.setController(DayPickerView.java:114)
at net.alhazmy13.hijridatepicker.date.hijri.DayPickerView.<init>(DayPickerView.java:105)
at net.alhazmy13.hijridatepicker.date.hijri.SimpleDayPickerView.<init>(SimpleDayPickerView.java:32)
at net.alhazmy13.hijridatepicker.date.hijri.HijriDatePickerDialog.onCreateView(HijriDatePickerDialog.java:340)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
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:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
and my main activity java
public class MainActivity extends AppCompatActivity implements HijriDatePickerDialog.OnDateSetListener{
TextView tvDate;
TextView etDate;
HijriDatePickerDialog.OnDateSetListener setListener;
public MainActivity() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvDate = findViewById(R.id.tv_date);
etDate = findViewById(R.id.et_date);
tvDate.setOnClickListener(v -> {
UmmalquraCalendar now = new UmmalquraCalendar();
HijriDatePickerDialog dpd = HijriDatePickerDialog.newInstance(
MainActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setMinDate(new UmmalquraCalendar(1440,0,1));
dpd.show(getSupportFragmentManager(), "Datepickerdialog");
});
}
#Override
public void onResume() {
super.onResume();
assert getFragmentManager() != null;
HijriDatePickerDialog dpd = (HijriDatePickerDialog) getSupportFragmentManager().findFragmentByTag("Datepickerdialog");
if (dpd != null) dpd.setOnDateSetListener(this);
}
#Override
public void onDateSet(HijriDatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = "You picked the following date: " + dayOfMonth + "/" + (++monthOfYear) + "/" + year;
System.out.println(date);
etDate.setText(date);
}
}
This problem seems to be a known bug in the msarhan-library. There was also a meanwhile closed issue on its related website. It indicates that a fix has been applied in the newest version.
So I suggest you to update your marhan library to version v2.0.2 which might solve your problem.
Update:
When looking at the date-picker-library you use we can see that it still uses the version v2.0.1. Either you wait until the author of the date-picker-library fixes the dependency or you build your own version of the date-picker-library.
If you use koltin this method will fix your issue
fun selectDateOfBirthHijri(context: Context, editText: EditText, childFragmentManager: FragmentManager) {
val now = Calendar.getInstance()
val dateOfBirthClickListener =
HijriDatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
now.set(Calendar.YEAR, year)
now.set(Calendar.MONTH, month)
now.set(Calendar.DAY_OF_MONTH, dayOfMonth)
editText.setText(
Formatter.format(
now.time.toString(),
Formatter.EEE_MMM_DD_HH_MM_SS_ZZZ_YYYY,
Formatter.DD_MM_YYYY
)
)
}
val ummAlquraCalendar = UmmalquraCalendar()
val b = HijriDatePickerDialog.newInstance(
dateOfBirthClickListener,
ummAlquraCalendar.get(UmmalquraCalendar.YEAR),
ummAlquraCalendar.get(UmmalquraCalendar.MONTH),
ummAlquraCalendar.get(UmmalquraCalendar.DAY_OF_MONTH)
)
b.maxDate = UmmalquraCalendar(ummAlquraCalendar.get(Calendar.YEAR),
ummAlquraCalendar.get(Calendar.MONTH),
ummAlquraCalendar.get(Calendar.DAY_OF_MONTH))
b.setTitle(context.getString(R.string.hint_date_of_birth))
b.show(childFragmentManager, "HijriDatePickerDialog")
}

How to fix this error regarding getSupportFragmentManager()?

recently i started a new project in android studios and came across an error I can't really find or see the problem with, my application crashes when it hits the line getSupportFragmentManager(). I am using it alongside of TimePickerFragment
I tried googling different things and trying different ways to implement this
ButtonListeners class
import android.app.TimePickerDialog;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
public class ButtonListeners extends FragmentActivity implements
View.OnClickListener, TimePickerDialog.OnTimeSetListener {
#Override
public void onClick(View buttons) {
switch (buttons.getId()){
case R.id.button1:
break;
case R.id.button2:
DialogFragment timePicker = new TimePickerFragment();
**FragmentManager manager = getSupportFragmentManager();** //<< error here
timePicker.show(manager,"timepicker");
}
}
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//do something
}
}
TimePickerFragment class
public class TimePickerFragment extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, DateFormat.is24HourFormat(getActivity()));
}
}
LogCat
2019-06-18 16:29:24.449 25857-25857/com.example.calendar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.calendar, PID: 25857
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2114)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:637)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:144)
at com.example.calendar.ButtonListeners.onClick(ButtonListeners.java:28)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Try to use getFragmentManager() in place of getSupportFragmentManager().
There is second option, using getActivity().getSupportFragmentManager();

DatePicker and TimePicker crash when I press the Set button

I am making a messenger that has date and time pickers pop up when I click the button. When I press the Set button to display them, the app crashes. My code is:
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
yearSet = year;
monthSet = monthOfYear + 1;
daySet = dayOfMonth;
btnDate.setText(yearSet + monthSet + daySet);
}
};
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hour, int minute) {
hourSet = hour;
minuteSet = minute;
btnTime.setText(hourSet + minuteSet);
}
};
LogCat:
E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x7f4
at android.content.res.Resources.getText(Resources.java:1068)
at android.widget.TextView.setText(TextView.java:4546)
at devncode.kemo.testApp.SendMessageActivity$3.onDateSet(SendMessageActivity.java:95)
at android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:199)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:154)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Your problem is that you are setting text of different items as integers.. For example btn.setText(int); where it should be a string btn.setText(String);
So to fix it, wherever you have something like
btnTime.setText(hourSet + minuteSet);
change it to
btnTime.setText(hourSet + minuteSet + "");
Adding the + "" will cast it as a String and not give an error.

How to call view from DatePickerFragment

package org.jaberrio.personai2;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
DataBaseManager dataBaseManager = new DataBaseManager();
String date = String.valueOf(month) + "/" + String.valueOf(day) + "/" + String.valueOf(year);
dataBaseManager.tempSaveCurrentEvent(DataBaseManager.FieldTypes.DUE_DATE,date, getActivity().getApplicationContext());
TextView dueDate = (TextView)view.getRootView().findViewById(R.id.dueDate);
dueDate.setText(dataBaseManager.tempGetCurrentEvent(DataBaseManager.FieldTypes.DUE_DATE,getActivity().getApplicationContext()));
}
}
LOGCAT:
06-23 19:04:05.451 13931-13931/org.jaberrio.personai2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: org.jaberrio.personai2, PID: 13931
java.lang.NullPointerException
at org.jaberrio.personai2.DatePickerFragment.onDateSet(DatePickerFragment.java:36)
at android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:148)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:116)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
I am calling dueDate.setText() however it is returning null. I called view.getRootView() because I am inside the view of DatePicker therefore it should return the view containing my dueDate TextView. I could also pass the View from the method calling DatePickerFragment but I do not know how.I am guessing that DatePicker view when called getRootView() does not return the view that contains R.id.dueDate. Thanks in advance.

Categories

Resources