-1

I am trying to change the background image of my fragment after some seconds but I still have trouble with it! I used fragment because I used tab bar. the app will work correctly until 2.5s which is the time for changing background! and I am sure the problem is caused from the codes not application!

here are my codes :

public class LogInFragment extends android.support.v4.app.Fragment {

    LinearLayout login_layout;

    public LogInFragment() {

    }

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

        login_layout = (LinearLayout) getActivity().findViewById(R.id.linear1);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {

                change();
            }

        }, 2500);
    }

    private void change()
    {
        Random random = new Random();
        int image_change = random.nextInt(5 - 1) + 1;

        switch (image_change)
        {
            case 1:
                login_layout.setBackgroundResource(R.drawable.blue1);
                break;

            case 2:
                login_layout.setBackgroundResource(R.drawable.yellow1);
                break;

            case 3:
                login_layout.setBackgroundResource(R.drawable.red1);
                break;

            default:
                login_layout.setBackgroundResource(R.drawable.green1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_log_in, container, false);

    }

}

I would be happy if anyone can help me please!

Hamid
  • 13
  • 3
  • 1
    `the application would be closed after 2.5s` - do you mean that it crashes? – Vladyslav Matviienko Aug 21 '18 at 05:27
  • I would guess that `login_layout` is null. Try replacing `(LinearLayout) getActivity().findViewById(R.id.linear1);` with `(LinearLayout) getView().findViewById(R.id.linear1);` – Jan Stoltman Aug 21 '18 at 05:39
  • got worse ...! XD – Hamid Aug 21 '18 at 05:55
  • you have used a method of onAttach() for the instance of you activity . share you code (Activity and xml) because you have used a R.layout.fragment_log_in in fragment view are cover you activityUI. – Ashwani kumar Aug 21 '18 at 06:07
  • Looks like you're actually changing the background of a layout in the attached activity. Why use a Fragment at all? – Chris Aug 21 '18 at 06:25
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Vladyslav Matviienko Aug 21 '18 at 06:27
  • I used fragments bc I used tab bar – Hamid Aug 21 '18 at 07:05
  • remove the login_layout = (LinearLayout) getActivity().findViewById(R.id.linear1); from oncreate. Because in fragment we have to use inflate, so find the view in oncreateview instead. Try this in oncreateview: View rootView = inflater.inflate(R.layout.fragment_log_in, container, false); login_layout = (LinearLayout) rootView.findViewById(R.id.linear1); return rootView; – user3576118 Aug 21 '18 at 08:29
  • it worked ...! thank you so much @user3576118 – Hamid Aug 21 '18 at 09:40

1 Answers1

0

remove the login_layout = (LinearLayout) getActivity().findViewById(R.id.linear1); from oncreate. Because in fragment we have to use inflate, so find the view in oncreateview instead. Try this in oncreateview: View rootView = inflater.inflate(R.layout.fragment_log_in, container, false); login_layout = (LinearLayout) rootView.findViewById(R.id.linear1); return rootView

Answered By @user3576118 ...!

Hamid
  • 13
  • 3