正如标题中所解释的,我希望使用AsyncTask
从数据库中获取值并更改片段中的TextView
文本。
使用AsyncTask
并从数据库中获取值时没有任何问题。但是当我更改TextView
文本(从片段中)时,我收到此错误:
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)
我在AsyncTask
(onCreate方法)中启动HomeFragment.java
:
context = getActivity().getBaseContext();
LoadLastNewAsync llna = new LoadLastNewAsync(context, lang);
llna.execute();
我在TextView
方法中宣布onCreateView
:
tv_tituloNoticia = (TextView) view.findViewById(R.id.home_fragment_tv_titulo_noticia);
这是onPostExecute
方法(LoadLastNewAsync.java
):
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
homeFragment = new HomeFragment();
homeFragment.writeTextViewsTituloDelPeriodico(tituloAviso);
}
WriteTextViewTituloDelPeriodico
是HomeFragment
中的公共方法。
public void writeTextViewsTituloDelPeriodico(String texto){
tv_tituloNoticia.setText(texto);
}
任何人都可以帮助我吗?
谢谢。
答案 0 :(得分:0)
我已经解决了我的问题。
我修改了AsyncTask
,现在它返回String
。
在HomeFragment(onCreate
方法)中,我执行AsyncTask
并收到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();
}
在onCreateView
方法中,我使用tituloAviso设置TextView。
这是LoadLastAsync.java代码。
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);
}
}