2016-10-24 1 views
0

J'ai une activité qui implémente chronomètre à l'intérieur de la notification dans ma notification il y a un bouton pour arrêter le chronomètre.L'événement de clic pour le bouton fonctionne avec succès mais quand j'appelle la méthode stopButtonClick() pour arrêter le chronomètre Je reçois une erreur.Impossible d'instancier l'objet chronomètre à l'intérieur Activité

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

    } 
} 

L'erreur dans le logcat est

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) 

J'ai créé objet de Chronometer, mais encore il donne cette error.Please aide-moi out.Thanks à l'avance !!

Répondre

0

Vous devez définir votre mise en page de contenu avec setContentView(R.layout.my_layout), my_layout étant une mise en page que vous avez créée. après cela, vous pouvez utiliser findViewById

+0

ouais je l'ai vérifié encore donner sa même erreur –

+0

puis-je voir ce que vous avez essayé jusqu'à présent? –

+0

au lieu d'utiliser setContentView() j'ai utilisé inflater.inflate (R.layout.widget, null); widget est la mise en page de la notification –