1

現在DatePicker、アプリケーションに を適用しましたが、 をクリックするButtonとクラッシュしました。以下はlogcatです:

08-18 00:22:39.564: E/AndroidRuntime(793): FATAL EXCEPTION: main
08-18 00:22:39.564: E/AndroidRuntime(793): java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.stts.sparetimetradingsystem/com.stts.sparetimetradingsystem.employer.EditEventActivity}:  java.lang.NullPointerException
08-18 00:22:39.564: E/AndroidRuntime(793):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-18 00:22:39.564: E/AndroidRuntime(793):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-18 00:22:39.564: E/AndroidRuntime(793):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-18 00:22:39.564: E/AndroidRuntime(793):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-18 00:22:39.564: E/AndroidRuntime(793):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 00:22:39.564: E/AndroidRuntime(793):  at android.os.Looper.loop(Looper.java:123)
08-18 00:22:39.564: E/AndroidRuntime(793):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-18 00:22:39.564: E/AndroidRuntime(793):  at java.lang.reflect.Method.invokeNative(Native Method)
08-18 00:22:39.564: E/AndroidRuntime(793):  at java.lang.reflect.Method.invoke(Method.java:521)
08-18 00:22:39.564: E/AndroidRuntime(793):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-18 00:22:39.564: E/AndroidRuntime(793):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-18 00:22:39.564: E/AndroidRuntime(793):  at dalvik.system.NativeStart.main(Native Method)
08-18 00:22:39.564: E/AndroidRuntime(793): Caused by: java.lang.NullPointerException
08-18 00:22:39.564: E/AndroidRuntime(793):  at com.stts.sparetimetradingsystem.employer.EditEventActivity.updateDisplayFrom(EditEventActivity.java:532)
08-18 00:22:39.564: E/AndroidRuntime(793):  at com.stts.sparetimetradingsystem.employer.EditEventActivity.onCreate(EditEventActivity.java:236)
08-18 00:22:39.564: E/AndroidRuntime(793):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-18 00:22:39.564: E/AndroidRuntime(793):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-18 00:22:39.564: E/AndroidRuntime(793):  ... 11 more

私のコードを配置していないことをお詫び申し上げます。これが私のコードです:

private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_event);

    edit_eventSpinner = (Spinner) findViewById(R.id.edit_spinner);
    btnSaveEvent = (Button) findViewById(R.id.btnSaveEvent);
    btnDeleteEvent = (Button) findViewById(R.id.btnDeleteEvent);
    edit_error = (TextView) findViewById(R.id.edit_error);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.salary_array,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    edit_eventSpinner.setAdapter(adapter);
    edit_eventSpinner
            .setOnItemSelectedListener(new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent,
                        View view, int position, long id) {

                    if (position == 0) {
                        rate = "/hour";
                    } else if (position == 1) {
                        rate = "/day";
                    } else {
                        rate = "/month";
                    }
                }

                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

    // getting event details from intent
    Intent i = getIntent();

    // getting event id (EID) from intent
    eid = i.getStringExtra(TAG_EID);

    // Getting complete event details in background thread
    new GetEventDetails().execute();

    // save button click event
    btnSaveEvent.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // starting background task to update event
            String e = edit_eventName.getText().toString();
            String c = edit_eventCName.getText().toString();
            String s = edit_eventSalary.getText().toString();
            String f = edit_eventFrom.getText().toString();
            String t = edit_eventTo.getText().toString();
            String whf = edit_eventWHFrom.getText().toString();
            String wht = edit_eventWHTo.getText().toString();
    });

    // Delete button click event
    btnDeleteEvent.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // deleting event in background thread
            new DeleteEvent().execute();
        }
    });

    Button edit_pickDateFrom = (Button) findViewById(R.id.edit_pickDateFrom);

    edit_pickDateFrom.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    updateDisplayFrom();
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {

    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListenerFrom, mYear,
                mMonth, mDay);
    }
    return null;
}

protected void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {

    case DATE_DIALOG_ID:
        ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
        break;
    }

}

// SET THE DATE FORMAT ALREADY USER SELECT FROM THE DATEPICKER
private void updateDisplayFrom() {
    edit_eventFrom.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(mDay).append("-").append(mMonth + 1).append("-")
            .append(mYear).append(""));
}

private DatePickerDialog.OnDateSetListener mDateSetListenerFrom = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplayFrom();
    }
};

以下は、edit_eventName などを初期化したにもかかわらず発生した新しいエラーです。

08-18 01:09:56.764: E/AndroidRuntime(977): FATAL EXCEPTION: main
08-18 01:09:56.764: E/AndroidRuntime(977): java.lang.IllegalArgumentException: current should be >= start and <= end
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.widget.NumberPicker.setCurrent(NumberPicker.java:288)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.widget.DatePicker.updateDaySpinner(DatePicker.java:364)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.widget.DatePicker.updateSpinners(DatePicker.java:350)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.widget.DatePicker.init(DatePicker.java:346)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.app.DatePickerDialog.<init>(DatePickerDialog.java:124)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.app.DatePickerDialog.<init>(DatePickerDialog.java:83)
08-18 01:09:56.764: E/AndroidRuntime(977):  at com.stts.sparetimetradingsystem.employer.EditEventActivity.onCreateDialog(EditEventActivity.java:527)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.app.Activity.onCreateDialog(Activity.java:2472)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.app.Activity.createDialog(Activity.java:881)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.app.Activity.showDialog(Activity.java:2547)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.app.Activity.showDialog(Activity.java:2514)
08-18 01:09:56.764: E/AndroidRuntime(977):  at com.stts.sparetimetradingsystem.employer.EditEventActivity$5.onClick(EditEventActivity.java:245)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.view.View.performClick(View.java:2408)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.view.View$PerformClick.run(View.java:8816)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.os.Handler.handleCallback(Handler.java:587)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.os.Looper.loop(Looper.java:123)
08-18 01:09:56.764: E/AndroidRuntime(977):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-18 01:09:56.764: E/AndroidRuntime(977):  at java.lang.reflect.Method.invokeNative(Native Method)
08-18 01:09:56.764: E/AndroidRuntime(977):  at java.lang.reflect.Method.invoke(Method.java:521)
08-18 01:09:56.764: E/AndroidRuntime(977):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-18 01:09:56.764: E/AndroidRuntime(977):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-18 01:09:56.764: E/AndroidRuntime(977):  at dalvik.system.NativeStart.main(Native Method)
4

2 に答える 2

1

edit_eventFrom投稿したコードのどこにも初期化していないようです。

View他の場合と同じように、レイアウトからそれを膨らませる必要がありViewsます。

edit_eventSpinner = (Spinner) findViewById(R.id.edit_spinner);
btnSaveEvent = (Button) findViewById(R.id.btnSaveEvent);
btnDeleteEvent = (Button) findViewById(R.id.btnDeleteEvent);
edit_error = (TextView) findViewById(R.id.edit_error);

そして、swayamViewが述べたように、コードで使用したいものに対してこれを行う必要があります。

swayam

同様に、コードで使用する前に、次のすべてのEditTextビューを初期化します


2番目のエラー

IllegalArgumentException:currentは>=startおよび<=endである必要があります

を初期化しmYear、デフォルトまたは任意の値に初期化する必要があります。mDaymMonth

このソリューションのリファレンス。

于 2012-08-17T16:47:33.590 に答える
1

XML を参照して EditText ビューを初期化していません。

EditText edit_eventName = (EditText) findViewById(R.id.idOfThisEditTextInYourXML);
EditText edit_eventFrom = (EditText) findViewById(R.id.idOfThisEditTextInYourXML);

同様に、コードで使用する前に、次のすべての EditText ビューを初期化します。

            edit_eventCName
            edit_eventSalary
            edit_eventFrom
            edit_eventTo
            edit_eventWHFrom
            edit_eventWHTo

の初期化も欠落していますmDay, mMonth and mYear。それらにいくつかの値(整数)を割り当てる必要があります。

private int mYear = 2012; // example value
private int mMonth = 7; // example value
private int mDay =17; // example value
于 2012-08-17T16:56:58.320 に答える