2017-10-23 5 views
0

このタイマーはなぜ実行されないのですか?私はそれを断片に設定して、それは私のrunOnUiThreadと関係があると感じているが、わからない。また、私は何かエラーや何もそれを実行するつもりを取得していないことを追加する必要があります。なぜこのタイマーは自分のフラグメント内で実行されませんか?

final WeakReference<Activity> activityRef = new WeakReference<Activity>((getActivity())); 
    //creating timer 
    public void startRandomButton(final ImageView img) 
    { 
     Timer timer = new Timer(); 
     timer.schedule(new TimerTask() 
    { 
     @Override 
     public void run() 
     { 
      if(activityRef.get() == null) 
       return; 

       //activityRef.get().runOnUiThread(new Runnable() 
      if(isAdded()) 
      { 
       activityRef.get().runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Activity activity = activityRef.get(); 
         if (isAdded()) 
          randomMovement(img); 
         else 
          Log.d("not added", "not added"); 
        } 
       }); 
      } 
      else 
       Log.d("Run on ui thread", "is null"); 
     } 
    }, 5000); 
} 

以下は、タイマーを作成するときに問題が発生している部分全体です。私が最初の質問で言ったように、私は問題が何であるか分かりません。コードはコンパイルして実行します。しかし、タイマーの方法はうまく動作しません。私は私のログ・ネコに活動が破壊されたとき、またはあなたがこれを通って行くときに教えてもらうことができます。私はそれが何かがヌルでどこに誤りがないかを教えてください。助けが大変ありがとう!:

package com.example.name.inserttitlename; 


import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Point; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.support.annotation.NonNull; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.os.Handler; 
import android.view.InflateException; 

import java.lang.ref.WeakReference; 
import java.util.Random; 
import java.util.Timer; 
import java.util.TimerTask; 
import java.util.logging.Level; 

import static android.R.attr.x; 


/** 
* A simple {@link Fragment} subclass. 
*/ 
public class LevelOneFragment extends Fragment { 

    //creating variables 
    private ImageView img; 
    private ViewGroup viewer; 
    TextView textCount; 
    Button failButton; 
    Button beginButton; 
    TextView countDownBeginText; 
    private TextView countDownMain; 
    public int scoreCounter; 

    public LevelOneFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onAttach(Context context) 
    { 
     super.onAttach(context); 
     Activity a; 

     if(context instanceof Activity) 
     { 
      a=(Activity) context; 
     } 

    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
    } 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 



     View view = inflater.inflate(R.layout.fragment_level_one, container, false); 

     //creating viewables and such 
     viewer = (ViewGroup) view.findViewById(R.id.view_level); 
     img = (ImageView) view.findViewById(R.id.imageView); 
     failButton = (Button) view.findViewById(R.id.failureButton); 
     countDownMain = (TextView) view.findViewById(R.id.countDownMain); 
     countDownBeginText = (TextView) view.findViewById(R.id.countDownBeginText); 
     beginButton = (Button) view.findViewById(R.id.beginButton); 


     RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150); 
     img.setLayoutParams(layoutParams); 
     img.setOnTouchListener(new LevelOneFragment.ChoiceTouchListener()); 

     //begin random movement 

      startRandomButton(img); 


     //setting up button for failure CORRECT TO TAKE YOU TO THE MAIN MENU 
     failButton.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View v){ 
       startActivity(new Intent(getActivity(), MainMenu.class)); 
      } 
     }); 


     //setting countdown text to 3 
     countDownBeginText.setText(":3"); 

     //begin countdown from three 
     beginButton.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View beginT) { 
       CountDownTimer countDownTimer = new CountDownTimer(3*1000, 3) { 
        @Override 
        public void onTick(long millisUntilFinished) { 

         countDownBeginText.setText(":" + millisUntilFinished/1000); 
         //add possible sound here every tick 
        } 

        @Override 
        public void onFinish() { 
         beginButton.setVisibility(View.INVISIBLE); 
         countDownBeginText.setVisibility(View.INVISIBLE); 

         //countdown from 60------- change parameters in count down timer to the time desired 
         CountDownTimer countDownTimer = new CountDownTimer(60*1000, 3) { 
          @Override 
          public void onTick(long millisUntilFinishedMain) { 

           countDownMain.setText(":" + millisUntilFinishedMain/1000); 
           //add possible sound here every tick 
          } 

          @Override 
          public void onFinish() { 
           //Toast.makeText(getActivity(),"You lose", Toast.LENGTH_LONG).show(); 
           //rest of text in method is trial 

           startActivity(new Intent(getActivity(), MainMenu.class)); 

          } 
         }.start(); 

        } 
       }.start(); 
      } 
     }); 

     return view; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstatnceCState) 
    { 
     super.onActivityCreated(savedInstatnceCState); 
    } 

    //getting screen size 
    public static Point getDisplaySize(@NonNull Context context){ 

     Point point = new Point(); 
     WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
     manager.getDefaultDisplay().getSize(point); 
     return point; 
    } 

    //randomly move 

    public void randomMovement(ImageView img){ 

      int x = new Random().nextInt(getDisplaySize(getActivity()).x); 
      int y = new Random().nextInt(getDisplaySize(getActivity()).y); 
      img.setY(y); 
      img.setX(x); 


    } 


    final WeakReference<Activity> activityRef = new WeakReference<Activity>((getActivity())); 
    //creating timer 
    public void startRandomButton(final ImageView img) 
    { 
     Timer timer = new Timer(); 
     timer.schedule(new TimerTask() 
     { 
      @Override 
      public void run() 
      { 
       if(activityRef.get() == null) 
        return; 

        //activityRef.get().runOnUiThread(new Runnable() 
       if(isAdded()) 
       { 
        activityRef.get().runOnUiThread(new Runnable() 
        { 
         @Override 
         public void run() 
         { 
          Activity activity = activityRef.get(); 

          if (isAdded()) 
           randomMovement(img); 
          else 
           Log.d("not added", "not added"); 
         } 
        }); 
       } 
       else 
        Log.d("Run on ui thread", "is null"); 
      } 
     }, 5000); 
    } 

    //listener for the moving ball 
    private final class ChoiceTouchListener implements View.OnTouchListener { 
     public boolean onTouch(View view, MotionEvent event){ 

      //Calling Counter 
      textCount = (TextView) getView().findViewById(R.id.textViewCount); 



      //switch statement for different events 
      switch (event.getAction() & MotionEvent.ACTION_MASK){ 

       case MotionEvent.ACTION_DOWN: 
        break; 

       case MotionEvent.ACTION_UP: 
        //this is where youre going to generate randmom location with id.setX(randomX) and id.setY(randomY) 
        randomMovement(img); 

        //Setting Counter to count when img is clicked 
        String countValue = textCount.getText().toString(); 
        scoreCounter = Integer.parseInt(countValue); 
        scoreCounter++; 
        textCount.setText(String.valueOf(scoreCounter)); 
        break; 


       case MotionEvent.ACTION_POINTER_DOWN: 
        break; 

       case MotionEvent.ACTION_POINTER_UP: 
        break; 


      } 



      //checking score 
      if(scoreCounter == 10) 
      { 
       Toast.makeText(getActivity(),"You win", Toast.LENGTH_LONG).show(); 

      } 

      viewer.invalidate(); 
      return true; 
     } 
    } 
} 

答えて

0

残りのクラスでどのようなコードを使用しているのかは少しはっきりしません。だからここフラグメントのコードです:

public class someFragmentClass extends Fragment { 

    Timer mTimer; 
    View mView; 

    TimerTask timerTask = new TimerTask(){ 
     @Override 
     public void run() { 
      getActivity().runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
         randomMovement(img); 
        } 
       } 
      }); 
     } 

    }; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

     mView = inflater.inflate(R.layout.someFragment, container, false); 

     mTimer = new Timer(); 
     mTimer.scheduleAtFixedRate(timerTask, 0 , 5000); 

     return mView; 
    } 

} 

微妙なミスがあるかもしれないので、私は、コードをテストしていません。

+0

私がしたい場合は、全体の断片を投稿することができます。 – Cantum2

+0

@ Cantum2はい、どうぞ。さらに、提供されたコードを使用してみてください –

+0

私はちょうど完全な断片をアップロードしました。あなたがそれを上回っている間、私はあなたのバージョンを使用し、それがどのようになるかを見ます。 – Cantum2

関連する問題