初始化数据库适配器类时出现NullPointerException

时间:2013-12-29 15:35:04

标签: android nullpointerexception

在我的MainActivity中,我有另一个类需要在onClick事件中将一些记录插入到数据库中 我收到Null Pointer Exception,应用程序崩溃并在LogCat中显示以下错误:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.note.SimpleCalendarViewActivity$GridCellAdapter$2$1.onClick(SimpleCalendarViewActivity.java:442)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
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:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

这是MainActivity类:

public class SimpleCalendarViewActivity extends Activity implements OnClickListener
{
    private static final String tag = "SimpleCalendarViewActivity";

    private ImageView calendarToJournalButton;
    private Button selectedDayMonthYearButton;
    private Button currentMonth;
    private ImageView prevMonth;
    private ImageView nextMonth;
    private GridView calendarView;
    private GridCellAdapter adapter;
    private Calendar _calendar;
    private int month, year;
     NotesDbAdapter adap;
    private final DateFormat dateFormatter = new DateFormat();
    private static final String dateTemplate = "MMMM yyyy";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.simple_calendar_view);

             adap = new NotesDbAdapter(this);
        }

以下类位于SimpleCalendarViewActivity类

public class GridCellAdapter extends BaseAdapter implements OnClickListener
        {
                      @Override
            public void onClick(View view)
                {


                    final String date_month_year = (String) view.getTag();
                    selectedDayMonthYearButton.setText("Selected: " + date_month_year);
                    Toast.makeText(getApplicationContext(), "bvbnv", Toast.LENGTH_SHORT).show();

                    AlertDialog.Builder builder = new AlertDialog.Builder(SimpleCalendarViewActivity.this);
                    builder.setMessage("New Diary").setTitle("");

                    builder.setNeutralButton("View Diaries", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            // TODO Auto-generated method stub

                        }
                    });

                    builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            // TODO Auto-generated method stub
                             AlertDialog.Builder builder2 = new AlertDialog.Builder(SimpleCalendarViewActivity.this);

                             builder2.setMessage("New Diary");

                            LayoutInflater inflator = getLayoutInflater();

                            builder2.setView(inflator.inflate(R.layout.note_edit, null)).setPositiveButton("Save", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // TODO Auto-generated method stub

                                    EditText title =  (EditText) findViewById(R.id.title);
                                    EditText body = (EditText) findViewById(R.id.body);


                                    adap.open();

                                    adap.createNote(title.getText().toString(), body.getText().toString(), date_month_year);
                                    Toast.makeText(getApplicationContext(), "Successfully added", Toast.LENGTH_SHORT);
                                    adap.close();

                                }
                            }).setNegativeButton("cancel", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // TODO Auto-generated method stub
                                    dialog.dismiss();
                                }
                            });

                            AlertDialog ad2 = builder2.create();
                            ad2.show();

                        }
                    })
                    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            // TODO Auto-generated method stub
                            arg0.cancel();

                        }
                    });

                     AlertDialog ad = builder.create();

                    ad.show();





}}

我没有分享整个代码

3 个答案:

答案 0 :(得分:0)

adap = new NotesDbAdapter(this);onCreate移至您的GridCellAdapter班级。

答案 1 :(得分:0)

您很可能没有将selectedDayMonthYearButton初始化。

selectedDayMonthYearButton.setText("Selected: " + date_month_year);

答案 2 :(得分:0)

更改为

final View v = inflator.inflate(R.layout.note_edit, null);
builder.setView(v).setPositiveButton("Save", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

EditText title =  (EditText) v.findViewById(R.id.title);
EditText body = (EditText) v.findViewById(R.id.body);

如果您的edittext位于R.layout.note_edit,那么您需要使用膨胀的视图对象来初始化您的视图。