Can you put the Countdowntimer in a Fragment?

I am new to android app development. I was doing okay until I decided to add a snippet that would display the Countdowntimer. I looked at Stackoverflow but none of the solutions worked for my scenario. Here are the problems I'm having:

1. When the application starts, the Fragment will not display the text from the requested Textview [R.id.Time_view]; (i think the problem is how i got the fragment view back, i used layoutinflater instead of setcontentview i suppose?)

2. The code does not work and causes the device to fail; (I believe I messed up the below class and methods)

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

public class Timer_java extends Fragment{

/*Can inflating the Fragment layout, 
 instead of using setcontentview, cause these problems?*/

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

    View v;
    v = inflater.inflate(R.layout.timer_fragment, container, false);
return v;
}

@Override
        public void onStart(){
    TimerActivity counter = new TimerActivity(30000,1000);
    counter.start();

}
TextView Time_view = (TextView)getView() .findViewById(R.id.Time_view) ;
    TextView done;

    public class TimerActivity extends CountDownTimer {
        public TimerActivity (long startTime, long interval) {
           super(startTime, interval);
        }

         @Override
        public void onTick(long millisUntilFinished) {

            Time_view.setText("Time left:" + millisUntilFinished / 1000);
         }

        @Override
            public void onFinish() {
                done.setText("Done");

    }
  }
}

      

Xml layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#ffdcddd8"
android:gravity="center">
<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="@string/timer"
    android:gravity="center"
    android:textStyle="bold"
    android:textSize="16sp"
    android:id="@+id/Time_view"/>

<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="@string/score"
    android:textStyle="bold"
    android:textSize="16sp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="85dp"
    android:layout_marginEnd="85dp" />


 </RelativeLayout>

      

+3


source to share


1 answer


onStart calls before onCreateView . your view hasn't been initialized yet.



This means that yours is TextView

also not initiailzed

. and crashes . your TextViews are null and you are setting the text to null Views.

0


source







All Articles