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>
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");
}
}
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With