-3

I have created a tabbed activity and wish that one of the Tabs will display the current time and date. I have already linked the fragments together however having some issues with displaying the time and date.

This is the error:

Error:(34, 61) error: cannot find symbol method findViewById(int)

and also:

Error:(31, 25) error: cannot find symbol method runOnUiThread()

This is the code;

package com.example.user.plannerv2;

import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class sub_page01 extends Fragment {
    private static final String TAG = "Sub_page01";

    // private Button btnTEST;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sub_page01,container,false);

        Thread t = new Thread() {
            public void run() {
                try {
                    while (!isInterrupted()) {
                        Thread.sleep(1000);
                        runOnUiThread(new Runnable() {
                            @RequiresApi(api = Build.VERSION_CODES.N)
                            public void run() {
                                TextView tdate = (TextView) findViewById(R.id.date);
                                long date = System.currentTimeMillis();
                                SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy\nhh-mm-ss a");
                                String dateString = sdf.format(date);
                                tdate.setText(dateString);
                            }
                        });
                    }
                } catch (InterruptedException e) {
                }
            }
        };
        t.start();



        return view;
    }

Any help would be grateful.

Renats Stozkovs
  • 2,549
  • 10
  • 22
  • 26
Fuj11
  • 1
  • 1

2 Answers2

0

You are inside a fragment, you should use view.findViewById( ) instead.

Replace: TextView tdate = (TextView) findViewById(R.id.date);

To: TextView tdate = (TextView) view.findViewById(R.id.date);

Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42
  • Thank you, that fixes the issue regarding view.`findViewById()` however still the problem of `Error:(31, 25) error: cannot find symbol method runOnUiThread()` stands. – Fuj11 Apr 07 '17 at 18:48
  • in addition, `runOnUiThread` is an Activity method. – C0D3LIC1OU5 Apr 07 '17 at 18:48
  • you can go `getActivity().ruinOnUiThread(...` – C0D3LIC1OU5 Apr 07 '17 at 18:49
  • like @C0D3LIC1OU5 said, `runOnUiThread( )` is an Activity method, you can`t call it in your Fragment. use your Activity instead like the comment above. – Luiz Fernando Salvaterra Apr 07 '17 at 18:50
  • @LuizFernandoSalvaterra I'm not quite sure I understand what you mean. – Fuj11 Apr 07 '17 at 19:00
  • 1
    you are calling `runOnUiThread()` method, this method belongs to `Activity` class, your `Fragment` is not an `Activity`, so you cannot do that, but you can get your `Activity` inside your `Fragment` calling the `getActivity().runOnUiThread( )` – Luiz Fernando Salvaterra Apr 07 '17 at 19:04
0

For

Error:(34, 61) error: cannot find symbol method findViewById(int)

In Fragment,we have to initialize our view like this,

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_sub_page01,container,false);

    TextView tdate = (TextView) view.findViewById(R.id.date);
return view;

For

Error:(31, 25) error: cannot find symbol method runOnUiThread()

runOnUiThread() is Activity method so.In Fragment,Always put like this,

 getActivity().runOnUiThread(new Runnable() {
                            @RequiresApi(api = Build.VERSION_CODES.N)
                            public void run() {
                                TextView tdate = (TextView) view.findViewById(R.id.date);
                                long date = System.currentTimeMillis();
                                SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy\nhh-mm-ss a");
                                String dateString = sdf.format(date);
                                tdate.setText(dateString);
                            }
                        });

It will fix the problem.

Braj Bhushan Singh
  • 607
  • 1
  • 10
  • 23