Partage
  • Partager sur Facebook
  • Partager sur Twitter

problème d'Intent dans une class importer

    29 novembre 2019 à 20:51:11

    bonjour,

    mon appli crash suite à la demande de l'Intent de ma class timerTime importer dans toute les vues de l'app.

    ma class timerTime est comme ceci:

    public class timerTime extends AppCompatActivity {
    
        /** timer */
        private static Timer timer;
        private static TimerTask timerTask;
        private final static Handler handler = new Handler();
    
        private static Context mCtx = null;
    
        public timerTime(Context ctxt){
            this.mCtx = ctxt;
        }
    
        public void startTimer(){
            Log.i("startTimer()","start 30000");
            timer = new Timer();
            loadTimer(/*theContext*/);
            timer.schedule(timerTask,30000);
        }
    
        public void startTimer(int time){
            Log.i("startTimer()","time => "+time);
            timer = new Timer();
            loadTimer();
            timer.schedule(timerTask,time);
        }
    
        public static void stopTimer(){
            if (timer != null) {
                Log.i("stopTimer()","stop");
                timer.cancel();
                timer = null;
    
            }
        }
        public void loadTimer(){
            timerTask = new TimerTask(){
                public void run(){
                    handler.post(new Runnable(){
                        @Override
                        public void run() {
                            stopTimer();
                            Intent i = new Intent(mCtx, MainActivity.class);
                            startActivity(i);
                        }
                    });
                }
            };
        }
    }

    et voici une des vues qui l'utilise

    je n'ai mis ici que ce qui est nécessaire pour l'exemple

    public class logon extends AppCompatActivity implements View.OnTouchListener, View.OnClickListener {
    
        private EditText et_login, et_mdp;
        /** timer */
        private timerTime timerTime = new timerTime(this);
    	
        private boolean update = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            hideNavigationBar();
            setContentView(R.layout.logon);
            hideNavigationBar();
    
            et_login = (EditText)findViewById(R.id.et_login);
            et_mdp = (EditText)findViewById(R.id.et_mdp);
    
            et_login.setOnTouchListener(this);
            et_mdp.setOnTouchListener(this);
    
            et_login.addTextChangedListener(verifForTimeLodin);
            et_mdp.addTextChangedListener(verifForTimeMdp);
        }
    	
        @Override
        public boolean onTouch(View v, MotionEvent event){
    
            if (v == et_login){
                log("et_login: " + stringValue(event));
            }
            if (v == et_mdp) {
                log("et_mdp: " + stringValue(event));
            }
            return false;
        }
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            log("Activity: " + stringValue(event));
            return true;
        }
        private String stringValue(MotionEvent event) {
    
            final int action = event.getAction();
    
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    timerTime.stopTimer();
                    timerTime.startTimer();
                    return "ACTION_DOWN";
                case MotionEvent.ACTION_MOVE:
                    return "ACTION_MOVE";
                case MotionEvent.ACTION_UP:
                    return "ACTION_UP";
                case MotionEvent.ACTION_CANCEL:
                    return "ACTION_CANCEL";
            }
    
            return "";
        }
    
        @Override
        protected void onResume() {
            timerTime.startTimer(10000);
            super.onResume();
        }
        TextWatcher verifForTimeLodin = new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            }
    
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            public void afterTextChanged(Editable s) {
                if (update){
                    update = false;
                    timerTime.startTimer();
                }else {
                    timerTime.stopTimer();
                    timerTime.startTimer();
                }
            }
        };
        TextWatcher verifForTimeMdp = new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            }
    
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            public void afterTextChanged(Editable s) {
                if (update){
                    update = false;
                    timerTime.startTimer();
                }else {
                    timerTime.stopTimer();
                    timerTime.startTimer();
                }
            }
        };
    }

    et dans le log quand il crash 

    java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
    tools.timerTime$1$1.run(timerTime.java:71)

    qui est :

    Intent i = new Intent(mCtx, MainActivity.class);

    une idée?




    .



    -
    Edité par juvamine 29 novembre 2019 à 20:51:51

    • Partager sur Facebook
    • Partager sur Twitter

    problème d'Intent dans une class importer

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown