Valera Valerianov
Valera Valerianov

Reputation: 247

Error when building APK in android studio

in my project there DialogFragment.

public class MyDatePicer extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    private TextView tv;

    public MyDatePicer(TextView tv) {
        this.tv = tv;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        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);

        Dialog picker = new DatePickerDialog(getActivity(), this, year, month, day);
        picker.setTitle("Выбереите дату");
        return picker;
    }

    @Override
    public void onStart() {
        super.onStart();
        Button nButton = ((AlertDialog) getDialog())
                .getButton(DialogInterface.BUTTON_POSITIVE);
        nButton.setText("Готово");
    }

    @Override
    public void onDateSet(android.widget.DatePicker datePicker, int year,int month, int day) {
        tv.setText(day + "-" + (month + 1) + "-" + year);
    }
}

and in my passage I call it:

 dateBegin = (TextView) v.findViewById(R.id.dateBegin);
        dateEnd = (TextView) v.findViewById(R.id.dateEnd);

....
 case R.id.dateBegin:
                dateDialog = new MyDatePicer(dateBegin);
                dateDialog.show(getActivity().getSupportFragmentManager(), "datePicker");
                break;
            case R.id.dateEnd:
                dateDialog = new MyDatePicer(dateEnd);
                dateDialog.show(getActivity().getSupportFragmentManager(), "datePicker");
                break;

everything works as I need to. I run on the emulator and on my phone and it works. but when I try to build an android APK studio produces an error:

Error:Error: This fragment should provide a default constructor (a public constructor with no arguments) (com.managment.pavel.managmentgradle.fragments.MyDatePicer) [ValidFragment]

I do not know how else to convey View(TextView my date) in another class. I do not know how to build APK so that I have. tell me what do I fix?

Upvotes: 2

Views: 2338

Answers (3)

Cailean
Cailean

Reputation: 181

I just ran into this problem myself. While it is best to redesign your app according to the recommendations given by Android Studio, all you need to do to bypass this error is build it in Debug mode instead of Release mode. That will solve your problem.

You should get the option screen to determine what kind of build when you're building your APK right after you've put in your keystore passwords.

Upvotes: 0

Andy Res
Andy Res

Reputation: 16043

You already have an answer that should solve your issue, and I will add just a few words of caution to that approach.

Adding an empty constructor should solve your issue, for the moment. However, this is subject to a NullPointerException in onDataSet() at tv.setTex(..) - textview could be null, if at some point in time the OS will try to recreate the fragment using that default constructor.

To fix this, you could check on null before setting the text, like:

if(tv!=null) {
      tv.setText(...)
} 

A better approach however, would be not to pass a TextView as a constructor parameter, but instead have a listener in your dialog that will pass the result, (the date formatted), to your calling activity. Something like this:

@Override
public void onDateSet(android.widget.DatePicker datePicker, int year,int month, int day) {
    listener.onDateSelected(day + "-" + (month + 1) + "-" + year);
}

Here's an example that uses this approach: http://android-developers.blogspot.com/2012/05/using-dialogfragments.html

And this also could be useful https://stackoverflow.com/a/12575319/1271435

Upvotes: 0

NoGall
NoGall

Reputation: 5246

Your error message suggests that it requires an empty constructor. Try this:

public class MyDatePicer extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    private TextView tv;

    public MyDatePicer(){
        // empty to satisfy the compiler.
    }

    public MyDatePicer(TextView tv) {
        this.tv = tv;
    }

// truncated...
}

Hopefully this will let you carry on :)

Upvotes: 2

Related Questions