Capie
Capie

Reputation: 996

Activity crash (android fragments)

Im started today wth fragments on android(till now i only used activities) and I get a crash after running the app(actually it doesnt even launch).

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment {


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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        init();//----> PROBLEM HERE
        return inflater.inflate(R.layout.fragment_main, container, false);

    }


    public TextView login;
    /** Called when the user clicks link for Login/Register on right corner of the app*/
    public void init() {
        login=(TextView) getView().findViewById(R.id.textView11_linkRegister);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //intent means what i want to do'
                Intent openLoginForm = new Intent(getActivity(),Login.class);
                startActivity(openLoginForm);
            }
        });
    }


}

I made the Init() function andeverything worked good till i tried to call it on onCreateView() I know the problem must be the way I call that function. I would appreciate any help.

THanks.

Upvotes: 1

Views: 109

Answers (3)

Chip Thien
Chip Thien

Reputation: 81

To explain the code that other answers have written, you cannot call getView() before onCreateView() has completed. As you can see in their code suggestions, you must findViewById() on a view that you have inflated (which you will return). When you return this inflated view, it will then be available in later lifecycle methods by calling getView().

Upvotes: 0

Ognian Gloushkov
Ognian Gloushkov

Reputation: 2659

The view is not yet set when you call the init() method. Inflate the view first then either pass it to the init() or directly get the view in the onCreateView. Return the inflated view at the end of the method.

View view = inflater.inflate(R.layout.fragment_main, container, false);
login= (TextView) view.findViewById(R.id.textView11_linkRegister);
login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //intent means what i want to do'
            Intent openLoginForm = new Intent(getActivity(),Login.class);
            startActivity(openLoginForm);
        }
    });

return view;

Upvotes: 0

Stefan
Stefan

Reputation: 2178

You can't access a view that does not yet exist, rewrite your code like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_main,container,false);
    init(v);//----> PROBLEM HERE
    return v;

}


public TextView login;
/** Called when the user clicks link for Login/Register on right corner of the app*/
public void init(View v) {
    login=(TextView) v.findViewById(R.id.textView11_linkRegister);
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //intent means what i want to do'
            Intent openLoginForm = new Intent(getActivity(),Login.class);
            startActivity(openLoginForm);
        }
    });
}

Upvotes: 1

Related Questions