调用DatePickerDialog时应用程序崩溃

时间:2013-03-05 22:23:56

标签: android android-intent nullpointerexception

我开始在开发人员控制台中显示以下NullPointerException以获取我的某个应用

java.lang.NullPointerException at com.lead.soft.deve.myapp.ae.onDateSet(Unknown Source) at
android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:148) at 
android.app.DatePickerDialog.onStop(DatePickerDialog.java:155) at 
android.app.Dialog.dismissDialog(Dialog.java:335) at 
android.app.Dialog.dismiss(Dialog.java:311) at 
com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:178) at
android.os.Handler.dispatchMessage(Handler.java:99) at 
android.os.Looper.loop(Looper.java:155) at 
android.app.ActivityThread.main(ActivityThread.java:5454) at 
java.lang.reflect.Method.invokeNative(Native Method) at 
java.lang.reflect.Method.invoke(Method.java:511) at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796) at 
dalvik.system.NativeStart.main(Native Method)

我正在使用此代码添加和编辑存储在App MySQL数据库中的日期“DOB”,请注意我删除了不适用于我正在获取的错误的代码

package com.leaders.software.development.myleadersbook;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class tab2 extends Activity implements View.OnClickListener {
Button ConfirmButton, CallSpouseButton, EmailSpouseButton, HomePhoneButton,
        WorkPhoneButton, CellPhoneButton, EmailAddressButton, SMSButton;

private static final int HELP_ID = Menu.FIRST;
private Long mRowId;
private NotesDbAdapter mDbHelper;
private TextView mdobText;
private TextView mdobDisplay;
private Button mPickdob;
private Calendar dobDate;


static final int DATE_DIALOG_ID = 0;

private TextView activeDateDisplay;
private Calendar activeDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();
    this.getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    setContentView(R.layout.tab2);


    mdobText = (TextView) findViewById(R.id.dob);

    /* capture our View elements for the start date function */
    mdobDisplay = (TextView) findViewById(R.id.dob);
    mPickdob = (Button) findViewById(R.id.pickdob);
    /* get the current date */
    dobDate = Calendar.getInstance();
    /* add a click listener to the button */
    mPickdob.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDateDialog(mdobDisplay, dobDate);
        }
    });

    mRowId = null;
    mRowId = (savedInstanceState == null) ? null
            : (Long) savedInstanceState
                    .getSerializable(NotesDbAdapter.KEY_ROWID);
    if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                : null;
    }

    populateFields();

private void updateDisplay(TextView dateDisplay, Calendar date) {
    dateDisplay.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(date.get(Calendar.MONTH) + 1).append("-")
            .append(date.get(Calendar.DAY_OF_MONTH)).append("-")
            .append(date.get(Calendar.YEAR)).append(" "));
}

public void showDateDialog(TextView dateDisplay, Calendar date) {
    activeDateDisplay = dateDisplay;
    activeDate = date;
    showDialog(DATE_DIALOG_ID);
}

private OnDateSetListener dateSetListener = new OnDateSetListener() {
    // @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        activeDate.set(Calendar.YEAR, year);
        activeDate.set(Calendar.MONTH, monthOfYear);
        activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateDisplay(activeDateDisplay, activeDate);
        unregisterDateDisplay();
    }
};

private void unregisterDateDisplay() {
    activeDateDisplay = null;
    activeDate = null;
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, dateSetListener,
                activeDate.get(Calendar.YEAR),
                activeDate.get(Calendar.MONTH),
                activeDate.get(Calendar.DAY_OF_MONTH));
    }
    return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);
    switch (id) {
    case DATE_DIALOG_ID:
        ((DatePickerDialog) dialog).updateDate(
                activeDate.get(Calendar.YEAR),
                activeDate.get(Calendar.MONTH),
                activeDate.get(Calendar.DAY_OF_MONTH));
        break;
    }
}

private void populateFields() {
    if (mRowId != null) {
        Cursor note = mDbHelper.fetchNote(mRowId);
        startManagingCursor(note);
        mdobText.setText(note.getString(note
                .getColumnIndexOrThrow(NotesDbAdapter.KEY_DOB)));

    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    saveState();
    outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mDbHelper != null) {
        mDbHelper.close();
    }
}

@Override
protected void onPause() {
    super.onPause();
    saveState();
}

@Override
protected void onResume() {
    super.onResume();
    populateFields();

}

private void saveState() {

    String dob = mdobText.getText().toString();

    if (mRowId == null) {
        long id = mDbHelper.createNote(dob);
        if (id > 0) {
            mRowId = id;
        }
    } else {
        mDbHelper.updateNote(mRowId, dob);
    }
}
}

在黑暗中拍摄时我将以下内容添加到Manifest文件中。但仍然没有运气

 android:configChanges="orientation"

我将上面的代码添加到我的Manifest文件后,在运行版本2.3的测试手机上不再出现此错误,但其他一些代码仍然存在

可能是“”

之间的空间
.append(date.get(Calendar.YEAR)).append(" "));

我也发现了这个搜索

在活动中添加此代码:

@Override

public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

并在清单文件中使用它:

<activity android:name="MyActivity" android:label="@string/app_name" 
android:configChanges="orientation|keyboardHidden"></activity>

这是一个好的解决方法

0 个答案:

没有答案