0

First of I tried the tips in the example here (getActivity) but it doesn't work

TextView text = (TextView)
getActivity().findViewById(R.nrOfBooksInCollection);//This results in nullpointer exception
text.setText("Text from a fragment");//This results in nullpointer exception

And neiter does the below code work. I don't get an error in Eclipse using the below code, it just doesn't change the text in the textview "nrOfBooksInCollection".

package com.ahmad.actionBar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TextView;

public class FragMent2 extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.summary, null);
    SimpleBookManager testBook = new SimpleBookManager();
    TextView nrOfBooksfield = (TextView) view.findViewById(R.id.nrOfBooksInCollection);
        String text = Integer.toString(testBook.count());
        nrOfBooksfield.setText(text);//The text doesn't change at all
        nrOfBooksfield.setText("text");//Neither does this
        return inflater.inflate(R.layout.summary, container, false);
    }
    }

It worked when I was using Activities so the XML file is fine.

Community
  • 1
  • 1
anders
  • 1,535
  • 5
  • 19
  • 43

1 Answers1

1

You are inflating your view outside your ViewGroup. Try:

View view = inflater.inflate(R.layout.summary, container, null);
...
return view;
Axel
  • 685
  • 5
  • 14