As explained in the title, I want get a value from a database using AsyncTask
and change TextView
text in a Fragment.
I don't have any problem to use AsyncTask
and get the value from the database. But when I change the TextView
text (from within a fragment) I receive this error:
FATAL EXCEPTION: main
Process: com.kirolm.instalacionesdep, PID: 14072
java.lang.NullPointerException
at com.kirolm.instalacionesdep.HomeFragment.writeTextViewsTituloDelPeriodico(HomeFragment.java:206)
at com.kirolm.instalacionesdep.asynctask.LoadLastNewAsync.onPostExecute(LoadLastNewAsync.java:92)
at com.kirolm.instalacionesdep.asynctask.LoadLastNewAsync.onPostExecute(LoadLastNewAsync.java:1)
06-at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
I Launch AsyncTask
in HomeFragment.java
(onCreate method):
context = getActivity().getBaseContext();
LoadLastNewAsync llna = new LoadLastNewAsync(context, lang);
llna.execute();
I declare TextView
in onCreateView
method:
tv_tituloNoticia = (TextView) view.findViewById(R.id.home_fragment_tv_titulo_noticia);
This is the onPostExecute
method (LoadLastNewAsync.java
):
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
homeFragment = new HomeFragment();
homeFragment.writeTextViewsTituloDelPeriodico(tituloAviso);
}
WriteTextViewTituloDelPeriodico
is a public method in HomeFragment
.
public void writeTextViewsTituloDelPeriodico(String texto){
tv_tituloNoticia.setText(texto);
}
Can anyone help me?
Thank you.
I have solved my problem.
I modified my
AsyncTask
and now it returns aString
. In the HomeFragment (onCreate
method) I execute myAsyncTask
and receive aString
:In the
onCreateView
method I set TextView using tituloAviso.This is LoadLastAsync.java code.