anand
anand

Reputation: 1751

How to set the UI values in fragment

I have one mainactivity layout composing of fragment layout. Now I have created one fragment class and implementing asyncTask in the fragment class. Now on postExecute i am trying to set the values for the fragment view like trying to set text for TextView. But i am getting NullPointerException.

My code snippet is:

public class MainHandlerFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new MyTask().execute("xyzname");
    }   

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);
        return view;
    }
public class MyTask extends AsyncTask<String, Void, Organization> {
        @Override
        protected String doInBackground(String... urls) {
            // getting api calls
        }
        @Override
        protected void onPostExecute(String str) {
            loadData(str);
        }
    }
public void loadData(String str){
        getSupportFragmentManager().beginTransaction()
        ((TextView) findViewById(R.id.text_frag)).setText("name"); // at this line getting null pointer exception
    }
}
} 

main_layout.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

 <fragment
     android:id="@+id/home_fragment"
     android:name="com.stata.mobile.android.ui.MainHandlerFragment"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"/>

</FrameLayout>

home_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_top_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text_frag"/>
</RelativeLayout>

Upvotes: 3

Views: 1559

Answers (3)

Sepehr Behroozi
Sepehr Behroozi

Reputation: 1800

Define a TextView field to hold your UI TextView:

public class MainHandlerFragment extends Fragment {
private TextView mTextView;

Override the onViewCreated() and call the worker to execute in onViewCreated() method:

@override
public void onViewCreated(View view, Bundle savedInstanceState)
{
    new MyTask().execute("xyzname");
}

And in your onCreateView() add this line:

mTextView = (TextView) view.findViewById(R.id.text_frag);

Change your loadData(String str) like this:

public void loadData(String str){
    getSupportFragmentManager().beginTransaction()
    mTextView.setText("name");
}

Upvotes: 2

localhost
localhost

Reputation: 5598

Use global variable for a TextView. Get reference to it in onCreateView and call setText on that reference.

public class MainHandlerFragment extends Fragment {
    private TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new MyTask().execute("xyzname");
    }   

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);
        textView = (TextView) view.findViewById(R.id.text_frag);
        return view;
    }

    public class MyTask extends AsyncTask<String, Void, Organization> {
        @Override
        protected String doInBackground(String... urls) {
            // getting api calls
        }
        @Override
        protected void onPostExecute(String str) {
            loadData(str);
        }
    }

    public void loadData(String str){
        getSupportFragmentManager().beginTransaction() // you miss something here
        textView.setText("name"); // should be ok now
    }
}

}

Upvotes: 1

EightBitBoy
EightBitBoy

Reputation: 683

You create the view at the following line:

View view = inflater.inflate(R.layout.home_fragment, container, false);

To get the TextView you must call findViewById() on this exact view object.

public class MainHandlerFragment extends Fragment {

    //added this line
    private View view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new MyTask().execute("xyzname");
    }   

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //updated this line
        view = inflater.inflate(R.layout.home_fragment, container, false);
        return view;
    }

    public class MyTask extends AsyncTask<String, Void, Organization> {
        @Override
        protected String doInBackground(String... urls) {
            // getting api calls
        }
        @Override
        protected void onPostExecute(String str) {
            loadData(str);
        }
    }
    public void loadData(String str){
        getSupportFragmentManager().beginTransaction()
        //updated this line
        ((TextView) view.findViewById(R.id.text_frag)).setText("name");
    }
}

Upvotes: 2

Related Questions