Android警报解雇导致崩溃

时间:2015-06-02 13:50:03

标签: android alarm

我想要做的是在检测到笑容时解除警报。首先当闹铃响起时,它会转到这个类AlarmScreen.java。然后,用户将单击微笑按钮以打开第二个活动,即MainActivity.java,它将打开相机活动。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    Intent alarmIntent = new Intent(AlarmScreen.this, AlarmManagerHelper.class);
    pendingIntent = PendingIntent.getBroadcast(AlarmScreen.this, 0, alarmIntent, 0);


    //Setup layout
    this.setContentView(R.layout.activity_alarm_screen);

    String name = getIntent().getStringExtra(AlarmManagerHelper.NAME);
    int timeHour = getIntent().getIntExtra(AlarmManagerHelper.TIME_HOUR, 0);
    int timeMinute = getIntent().getIntExtra(AlarmManagerHelper.TIME_MINUTE, 0);
    String tone = getIntent().getStringExtra(AlarmManagerHelper.TONE);

    TextView tvName = (TextView) findViewById(R.id.alarm_screen_name);
    tvName.setText(name);

    TextView tvTime = (TextView) findViewById(R.id.alarm_screen_time);
    tvTime.setText(String.format("%02d : %02d", timeHour, timeMinute));

    Button dismissButton = (Button) findViewById(R.id.alarm_screen_button);
    dismissButton.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View view) {
            mPlayer.stop();
            finish();
        }
    });

    Button camera = (Button) findViewById(R.id.camera);
    camera.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {


            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    });


public void stopAlarm() {

    Button dismissButton = (Button) findViewById(R.id.alarm_screen_button);
    dismissButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            mPlayer.stop();
            finish();
        }
    });
}

    mPlayer = new MediaPlayer();
    try {
        if (tone != null && !tone.equals("")) {
            Uri toneUri = Uri.parse(tone);
            if (toneUri != null) {
                mPlayer.setDataSource(this, toneUri);
                mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                mPlayer.setLooping(true);
                mPlayer.prepare();
                mPlayer.start();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

这是MainActivity.java类,其中所有检测都在此处进行。

public void onPreviewFrame(byte[] data, Camera camera) {
    int surfaceWidth = mPreview.getWidth();
    int surfaceHeight = mPreview.getHeight();
    faceProc.normalizeCoordinates(surfaceWidth, surfaceHeight);
    numFaces = faceProc.getNumFaces();
    int dRotation = display.getRotation();
    PREVIEW_ROTATION_ANGLE angleEnum = PREVIEW_ROTATION_ANGLE.ROT_0;

    if (numFaces > 0) {
        System.out.println("face detected");
        Log.d("TAG", "Face Detected");
        faceArray = faceProc.getFaceData();
        preview.removeView(drawView);
        drawView = new DrawView(this, faceArray, true);
        preview.addView(drawView);

    } else {
        preview.removeView(drawView);
        drawView = new DrawView(this, null, false);
        preview.addView(drawView);

    }

在这个类上,它将在嘴部区域绘制圆圈。在圆圈变为绿色后,它将调用另一个活动,即Dismiss.java类。

public class DrawView extends SurfaceView {
    public FaceData []mFaceArray;
    boolean _inFrame;
    private Paint mouthBrush = new Paint();
    private Paint rectBrush = new Paint();
    public Point mouth;
    Rect mFaceRect;
    AlarmService A;
    AlarmScreen B;
    Paint paint = new Paint();
}

public DrawView(Context context,FaceData []faceArray, boolean inFrame){
    super(context);
    setWillNotDraw(false);
    mFaceArray = faceArray;
    _inFrame = inFrame;
}

protected void onDraw(Canvas canvas) {
    if(_inFrame){
        for(int i = 0; i<mFaceArray.length; i++){
             setCircleColor(mFaceArray[i], mouthBrush);
             canvas.drawCircle(mFaceArray[i].mouth.x, mFaceArray[i].mouth.y, 20f, mouthBrush);  
        }
    }else{
        canvas.drawColor(0, Mode.CLEAR);
    }
}

private void setCircleColor(FaceData faceData, Paint rectBrush) {
    if(faceData.getSmileValue()<80){
        mouthBrush.setColor(Color.RED);
    }else{
        mouthBrush.setColor(Color.GREEN);
        startActivity(getContext());
    }
}

public void startActivity(Context mContext){
    Intent intent = new Intent(mContext,Dismiss.class);   
    mContext.startActivity(intent);  
}

这是我应该关闭警报的活动类,通过从AlarmScreen.java类调用stopAlarm()方法,警报被解除,不幸的是应用程序在这里停止。我是这个领域的新手,我不太清楚错误发生的原因。有人能帮助我吗?

public class Dismiss extends Activity {

    public static Activity AlarmScreen;
    private MediaPlayer mPlayer;

    AlarmService A;
    AlarmScreen B;
    AlarmRing C;

    @Override
    Protected void onCreate (final Bundle savedInstanceState)
    {

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

        Button dismissButton = (Button) findViewById(R.id.dismiss);
        dismissButton.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View view) {
                B.stopAlarm();
                finish();
            }
        });
    }
}

我不知道如何显示logcat,因为我还是新手,但现在是。

06-03 19:40:10.400: E/AndroidRuntime(31323): FATAL EXCEPTION: main
06-03 19:40:10.400: E/AndroidRuntime(31323): Process: com.trigg.alarmclock, PID: 31323
06-03 19:40:10.400: E/AndroidRuntime(31323): java.lang.NullPointerException
06-03 19:40:10.400: E/AndroidRuntime(31323):    at com.smileclock.Dismiss$1.onClick(Dismiss.java:61)
06-03 19:40:10.400: E/AndroidRuntime(31323):    at android.view.View.performClick(View.java:4569)
06-03 19:40:10.400: E/AndroidRuntime(31323):    at android.view.View$PerformClick.run(View.java:18553)
06-03 19:40:10.400: E/AndroidRuntime(31323):    at android.os.Handler.handleCallback(Handler.java:733)
06-03 19:40:10.400: E/AndroidRuntime(31323):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-03 19:40:10.400: E/AndroidRuntime(31323):    at android.os.Looper.loop(Looper.java:212)
06-03 19:40:10.400: E/AndroidRuntime(31323):    at android.app.ActivityThread.main(ActivityThread.java:5135)
06-03 19:40:10.400: E/AndroidRuntime(31323):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 19:40:10.400: E/AndroidRuntime(31323):    at java.lang.reflect.Method.invoke(Method.java:515)
06-03 19:40:10.400: E/AndroidRuntime(31323):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
06-03 19:40:10.400: E/AndroidRuntime(31323):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
06-03 19:40:10.400: E/AndroidRuntime(31323):    at dalvik.system.NativeStart.main(Native Method)
06-03 19:40:11.750: I/Process(31323): Sending signal. PID: 31323 SIG: 9
06-03 19:40:11.860: E/MediaPlayer-JNI(32419): QCMediaPlayer mediaplayer NOT present
06-03 19:40:11.900: D/AndroidRuntime(32419): Shutting down VM
06-03 19:40:11.900: W/dalvikvm(32419): threadid=1: thread exiting with uncaught exception (group=0x41613d88)
06-03 19:40:11.900: E/AndroidRuntime(32419): FATAL EXCEPTION: main
06-03 19:40:11.900: E/AndroidRuntime(32419): Process: com.trigg.alarmclock, PID: 32419
06-03 19:40:11.900: E/AndroidRuntime(32419): java.lang.RuntimeException: Unable to start service com.smileclock.AlarmService@447f7488 with null: java.lang.NullPointerException
06-03 19:40:11.900: E/AndroidRuntime(32419):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2767)
06-03 19:40:11.900: E/AndroidRuntime(32419):    at android.app.ActivityThread.access$2100(ActivityThread.java:144)
06-03 19:40:11.900: E/AndroidRuntime(32419):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1343)
06-03 19:40:11.900: E/AndroidRuntime(32419):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 19:40:11.900: E/AndroidRuntime(32419):    at android.os.Looper.loop(Looper.java:212)
06-03 19:40:11.900: E/AndroidRuntime(32419):    at android.app.ActivityThread.main(ActivityThread.java:5135)
06-03 19:40:11.900: E/AndroidRuntime(32419):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 19:40:11.900: E/AndroidRuntime(32419):    at java.lang.reflect.Method.invoke(Method.java:515)
06-03 19:40:11.900: E/AndroidRuntime(32419):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
06-03 19:40:11.900: E/AndroidRuntime(32419):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
06-03 19:40:11.900: E/AndroidRuntime(32419):    at dalvik.system.NativeStart.main(Native Method)
06-03 19:40:11.900: E/AndroidRuntime(32419): Caused by: java.lang.NullPointerException
06-03 19:40:11.900: E/AndroidRuntime(32419):    at android.content.Intent.putExtras(Intent.java:6134)
06-03 19:40:11.900: E/AndroidRuntime(32419):    at com.smileclock.AlarmService.onStartCommand(AlarmService.java:21)
06-03 19:40:11.900: E/AndroidRuntime(32419):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2750)

1 个答案:

答案 0 :(得分:0)

从您发布的代码中,以下行将抛出空指针异常,因为您的代码似乎没有初始化B

B.stopAlarm();

如果要在AlarmScreen类上调用此方法,则必须初始化B以引用该类型的对象。您可以将AlarmScreen对象传递给类Dismiss

的构造函数

或者,您可以从B.stopAlarm()类中取出对Dismiss的调用,并从父活动中调用它,该活动可能具有对AlarmScreen的有效引用。