Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
package com.example.afinal;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.widget.TextView;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by admin on 13/07/2017.
 */

public class Tab1 extends Fragment {
    TextView textView;
    TextView label1;
    String  strDate;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/dd/MM");

        Date day = new Date();
        strDate=sdf.format(new Date(day.getTime() - TimeUnit.DAYS.toMillis(1)));
        textView =(TextView)findViewById(R.id.textView);
        textView.setText(strDate);
        return inflater.inflate(R.layout.tab1,container,false);

        //  textView.setText(new Date().toString());
    }
}


What I have tried:

I tried using getView() method before findviewById(int).My app crashed after using that method.Am an beginner in android.Help me out Guys.
Posted
Updated 12-Jul-17 23:57pm

1 solution

Several things to understand here, first of all you are using findViewById, but in the Fragment derived class, secondly you are using it in the onCreateView() function. These are not allowed and you need to wait and call it from a later function (any other handler). A quick hack (and a bad practice) would be to think that you can just inflate a new view and capture the objects from there, just don't do that.

The best approach would be to call it any other function, such as onStart() function or any other function that gets executed when your View is shown to the user. It won't be a bad practice to show a "Loading..." to the user.

Java
public void onStart() {
    super.onStart(); // Call this here. 
    // Get it here.
    TextView textView = (TextView) getView().findViewById(R.id.textView);
}

Notice the fact that onViewCreated() function is never guaranteed to be called thus this function would be useful. For more, you should consider reading the Fragment[^] class reference to understand how functions get called — events get raised.

android - findViewById in Fragment - Stack Overflow[^]
 
Share this answer
 
v2
Comments
Bala Milka 13-Jul-17 6:06am    
Thanks for the quick response Ahmad.I tried the method u suggested.But it doesn't works well.Will you please suggest some other method ?
Afzaal Ahmad Zeeshan 13-Jul-17 6:20am    
What error do you get? Maybe the error can help us debug this problem of yours.
Bala Milka 13-Jul-17 6:24am    
"casting getView.findViewById(R.Id.textView) into TextView is reduntant" this is the error i got.
Afzaal Ahmad Zeeshan 13-Jul-17 6:28am    
That is merely a warning, you can just remove the cast and try again. Remove the (TextView) ... part and do this again.
Bala Milka 13-Jul-17 6:28am    
and also i got error on "onStart" also it shows me"overriding method should call super.onStart"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900