我找不到自己的错误。
public class FragmentParsing extends Fragment implements MyTask.OnTaskComplete{
private ArrayList<String> arrayList=new ArrayList<>();
private ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList);
private ProgressDialog progressDialog = null;
private static final String url="http://quotes.zennex.ru/api/v3/bash/quotes?sort=time";
public FragmentParsing() {
// Required empty public constructor
}
// public static FragmentParsing newInstance() {
// FragmentParsing fragment = new FragmentParsing();
// return fragment;
// }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_parsing, container, false);
ListView lv=(ListView)view.findViewById(R.id.listView);
lv.setAdapter(adapter);
progressDialog = ProgressDialog.show(lv.getContext(), null, "Подключение к серверу...", true, true);
MyTask myTask = new MyTask(FragmentParsing.this);
myTask.execute(url);
return view;
}
@Override
public void onTaskComplete(ArrayList<String> array) {
if(array!=null) {
adapter.clear();
arrayList.addAll(array);
adapter.notifyDataSetChanged();
}
else
Toast.makeText(getView().getContext(), "Сбой при подключение к серверу", Toast.LENGTH_SHORT).show();
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
错误日志:
FATAL EXCEPTION: main
Process: ru.bunakov.testapplication, PID: 29433
java.lang.NullPointerException
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
at ru.bunakov.testapplication.fragments.FragmentParsing.<init>(FragmentParsing.java:20)
at ru.bunakov.testapplication.adapters.TabAdapter.getItem(TabAdapter.java:23)
行中有问题 -
private ArrayAdapter<String> adapter = new ArrayAdapter<String(getActivity(), android.R.layout.simple_list_item_1, arrayList);
在这一行 -
case 2: return new FragmentParsing();
这是TabAdapter
public class TabAdapter extends FragmentStatePagerAdapter {
private int numberOfTabs;
public TabAdapter(FragmentManager fm, int numberOfTabs) {
super(fm);
this.numberOfTabs = numberOfTabs;
}
@Override
public Fragment getItem(int i) {
switch (i){
case 0: return new FragmentList();
case 1: return new FragmentScaling();
case 2: return new FragmentParsing();
case 3: return new FragmentMap();
default: return null;
}
}
@Override
public int getCount() {
return numberOfTabs;
}
}
我的申请正在关闭我选择不是第一个标签。 如果类FragmentParsing为空 - 应用程序没有问题。
编辑:在评论中请求更改后。
public class FragmentParsing extends Fragment implements MyTask.OnTaskComplete{
private ArrayList<String> arrayList = new ArrayList<>();
private ArrayAdapter<String> adapter;
private ProgressDialog progressDialog = null;
private static final String url="http://quotes.zennex.ru/api/v3/bash/quotes?sort=time";
public FragmentParsing() {
// Required empty public constructor
}
// public static FragmentParsing newInstance() {
// FragmentParsing fragment = new FragmentParsing();
// return fragment;
// }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_parsing, container, false);
ListView lv=(ListView)view.findViewById(R.id.listView);
progressDialog = ProgressDialog.show(lv.getContext(), null, "Подключение к серверу...", true, true);
MyTask myTask = new MyTask(FragmentParsing.this);
myTask.execute(url);
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList);
lv.setAdapter(adapter);
return view;
}
错误日志:
FATAL EXCEPTION: main
Process: ru.bunakov.testapplication, PID: 12608
java.lang.NullPointerException
at ru.bunakov.testapplication.fragments.FragmentParsing.onCreateView(FragmentParsing.java:48)
Line - progressDialog = ProgressDialog.show(lv.getContext(), null, "Подключение к серверу...", true, true);
答案 0 :(得分:0)
您的adapter
似乎已使用导致NullPointerException
的空列表进行初始化。简单地把你的
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList); lv.setAdapter(adapter);
在
下面的onTaskComplete()
内
arrayList.addAll(array);
在初始化适配器之前,列表不再为空。干杯! :d