0

我已经为事件通知程序编写了代码。将设置一个事件时间,届时警报将响起。我可以设置闹钟。但是报警页面不起作用。它在开始活动时停止。alertActivity.java 的代码是:

package com.example.eventmanager;

import java.io.File;
import com.example.alarmmanager.R;

import android.app.Activity;
import android.database.Cursor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AlertActivity extends Activity implements OnClickListener{

    String save;
    TextView ques;
    TextView eventname;
    Button bt;
    Ringtone r; 

     File dir = Environment.getExternalStorageDirectory();

     Bundle extras = getIntent().getExtras();

         String ename = extras.getString("ename");

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.alert_layout);


        Uri noti = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        r = RingtoneManager.getRingtone(getApplicationContext(), noti);
        r.play();



    }
public void DisplayQuestion(final Cursor c) {

        ques.setText(c.getString(1));
        eventname.setText(ename);

        bt.setOnClickListener(this);

}



    @Override
    public void onClick(View v) {



                            Toast.makeText(this, "Alerm Has been Disabled", Toast.LENGTH_SHORT).show();
                            r.stop();
                        //try {  

                            //Intent i = new Intent(this,MainActivity.class);
                            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            //startActivity(i);       


                        //}
                          //catch (Exception e) {}




    }

    }

这是主要活动的代码:

package com.example.eventmanager;

import java.util.Calendar;

import com.example.alarmmanager.R;



import android.app.Activity;
import android.content.Intent;
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;

    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.editText1);
    }


    public void onDateSelectedButtonClick(View v){

        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 ename = name.toString();
         Intent i = new Intent(getApplicationContext(), AlertActivity.class);
         i.putExtra("ename",ename);

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


    }

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


}

这是我的日志:

09-12 13:04:00.822: I/NotifyService(12836): onCreate()
09-12 13:04:00.827: I/LocalService(12836): Received start id 1: Intent { flg=0x4 cmp=com.example.alarmmanager/com.example.eventmanager.NotifyService (has extras) }
09-12 13:04:00.842: I/power(2300): *** acquire_dvfs_lock : lockType : 1  freq : 1000000 
09-12 13:04:00.842: D/PowerManagerService(2300): acquireDVFSLockLocked : type : DVFS_MIN_LIMIT  frequency : 1000000  uid : 1000  pid : 2300  tag : ActivityManager
09-12 13:04:00.847: W/ActivityManager(2300): mDVFSLock.acquire()
09-12 13:04:00.877: D/ApplicationPolicy(2300): isStatusBarNotificationAllowed: packageName = com.example.alarmmanager
09-12 13:04:00.877: D/STATUSBAR-NotificationService(2300): Noti Alert - mSystemReady:true, AlertEnabled:true
09-12 13:04:00.877: D/PhoneStatusBar(2625): addNotification score=0
09-12 13:04:00.897: D/WindowManager(2300): PhoneWindowManager: focusChangedLw
09-12 13:04:00.897: D/KeyguardViewMediator(2300): setHidden false
09-12 13:04:00.897: D/AndroidRuntime(12836): Shutting down VM
09-12 13:04:00.897: W/dalvikvm(12836): threadid=1: thread exiting with uncaught exception (group=0x410152a0)
09-12 13:04:00.897: D/WindowManager(2300): mInputFocus is not null.
09-12 13:04:00.897: D/WindowManager(2300): mInputFocus is not null.
09-12 13:04:00.897: D/WindowManager(2300): mInputFocus is not null.
09-12 13:04:00.902: E/AndroidRuntime(12836): FATAL EXCEPTION: main
09-12 13:04:00.902: E/AndroidRuntime(12836): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.alarmmanager/com.example.eventmanager.AlertActivity}: java.lang.NullPointerException
09-12 13:04:00.902: E/AndroidRuntime(12836):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at android.os.Looper.loop(Looper.java:137)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at android.app.ActivityThread.main(ActivityThread.java:4898)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at java.lang.reflect.Method.invokeNative(Native Method)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at java.lang.reflect.Method.invoke(Method.java:511)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at dalvik.system.NativeStart.main(Native Method)
09-12 13:04:00.902: E/AndroidRuntime(12836): Caused by: java.lang.NullPointerException
09-12 13:04:00.902: E/AndroidRuntime(12836):    at com.example.eventmanager.AlertActivity.<init>(AlertActivity.java:35)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at java.lang.Class.newInstanceImpl(Native Method)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at java.lang.Class.newInstance(Class.java:1319)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
09-12 13:04:00.902: E/AndroidRuntime(12836):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
09-12 13:04:00.902: E/AndroidRuntime(12836):    ... 11 more
09-12 13:04:00.902: D/StatusBar(2625): expanding top notification at 3entry.userCollapsed() = false
09-12 13:04:00.912: D/WindowManager(2300): PhoneWindowManager: focusChangedLw
09-12 13:04:00.912: E/android.os.Debug(2300): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
09-12 13:04:00.912: D/WindowManager(2300): mInputFocus is not null.
09-12 13:04:00.917: I/dumpstate(13518): begin
09-12 13:04:01.117: I/InputReader(2300): Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.460 ]
09-12 13:04:01.152: I/InputReader(2300): Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=]
09-12 13:04:01.322: D/KeyguardViewMediator(2300): setHidden false
09-12 13:04:01.322: I/SurfaceFlinger(1908): id=334 Removed Uoast idx=6 MapSz=4
09-12 13:04:01.322: I/SurfaceFlinger(1908): id=334 Removed Uoast idx=-2 MapSz=4
09-12 13:04:01.322: D/KeyguardViewMediator(2300): setHidden false
09-12 13:04:01.322: D/PowerManagerService(2300): releaseWakeLockLocked flags=0x0 tag=KEEP_SCREEN_ON_FLAG myUID=1000 myPID=2300 myTID=2524
09-12 13:04:01.327: D/KeyguardViewMediator(2300): setHidden false
4

1 回答 1

0

错误是第 35 行的 NullPointerException。代码中的第 35 行没有任何内容。可能是因为这条线

Bundle extras = getIntent().getExtras();

     String ename = extras.getString("ename");

您可以使用此解决它

Bundle extras = getIntent().getExtras();
String ename;
if(extras == null){
    ename = "some default name";
}
else{
    ename = extras.getString("ename");
}
于 2013-09-12T10:28:24.747 回答