Get value from data base using AsyncTask and change TextView value in Fragment

296 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

I have solved my problem.

I modified my AsyncTask and now it returns a String. In the HomeFragment (onCreate method) I execute my AsyncTask and receive a String:

            LoadLastNewAsync llna = new LoadLastNewAsync(context, lang);

            try {
                tituloAviso = llna.execute().get();
                Log.d("Testing", "El valor del string recibido es: "+tituloAviso);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

In the onCreateView method I set TextView using tituloAviso.

This is LoadLastAsync.java code.

public class LoadLastNewAsync extends AsyncTask <Void, Void, String>{
    Context context;
    private DataBaseHelper myDbHelper;
    private String tituloAviso;
    private Aviso aviso;
    private String lang;
    private HomeFragment homeFragment;


    //constructor
    public LoadLastNewAsync(Context context, String lang) {
         this.context = context;
         this.lang = lang;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);


    }

    @Override
    protected String doInBackground(Void... params) {
        Log.i("Testing", "LoadLastNewAsync. Entra en doInBackGround");
        myDbHelper = new DataBaseHelper(context);
        boolean dbExist = myDbHelper.checkDataBase();
        if(dbExist){
            aviso = new Aviso();
            aviso = recuperarUltimoAviso();
            tituloAviso = aviso.getTitle();
        }

        return tituloAviso; 

    }

    private Aviso recuperarUltimoAviso() {
        try {
            myDbHelper.openDataBase();
            aviso = myDbHelper.getUltimoAviso(this.lang);
        } catch (Exception e) {
            // TODO: handle exception
        }
        myDbHelper.close();
        return aviso;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }
}