0

我有两个 java 类,在第一个活动中,我创建了一个数据库并将一些数据存储在 sqlite DB 中。在第二个 java 类中,将检索数据并检查当前日期和时间。但是第二节课没有开始。它在 logcat 中显示空指针异常。第一类是:

package com.example.eventmanager;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import com.example.eventmanager.R;



import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity {
    MediaPlayer mp;
    EditText name;
    EditText venue;
    public SQLiteDatabase eventsDB;



    private ScheduleClient scheduleClient;

    private DatePicker picker;

    private TimePicker timePic;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        scheduleClient = new ScheduleClient(this);
        scheduleClient.doBindService();

         picker = (DatePicker) findViewById(R.id.scheduleTimePicker);
         timePic = (TimePicker) findViewById(R.id.timePicer);
         name = (EditText) findViewById(R.id.ename);
         venue = (EditText) findViewById(R.id.venue);




    }


    public void onDateSelectedButtonClick(View v){

         eventsDB = this.openOrCreateDatabase("eventsDB", 0, null);
         eventsDB.setVersion(2);
         eventsDB.setLocale(Locale.getDefault());

         eventsDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                 " events " +
                 " (name VARCHAR, date VARCHAR,time VARCHAR, venue VARCHAR);");



        int day = picker.getDayOfMonth();
        int month = picker.getMonth();
        int year = picker.getYear();
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);
        c.set(Calendar.HOUR_OF_DAY, timePic.getCurrentHour());
        c.set(Calendar.MINUTE, timePic.getCurrentMinute());
        c.set(Calendar.SECOND, 0);

        String eday = String.valueOf(day);
        SimpleDateFormat month_date = new SimpleDateFormat("MMM"); // or "MMM" for short month name
        String emonth = month_date.format(c.getTime());


    //  String emonth = String.valueOf(month);
        String eyear = String.valueOf(year);
        String edate = eday+","+emonth+" "+eyear;

        String ehour = String.valueOf(timePic.getCurrentHour());
        String eminute = String.valueOf(timePic.getCurrentMinute());
        String etime = ehour+" : "+eminute;




        eventsDB.execSQL("INSERT INTO " +
                "events" +
                " Values ('"+name+"','"+edate+"','"+etime+"','"+venue+"');");




        scheduleClient.setAlarmForNotification(c);
        Toast.makeText(this, "The event for: "+ day +"/"+ (month+1) +"/"+ year+" Has been set", Toast.LENGTH_SHORT).show();


    }

    @Override
    protected void onStop() {
        if(scheduleClient != null)
            scheduleClient.doUnbindService();
        super.onStop();
    }


}

第二类是:

package com.example.eventmanager;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;






public class AlertActivity extends Activity {
    TextView test;
    public SQLiteDatabase eventsDB;
    private DatePicker picker;
    private TimePicker timePic;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert);

        test = (TextView) findViewById(R.id.textView1);


        Calendar c = Calendar.getInstance(); 
        int day = c.get(Calendar.DAY_OF_MONTH);

        //int month = c.get(Calendar.MONTH);
        SimpleDateFormat month_date = new SimpleDateFormat("MMM"); // or "MMM" for short month name
        String month = month_date.format(c.getTime());

        int year = c.get(Calendar.YEAR);

        String eday = String.valueOf(day);
        String emonth = String.valueOf(month);
        String eyear = String.valueOf(year);
        String edate = eday+","+emonth+" "+eyear;

        String ehour = String.valueOf(timePic.getCurrentHour());
        String eminute = String.valueOf(timePic.getCurrentMinute());
        String etime = ehour+" : "+eminute;

        String q="Select * from events where date='" + edate + "' and time='" + etime+ "'";

        Cursor m = eventsDB.rawQuery(q, null);

        if(edate.matches("") || etime.matches("") ){
            //Toast.makeText(getApplicationContext(), "Please insert a student ID", Toast.LENGTH_LONG).show();

        }
        else {
            if(m.moveToFirst() )
            {
            String curevent = m.getString(m.getColumnIndex("name"));
            String curdate = m.getString(m.getColumnIndex("edate"));

            test.setText(curevent);


            }
            else {
                test.setText("Not found");

            }
            }




    }


}

日志在这里:

09-14 19:55:29.954: D/GalleryCacheReady(2754): Receive action.ACTION_MEDIA_SCANNER_FINISHED
09-14 19:55:29.959: E/AndroidRuntime(1266): FATAL EXCEPTION: main
09-14 19:55:29.959: E/AndroidRuntime(1266): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eventmanager/com.example.eventmanager.AlertActivity}: java.lang.NullPointerException
09-14 19:55:29.959: E/AndroidRuntime(1266):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at android.os.Looper.loop(Looper.java:137)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at android.app.ActivityThread.main(ActivityThread.java:4898)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at java.lang.reflect.Method.invokeNative(Native Method)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at java.lang.reflect.Method.invoke(Method.java:511)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at dalvik.system.NativeStart.main(Native Method)
09-14 19:55:29.959: E/AndroidRuntime(1266): Caused by: java.lang.NullPointerException
09-14 19:55:29.959: E/AndroidRuntime(1266):     at com.example.eventmanager.AlertActivity.onCreate(AlertActivity.java:47)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at android.app.Activity.performCreate(Activity.java:5206)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
09-14 19:55:29.959: E/AndroidRuntime(1266):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
09-14 19:55:29.959: E/AndroidRuntime(1266):     ... 11 more
09-14 19:55:30.009: D/dalvikvm(1266): GC_CONCURRENT freed 204K, 6% free 13512K/14343K, paused 17ms+5ms, total 37ms

请帮助我找出错误,因为我是 android 应用程序开发的新手。

4

3 回答 3

0

未初始化的对象。例如,您的eventsDBinAlertActivity.onCreate()为空。你还没有初始化它。其他答案也指出了其他问题。

建议:

  1. 学习阅读堆栈跟踪。他们查明问题发生的确切来源。在这种情况下,AlertActivity.java:47

  2. 学习使用调试器。通过一次单步执行你的代码,你可以检查你的变量和程序的一般状态,以及为什么它的行为是这样的。

于 2013-09-14T14:06:55.993 回答
0

问题在于String ehour = String.valueOf(timePic.getCurrentHour());,您永远不会初始化timePic!另外,请注意它eventsDB也是空的。

根据评论,您可以使用String ehour = String.valueOf(c.get(Calendar.HOUR); And minute 获得小时:String eminute = String.valueOf(c.get(Calendar.MINUTE);

于 2013-09-14T14:07:44.873 回答
0

不仅复制/粘贴 logcat 输出,而且在发布您的问题之前实际阅读它,这对您很有帮助。

09-14 19:55:29.959: E/AndroidRuntime(1266): Caused by: java.lang.NullPointerException
09-14 19:55:29.959: E/AndroidRuntime(1266):     at com.example.eventmanager.AlertActivity.onCreate(AlertActivity.java:47)

这不是失败的地方吗?String ehour = String.valueOf(timePic.getCurrentHour());是那里的代码行,所以也许你应该检查timePic. 该变量似乎没有任何初始化,这与您的崩溃非常一致。

于 2013-09-14T14:09:06.097 回答