0

I have an activity that implements chronometer inside the Notification in my notification there is a button to stop the chronometer.The click event for button is working successfully but when i call method stopButtonClick() to stop chronometer but i am getting error.

    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();
        }

    }
}

The error in the logcat is

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)

I have created object of Chronometer but still it gives this error.Please help me out.Thanks in advance!!

Ricardo Vieira
  • 1,738
  • 1
  • 18
  • 26
sai pavan
  • 1
  • 4

1 Answers1

0

You need to set your content layout with setContentView(R.layout.my_layout) which my_layout is a layout you created. after that you can use findViewById

Mohammad Zarei
  • 1,773
  • 14
  • 33