Skip to content Skip to sidebar Skip to footer

Textview Is Null On First Click But Updates On Second Click

It's small Android 2.2 test application using the Compatibility Package. I am trying to update the textview on another fragment on another activity on list item selection. But prob

Solution 1:

Instead of doing details.setTextView(index) in DetailActivity , set the value of TextView in the DetailFragment's onActivityCreated while passing the value to be set in fragments setArgument method in the Detailactivity...

DetailsFragmentdetails=newDetailsFragment();
  details.setArguments(getIntent().getExtras());  // pass the value of text view here

And In fragment onActivityCreated get that value via getArguments() and set it in textview..

EDIT for sending value Selected to loaded Fragment

In List Activity

  detailFragment = getFragmentbyTag
  if(detailFragment == null)
      Create Fragment and Add it and Set Arguments here as well
  else
      detailFragment.setTextView(value); // fragment already loaded no need to set arguments

if you want to replace each time and not add once and use the added/loaded fragment , use setarguments each time.... and remove previous fragment and add new fragment( with Arguments) But added once and reused is preferred instead of removing and adding/ replacing on each click

Solution 2:

Updated2

Just initialize your TextView in onCreateView() no need to create DetailFragment with parametrize just put in comment

Check the details object whether its null or not if null initialize as you do and then called setTextView()

if(details==null)
    details = new DetailFragment(index);
else
   setTextView(index);

TextView inside onCreateView() and used their

publicclassDetailFragmentextendsFragment {

    String[] capitals;

    int index;
    private TextView textView;

    publicDetailFragment(int index){
        this.index = index;
    }

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        if (container == null)
            returnnull;

        capitals = newString[] { "delhi", "karachi", "colombo", "beijing",
                "dhaka", "katmandu", "Afghanistan", "pyongyang", "seoul",
                "tokyo" };

        Viewv= inflater.inflate(R.layout.detail_fragment, container, false);
        // Initialize textView
        textView = (TextView) v.findViewById(R.id.detailView);
        setTextView(index); // set value value here for first timereturn v;
    }

    publicvoidsetTextView(int index) {
        if(textView!=null)
            textView.setText(capitals[index]); 
    }

    publicintgetShownIndex() {
        return getArguments().getInt("index", 0);
    }
}

Solution 3:

did you tried using this

TextViewview= (TextView) findViewById(R.id.detailView);

instead of this

TextViewview= (TextView) getView().findViewById(R.id.detailView);

Post a Comment for "Textview Is Null On First Click But Updates On Second Click"