android java.lang.InstantiationException

时间:2014-01-28 15:41:24

标签: android instantiationexception

我正在尝试怀孕的应用。我的应用程序工作正常,但有时我的应用程序获得java.lang.InstantiationException。

这是例外

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)

我明白这段代码我的OpenActivity.java没有空构造函数。

这是打开的activity.java文件

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

抱歉英语不好。 请帮忙。谢谢

1 个答案:

答案 0 :(得分:2)

嗯,你的答案就是问题......

来自stackStrace:

has an empty constructor that is public

所以你需要定义一个构造函数:

public OpenActivity() {
}

您可以使用getActivity()

访问上下文

如果您绝对想要私有Context(不建议使用...),请覆盖方法onAttach&amp; onDetach

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

@Override
public void onDetach() {
    mContext = null;
    super.onDetach();
}