我有一个适用于LisFragment和ViewPagerIndicator的应用程序。当屏幕旋转两次时,应用程序会给出logcat附加的错误。 我的问题似乎与上下文无关。我在StackOverflow上看到了几个问题,但我还没有设法解决它。似乎我必须实现onAttach和onDetach上的方法,但不能像我试过的那样失败。
请你帮我。我坦率地对这个问题感到绝望。
感谢。
我的LogCat
11-28 08:30:18.939: E/AndroidRuntime(602): FATAL EXCEPTION: main
11-28 08:30:18.939: E/AndroidRuntime(602): java.lang.NullPointerException
11-28 08:30:18.939: E/AndroidRuntime(602): at android.widget.ArrayAdapter.init(ArrayAdapter.java:271)
11-28 08:30:18.939: E/AndroidRuntime(602): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:150)
11-28 08:30:18.939: E/AndroidRuntime(602): at es.app.adeptlabel.AdaptadorList.<init>(AdaptadorList.java:27)
11-28 08:30:18.939: E/AndroidRuntime(602): at es.app.adeptlabel.ArtFragment$CreateArrayListTaskArt.onPostExecute(ArtFragment.java:63)
11-28 08:30:18.939: E/AndroidRuntime(602): at es.app.adeptlabel.ArtFragment$CreateArrayListTaskArt.onPostExecute(ArtFragment.java:1)
11-28 08:30:18.939: E/AndroidRuntime(602): at android.os.AsyncTask.finish(AsyncTask.java:417)
11-28 08:30:18.939: E/AndroidRuntime(602): at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-28 08:30:18.939: E/AndroidRuntime(602): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-28 08:30:18.939: E/AndroidRuntime(602): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 08:30:18.939: E/AndroidRuntime(602): at android.os.Looper.loop(Looper.java:123)
11-28 08:30:18.939: E/AndroidRuntime(602): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-28 08:30:18.939: E/AndroidRuntime(602): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 08:30:18.939: E/AndroidRuntime(602): at java.lang.reflect.Method.invoke(Method.java:507)
11-28 08:30:18.939: E/AndroidRuntime(602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-28 08:30:18.939: E/AndroidRuntime(602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-28 08:30:18.939: E/AndroidRuntime(602): at dalvik.system.NativeStart.main(Native Method)
11-28 08:30:19.099: E/Buffer Error(602): Error converting result java.io.IOException: Attempted read on closed stream.
我继承自ListFragment
的类public class RlsFragment extends ListFragment{
Activity mActivity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// normally you should inflate a view here and save references
// using ListFragment default layout for this example
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
new CreateArrayListTaskRls().execute();
}
private class CreateArrayListTaskRls extends AsyncTask<String, Void, ArrayList <ElementosList>> {
protected ArrayList <ElementosList> doInBackground(String...strings)
{
ArrayList <ElementosList> itemRls = new ArrayList <ElementosList>();
itemRls.clear();
loadDataJsonRls();
for(int i=0;i<ListViewConfig.getResim_list_txt().size();i++)
{
itemRls.add(new ElementosList(ListViewConfig.getResim_list_txt().get(i),
ListViewConfig.getResim_list_img().get(i)));
}
return itemRls;
}
protected void onPostExecute(ArrayList <ElementosList> itemsRls) {
ArrayAdapter<ElementosList>adaptadorRls = new AdaptadorList(getActivity(),itemsRls);
setListAdapter(adaptadorRls);
}
}//fin createArraylistTask
.
.
.
.
.
.
La clase AdaptadorList
public class AdaptadorList extends ArrayAdapter<ElementosList> {
Activity contexto;
private ArrayList<ElementosList> elementos;
AdaptadorList(Activity contexto, ArrayList<ElementosList> elementos)
{
super(contexto,R.layout.opciones,elementos);
this.elementos = elementos;
this.contexto = contexto;
}
public View getView(int position, View convertView, ViewGroup parent )
{
View item = convertView;
final VistaTag vistaTag;
if(item==null)
{
LayoutInflater inflater = contexto.getLayoutInflater();
item = inflater.inflate(R.layout.opciones, null);
vistaTag = new VistaTag();
vistaTag.titulo = (TextView)item.findViewById(R.id.textView1);
vistaTag.imgadpt = (ImageView)item.findViewById(R.id.imageView1);
item.setTag(vistaTag);
}
else
{
vistaTag = (VistaTag)item.getTag();
}
//Aplicacion fuente verdana
Typeface verdana = Typeface.createFromAsset(contexto.getAssets(), "verdana.ttf");
vistaTag.titulo.setText(elementos.get(position).getTitulo());
vistaTag.titulo.setTypeface(verdana);
vistaTag.imgadpt.setImageBitmap(getBitmapFromURL(elementos.get(position).getImagen()));
return(item);
}//fin getView
答案 0 :(得分:0)
这是片段和AsyncTask的常见问题。它是由AsyncTask完成的,它在当下工作,然后片段的视图不存在。如果对片段使用AsyncTask,请始终检查片段的视图是否可用。更好的方法是使用加载器。
这可以修复你的代码:
private class CreateArrayListTaskRls extends AsyncTask<String, Void, ArrayList <ElementosList>> {
protected ArrayList <ElementosList> doInBackground(String...strings)
{
ArrayList <ElementosList> itemRls = new ArrayList <ElementosList>();
itemRls.clear();
loadDataJsonRls();
for(int i=0;i<ListViewConfig.getResim_list_txt().size();i++)
{
itemRls.add(new ElementosList(ListViewConfig.getResim_list_txt().get(i),
ListViewConfig.getResim_list_img().get(i)));
}
return itemRls;
}
protected void onPostExecute(ArrayList <ElementosList> itemsRls) {
ArrayAdapter<ElementosList>adaptadorRls = new AdaptadorList(getActivity(),itemsRls);
if (getListView() != null) {
setListAdapter(adaptadorRls);
}
}
}//fin createArraylistTask
答案 1 :(得分:0)
解决方案:
protected void onPostExecute(ArrayList <ElementosList> itemsRls) {
if (getListView() != null) { ArrayAdapter<ElementosList>adaptadorRls = new AdaptadorList(getActivity(),itemsRls);
setListAdapter(adaptadorRls);
}
}
感谢大家。谢谢Oknesif。