无法在活动

时间:2016-10-24 17:18:08

标签: android chronometer

我有一个活动,在我的通知中通知里面有一个按钮来停止计时器。按钮的点击事件工作成功但是当我调用方法stopButtonClick()来停止计时器但我得到了错误。

    public class Chronometers extends Activity {

    private long timeWhenStopped = 0;
    private boolean stopClicked;
    private Chronometer chs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        chs=(Chronometer)this.findViewById(R.id.ch);
        NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.icas);           
        RemoteViews remoteViews = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.widget);
        remoteViews.setChronometer(R.id.ch, SystemClock.elapsedRealtime(), null, true);
        builder.setContent(remoteViews);

        Intent buttonsIntent = new Intent(this,StartupReceiver.class);
        buttonsIntent.putExtra("do_action","play");
        PendingIntent as21 = PendingIntent.getBroadcast(this, 0, buttonsIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.button3, as21);
        nm.notify(0, builder.build());
    }

    private void showElapsedTime()  {
        long elapsedMillis = SystemClock.elapsedRealtime() - chs.getBase();  
    }

    // the method for when we press the 'start' button
    public void startButtonClick() {
        int stoppedMilliseconds = 0;
        String chronoText = chs.getText().toString();
        String array[] = chronoText.split(":");
        if (array.length == 2) {
            stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
                    + Integer.parseInt(array[1]) * 1000;
        } else if (array.length == 3) {
            stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
                    + Integer.parseInt(array[1]) * 60 * 1000
                    + Integer.parseInt(array[2]) * 1000;
        }
        chs.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
        chs.start();
    }

    // the method for when we press the 'stop' button
    public void stopButtonClick() {
        if (!stopClicked) {
            chs.stop();
            showElapsedTime();
        }

    }

    public static class StartupReceiver extends BroadcastReceiver {

        Context ctx;

        @Override
        public void onReceive(Context ctx, Intent intent)  {
            // TODO Auto-generated method stub
            this.ctx = ctx;
            Toast.makeText(ctx,"mak pro cina",Toast.LENGTH_SHORT).show();
            Intent i = new Intent(ctx,NotifyActivityHandler.class);
            i.putExtra("do_action","play");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ctx.startActivity(i);
            System.out.println("controll will be here ");
        }
    }

    public static class NotifyActivityHandler extends Activity {
        String action;
        int k = 0;

        protected void onCreate( Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            Intent abs=getIntent();
            action = abs.getStringExtra("do_action");
            System.out.println(action);
            if (action.equals("play"))
            {
                System.out.println("patrpooooooooooooo");
                new Chronometers().stopButtonClick();
            }
            else
            {
                System.out.println("mahitreee");
            }
            Toast.makeText(this,"jasgh",Toast.LENGTH_SHORT).show();
        }

    }
}

logcat中的错误是

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.home.outdoor/com.example.home.outdoor.Chronometers$NotifyActivityHandler}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Chronometer.stop()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2326)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:147)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5264)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)

我已经创建了Chronometer的对象,但它仍然给出了这个错误。请帮帮我。谢谢!

1 个答案:

答案 0 :(得分:0)

您需要使用setContentView(R.layout.my_layout)设置内容布局,my_layout是您创建的布局。之后,您可以使用findViewById