Question

i am working on an android project but i have a problem with fragment which is used in fragmentpageradapter which it's an adapter for a view pager

i have the following code and i am trying to show Writer information on each page (writer is a member of an arraylist)

it works fine on normal screen but when the phone mode changes to landscape the program crash caused by "null pointer exception" because the writer changes to NULL (why?)

private Writer writer;

public WriterForm()
{
}

public void setWriter(Writer writer)
{
    this.writer = writer;
}

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

    // Name
    TextView wName = (TextView) rootView.findViewById(R.id.writer_list_methods_writer_name);
    wName.setTypeface(DefaultActivity.font, Typeface.BOLD);
    wName.setText(" " + writer.getName());
}

so how i can solove this problem?

thanks in advance :))

Was it helpful?

Solution

the problem is that each time you chnage the orientation the fragment is recreated that is where the null pointer came from.

solution:

you could either prevent it to landscape add this to ur activity manifest android:screenOrientation="portrait"

or

if you want to live the fragment like the lifecyle of the activity you need to add the setRetainInstance(true)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top