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>