0

I trying a pregnant app. My apps working fixed but sometime my app get java.lang.InstantiationException.

This is exception

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tupbebekailesi.medyasef.hamilelikrehberi/com.tupbebekailesi.medyasef.hamilelikrehberi.MotherActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.tupbebekailesi.medyasef.hamilelikrehberi.OpenActivity: make sure class name exists, is public, and has an empty constructor that is public
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
    at android.app.ActivityThread.access$700(ActivityThread.java:165)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5450)
    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:1187)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.tupbebekailesi.medyasef.hamilelikrehberi.OpenActivity: make sure class name exists, is public, and has an empty constructor that is public
    at android.app.Fragment.instantiate(Fragment.java:600)
    at android.app.FragmentState.instantiate(Fragment.java:97)
    at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767)
    at android.app.Activity.onCreate(Activity.java:932)
    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:204)
    at com.tupbebekailesi.medyasef.hamilelikrehberi.MotherActivity.onCreate(MotherActivity.java:92)
    at android.app.Activity.performCreate(Activity.java:5369)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
    ... 11 more
    Caused by: java.lang.InstantiationException: can't instantiate class com.tupbebekailesi.medyasef.hamilelikrehberi.OpenActivity; no empty constructor
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1130)
at android.app.Fragment.instantiate(Fragment.java:589)

I understand that This code my OpenActivity.java have not no empty constructor.

This is open activity.java file

public class OpenActivity extends Fragment implements View.OnClickListener {

    private Button done,pregnant_button;
    private Context mContext;
    private Button numbered_date;
    private EditText mom_name,pre_pregnant_weight;

    public OpenActivity(Context mContext) {
        this.mContext = mContext;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView   = inflater.inflate(R.layout.open_calculator,container,false);

        mom_name            = (EditText) rootView.findViewById(R.id.mom_name);
        pregnant_button     = (Button) rootView.findViewById(R.id.pregnant_button);
        pre_pregnant_weight = (EditText) rootView.findViewById(R.id.pre_pregnant_weight);
        done                = (Button) rootView.findViewById(R.id.done);

        mom_name.setText(MotherActivity.preferences.getString(MotherActivity.USER_NS,""));
        pregnant_button.setText(MotherActivity.preferences.getString(MotherActivity.USER_DUE_DATE,"-- / -- / --"));
        pre_pregnant_weight.setText(MotherActivity.preferences.getString(MotherActivity.USER_PRE_WEIGHT,""));

        pregnant_button.setOnClickListener(this);
        done.setOnClickListener(this);

        return rootView;
    }

    public void showDatePickerDialog() {
        DialogFragment newFragment = new CalendarItem(pregnant_button);
        newFragment.show(getFragmentManager(), "datePicker");
    }

    public boolean calculate_elapsed_time(long milisecondsdate) {
        long gecen_zaman        = System.currentTimeMillis() - milisecondsdate;
        long toplamhamilelik    = 280 * CalculateActivity.bir_gun;
        if( milisecondsdate > System.currentTimeMillis() || gecen_zaman > toplamhamilelik) {
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {

        String get_name_surname         = mom_name.getText().toString();
        String get_due_date             = pregnant_button.getText().toString();
        String get_pre_pregnant_weight  = pre_pregnant_weight.getText().toString();

        SharedPreferences.Editor editor = MotherActivity.preferences.edit();

        if (v == pregnant_button) {
            showDatePickerDialog();
        }
        else if( v == done ) {
            long pregnant_date      = MotherActivity.preferences.getLong(MotherActivity.USER_DUE_DATE_MS,0);
            if(get_name_surname.equals("Adınız Soyadınız") || get_name_surname.equals("")) {
                show_toast(R.string.empty_name_surname);
            }
            else if(get_due_date.equals("-- / -- / --")) {
                show_toast(R.string.empty_due_date);
            }
            else if(calculate_elapsed_time(pregnant_date)){
                show_toast(R.string.wrong_pregnant_date_calculate);
            }
            else if(get_pre_pregnant_weight.equals("")) {
                show_toast(R.string.empty_pre_pregnant_warning);
            }
            else {

                boolean get_due_date_state      = MotherActivity.preferences.getBoolean(MotherActivity.DUE_DATE_ENTRY, false);
                /*
                Hesaplıyıcı sayfasında tamam butonuna basıldğında
                hafta bitimine kalan gün değerini int olarak yolluyoruz.
                Kullanıcı 6. gündeyse 1 değerini 7. gündeyse 0 değerini yolluyoruz.
                Dinleyen dosya WeekAlarms.java
                 */
                Intent start_alarm =    new Intent(MotherActivity.WEEK_ALARM_NAME);
                getActivity().sendBroadcast(start_alarm);


                int week_number                 = MotherActivity.week_number();
                CRUD crud                       = new CRUD(mContext);
                List<WeekToWeekWeights> wtw     = new ArrayList<WeekToWeekWeights>();

                /*
                Uygulamaya ilk giriş olup olmadığını kontrol eder.
                İlk giriş olup olmadığına göre
                Kilo hesabı için kullanılan database şekli değişecektir.
                 */
                if(get_due_date_state){
                    int get_db_total_count      = crud.db_total_count();
                    if(week_number > get_db_total_count) {

                        for (int i = get_db_total_count + 1; i <= week_number + 1 ; i++) {
                            wtw.add(new WeekToWeekWeights(i,"--",usualy_weight_val[i-1]));
                        }
                        crud.add_weight(wtw);
                    }
                    else if(week_number < get_db_total_count) {
                        crud.delete_weights(week_number+1);
                        crud.update_weight(String.valueOf(MotherActivity.preferences.getString(MotherActivity.USER_CURRENT_WEIGHT,"--")));
                    }
                }
                else {
                    for (int i = 1; i <= week_number + 1 ; i++) {
                        wtw.add(new WeekToWeekWeights(i,"--",usualy_weight_val[i-1]));
                    }
                    crud.add_weight(wtw);
                }

                editor.putString(MotherActivity.USER_NS,get_name_surname);
                editor.putString(MotherActivity.USER_DUE_DATE,get_due_date);
                editor.putString(MotherActivity.USER_PRE_WEIGHT,get_pre_pregnant_weight);
                editor.putBoolean(MotherActivity.DUE_DATE_ENTRY,true);
                editor.apply();

                Intent go_entry_page    = new Intent(mContext,MotherActivity.class);
                go_entry_page.putExtra(MotherActivity.TARGET_RESPOND,"entry_page");
                startActivity(go_entry_page);
            }
        }
    }

    public void show_toast(int value){
        Toast.makeText(mContext,value,Toast.LENGTH_LONG).show();
    }
}

Sorry bad english. please help. thank you

1
  • I understand that This code my OpenActivity.java have not no empty constructor. then what is your question? Commented Jan 28, 2014 at 16:13

1 Answer 1

2

Well, your answer is in the question ...

From the stackStrace :

has an empty constructor that is public

So you need to define a constructor :

public OpenActivity() {
}

you can access to the Context using getActivity()

if you absolutely want to have a private to the Context (not recommended though ...), override the method onAttach & onDetach :

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mContext = activity;
}

@Override
public void onDetach() {
    mContext = null;
    super.onDetach();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.