2016-09-30 84 views
0

我正在測試應用程序有五個片段,每個片段都有一個問題。片段移動有兩種方式。應用程序停止使用倒數計時器

  1. 當用戶點擊一個選項時,下一個片段立即出現。
  2. 我有一個倒數計時器,如果用戶沒有點擊任何選項,然後10秒後片段被移動到新的片段。

我已經實現了點擊功能。當用戶點擊選項應用程序將其帶到下一個片段,但如果在下一個片段中用戶不點擊任何選項,則應用程序立即停止。 請看看這個,我分享代碼。

Question.java(片段1)

package com.example.sheikhspc.testapp; 


import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.drawable.ShapeDrawable; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 


public class Question1 extends Fragment implements View.OnClickListener { 


public Question1() { 
    // Required empty public constructor 
} 
int counter = 0; 
View view; 
Fragment frag = null; 
TextView tv; 
ImageView imageView1, imageViewR, imageView2; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    view = inflater.inflate(R.layout.fragment_question1, container, false); 
    imageView1 = (ImageView) view.findViewById(R.id.wrong1); 
    imageViewR = (ImageView) view.findViewById(R.id.right); 
    imageView2 = (ImageView) view.findViewById(R.id.wrong2); 
    tv = (TextView) view.findViewById(R.id.timer1); 

    imageView1.setOnClickListener(this); 
    imageViewR.setOnClickListener(this); 
    imageView2.setOnClickListener(this); 
    new CountDownTimer(10000, 1000) { 

     public void onTick(long millisUntilFinished) { 
      tv.setText("Remaining Time: " + millisUntilFinished/1000); 
      //here you can have your logic to set text to edittext 
     } 

     public void onFinish() { 
      frag = new Question2(); 
      FragmentManager fmm = getFragmentManager(); 
      FragmentTransaction fragmentTransactionn = fmm.beginTransaction(); 
      fragmentTransactionn.addToBackStack(null); 

      fragmentTransactionn.replace(R.id.container, frag); 

      fragmentTransactionn.commit(); 

     } 


    }.start(); 


    return view; 
} 


@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.wrong1: 
      frag = new Question2(); 
      FragmentManager fm = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

      fragmentTransaction.replace(R.id.container,frag); 

      fragmentTransaction.commit(); 

      break; 
     case R.id.right: 
      frag = new Question2(); 
      FragmentManager fm1 = getFragmentManager(); 
      FragmentTransaction fragmentTransaction1 = fm1.beginTransaction(); 

      fragmentTransaction1.replace(R.id.container,frag); 

      fragmentTransaction1.commit(); 





      break; 
     case R.id.wrong2: 
      frag = new Question2(); 
      FragmentManager fm2 = getFragmentManager(); 
      FragmentTransaction fragmentTransaction2 = fm2.beginTransaction(); 

      fragmentTransaction2.replace(R.id.container,frag); 

      fragmentTransaction2.commit(); 

      break; 
     default: 
      new CountDownTimer(10000, 1000) { 

       public void onTick(long millisUntilFinished) { 
        tv.setText("Remaining Time: " + millisUntilFinished/1000); 
        //here you can have your logic to set text to edittext 
       } 

       public void onFinish() { 
        frag = new Question2(); 
        FragmentManager fmm = getFragmentManager(); 
        FragmentTransaction fragmentTransactionn = fmm.beginTransaction(); 
        fragmentTransactionn.addToBackStack(null); 

        fragmentTransactionn.replace(R.id.container, frag); 

        fragmentTransactionn.commit(); 

       } 


      }.start(); 
      break; 

    } 
} 
} 

Question2.java(Fragment2)

package com.example.sheikhspc.testapp; 


import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.app.Fragment; 
import android.os.CountDownTimer; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 


public class Question2 extends Fragment implements View.OnClickListener { 


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


Fragment frag = null; 
TextView tv; 
View view; 
ImageView imageView1, imageViewR, imageView2; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    view = inflater.inflate(R.layout.fragment_question2, container, false); 

    tv = (TextView)view.findViewById(R.id.timer2); 
    imageView1 = (ImageView) view.findViewById(R.id.wrong1); 
    imageViewR = (ImageView) view.findViewById(R.id.right); 
    imageView2 = (ImageView) view.findViewById(R.id.wrong2); 
    imageView1.setOnClickListener(this); 
    imageViewR.setOnClickListener(this); 
    imageView2.setOnClickListener(this); 
    new CountDownTimer(10000, 1000) { 

     public void onTick(long millisUntilFinished) { 
      tv.setText("seconds remaining: " + millisUntilFinished/1000); 
      //here you can have your logic to set text to edittext 
     } 

     public void onFinish() { 
      tv.setText("done!"); 
      frag = new Question3(); 
      FragmentManager fm = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

      fragmentTransaction.replace(R.id.container,frag); 

      fragmentTransaction.commit(); 

     } 

    }.start(); 


    return view; 
} 


@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.wrong1: 
      frag = new Question3(); 
      FragmentManager fm = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

      fragmentTransaction.replace(R.id.container,frag); 

      fragmentTransaction.commit(); 

      break; 
     case R.id.right: 
      frag = new Question3(); 
      FragmentManager fm1 = getFragmentManager(); 
      FragmentTransaction fragmentTransaction1 = fm1.beginTransaction(); 

      fragmentTransaction1.replace(R.id.container,frag); 

      fragmentTransaction1.commit(); 





      break; 
     case R.id.wrong2: 
      frag = new Question3(); 
      FragmentManager fm2 = getFragmentManager(); 
      FragmentTransaction fragmentTransaction2 = fm2.beginTransaction(); 

      fragmentTransaction2.replace(R.id.container,frag); 

      fragmentTransaction2.commit(); 

      break; 
     default: 
      new CountDownTimer(10000, 1000) { 

       public void onTick(long millisUntilFinished) { 
        tv.setText("seconds remaining: " + millisUntilFinished/1000); 
        //here you can have your logic to set text to edittext 
       } 

       public void onFinish() { 
        tv.setText("done!"); 
        frag = new Question3(); 
        FragmentManager fm = getFragmentManager(); 
        FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

        fragmentTransaction.replace(R.id.container,frag); 

        fragmentTransaction.commit(); 

       } 

      }.start(); 

      break; 

    } 
} 
} 

錯誤日誌

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.example.sheikhspc.testapp, PID: 32330 
       java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentTransaction android.app.FragmentManager.beginTransaction()' on a null object reference 
        at com.example.sheikhspc.testapp.Question1$1.onFinish(Question1.java:58) 
        at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5343) 
        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:905) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
I/Process: Sending signal. PID: 32330 SIG: 9 
Application terminated. 

Second.java(活動中顯示了所有的片段)

package com.example.sheikhspc.testapp; 

import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.app.Fragment; 
import android.os.CountDownTimer; 
import android.provider.DocumentsContract; 
import android.support.v7.app.AppCompatActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Second extends AppCompatActivity { 

Fragment frag; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_second); 

    FragmentManager fm = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

    frag = new Question1(); 
    fragmentTransaction.replace(R.id.container,frag); 

    fragmentTransaction.commit(); 

} 
} 
+0

明顯'getFragmentManager()'返回空的地方......我敢打賭,它監守活動不活了 – Selvin

+0

嘗試終止你的倒數計時器,一旦移動到另一個片段。 –

+0

問題出在你的活動課內。你能告訴我們嗎? –

回答

1

在點擊替換片段之前,您需要取消計時器。

當您在onCreate中創建CountDownTimer的新實例時,將其分配給一個變量,例如,

CountDownTimer timer = new CountDownTimer(10000, 1000) {...}; 
timer.start(); 

然後在onClick方法即,當用戶點擊任何選項停止例如定時器

@Override 
public void onClick(View v) { 
switch (v.getId()) { 
    case R.id.btnNext: 

     // Add this line to avoid crash 
     timer.cancel(); 

     frag = new FragmentQuestion2(); 
     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

     fragmentTransaction.replace(R.id.container,frag); 

     fragmentTransaction.commit(); 

     break; 
} 
+0

謝謝。 那爲我工作:) –