Reputation: 193
In my application I have MainActivity (extends Fragment activity), topButtons independent from mPagerView (ex: AddButton), PagerView with Fragments inside. What I want to do is:
1) Download some content (new activity is started then on activityResult is done get information back)
2) Create new object and add it to the listView inside Fragment in ViewPager.
3) Refresh listView inside Fragment in PagerView after adding new element to the list.
What I get:
1) Successfully download file and create object and add it to the list
Problem:
1)When I want to refresh listview I got error like: NullPointerException in.....
Code here:
Find my target Fragment in PagerView:
Fragment frag = mAdapter.getItem(position);
if(position == 1)
{
((KwejkFragment) frag).notifyNewInsertedData();
}
Trying to refresh lisview using this in destination Fragment (my method):
public void notifyNewInsertedData()
{
this.adapter.notifyDataSetChanged(); //App crashes here telling me that I have a null pointerException. List is created on activityStarted and is not null.
}
And here I got an error like:
02-15 15:02:27.833: E/AndroidRuntime(31310): Caused by: java.lang.NullPointerException
02-15 15:02:27.833: E/AndroidRuntime(31310): at pl.app.fragments.KwejkFragment.notifyNewInsertedData(KwejkFragment.java:340)
02-15 15:02:27.833: E/AndroidRuntime(31310): at pl.engine.main.MainActivity.onActivityResult(MainActivity.java:630)
02-15 15:02:27.833: E/AndroidRuntime(31310): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
02-15 15:02:27.833: E/AndroidRuntime(31310): at android.app.ActivityThread.deliverResults(ActivityThread.java:2549)
02-15 15:02:27.833: E/AndroidRuntime(31310): ... 11 more
What is wrong? How to resolve this problem? What am I doing wrong? All lists are stored in GlobalApplication class. I point to them when creating Fragments and get this lists.
UPDATED:
public class KwejkFragment extends Fragment{
private static ArrayList<GifModel> list = null; //BASIC PROGRAM LIST
private static ListView listView;
private static List<GifModel> sortingList;
private AlertDialog.Builder dialog;
private DataBaseManager dataBaseManager;
private Handler handler;
private ListAdapter adapter;
private String sprawdzenie;
///////// VARIABLES HERE////////
private boolean useAnimations = false;
private int animationSpinnerPosition;
private String fragmentTag;
public KwejkFragment(String tagName,String sprawdzenie)
{
this.fragmentTag = tagName;
this.sprawdzenie = sprawdzenie;
}
public String sprawdzenie()
{
return sprawdzenie;
}
public String getTagName()
{
return this.fragmentTag;
}
public void refresh(){};
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
getSharedPreferences();
//setHandler();
GlobalApplication globalApp = (GlobalApplication) getActivity().getApplication();
list = globalApp.getKwejkList();
adapter = new ListAdapter(getActivity(), R.id.list_item, list, useAnimations, animationSpinnerPosition);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return fragmentTag;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//dataBaseManager.close();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.kwejkpl_layout, container, false);
listView = (ListView) view.findViewById(R.id.listViewKwejk);
// list = new ArrayList<GifModel>();
// list.add(new GifModel());
// adapter = new ListAdapter(getActivity(), R.id.list_item,list, useAnimations, animationSpinnerPosition);
// if(adapter == null)
// System.out.println("ADAPTER YEST NULL");
listView.setAdapter(adapter);
// TODO Auto-generated method stubl
//prepareDataBase();
//list = dataBaseManager.getAllGifs();
//Log.i("!!!!!!!",""+ list.size());
setUpView();
//adapter.notifyDataSetChanged();
return view;
}
public void createNewItems(String name,int rating)
{
GifModel gif = new GifModel();
gif.setNameOfItem(name);
gif.setRating(rating);
list.add(gif);
System.out.println(list.size());
//System.out.println("Is adapter null:"+ adapter.isEmpty());
adapter = new ListAdapter(getActivity(), R.id.list_item, list, useAnimations, animationSpinnerPosition);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
// Thread a = new Thread(new Runnable() {
//
// @Override
// public void run() {
// list = dataBaseManager.getAllGifs();
// handler.sendEmptyMessage(1);
// //System.out.println(list.size());
// }
// });
// a.start();
super.onResume();
adapter.notifyDataSetChanged();
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
//dataBaseManager.close();
super.onDestroyView();
}
public void prepareDataBase()
{
// if(ifAppWorks)
// {
// dataBaseManager.close();
// }
dataBaseManager = new DataBaseManager(getActivity());
dataBaseManager.open();
System.out.println("has connection?:" + dataBaseManager.isOpen());
}
private void setHandler()
{
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.what == 1)
{
adapter = new ListAdapter(getActivity(), R.layout.list_item, list, useAnimations, animationSpinnerPosition);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
};
}
private void getSharedPreferences()
{
SharedPreferences preferences = getActivity().getSharedPreferences(Tools.PREFS_NAME,Activity.MODE_PRIVATE);
useAnimations = preferences.getBoolean("animation", false);
animationSpinnerPosition = preferences.getInt("animationPosition", 0);
}
private void setUpView(){
dialog = new AlertDialog.Builder(getActivity());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
System.out.println(list.get(position).getCoverUrl());
System.out.println("link pozycji");
//GlobalApplication app = (GlobalApplication) getApplication();
if(list.get(position).getCoverUrl().contains("http://i1.ytimg") || list.get(position).getCoverUrl().contains("http://www.youtube.com/embed"))
{
Intent intent = new Intent(getActivity(), MovieShow.class);
//System.out.println(list.get(position).getUrl());
intent.putExtra("youtubeLink", list.get(position).getUrl());
System.out.println(list.get(position).getUrl());
intent.putExtra("header", list.get(position).getNameOfIteme());
intent.putExtra("contentlink", list.get(position).getMainLinkToTheContent());
System.out.println(list.get(position).getUrl());
//overridePendingTransition(R.anim.trans_left_in,R.anim.trans_left_out);
System.out.println(list.get(position).getUrl());
startActivity(intent);
// if(app.getIfAnimationsOrNot())
// overridePendingTransition(R.anim.trans_left_in,R.anim.trans_left_out);
}
else if((list.get(position).getCoverUrl().contains("jpg")) || (list.get(position).getCoverUrl().contains("png")) ||
(list.get(position).getCoverUrl().contains("jpeg")))
{
Intent intent = new Intent(getActivity(), ImageShow.class);
intent.putExtra("imageUrl", list.get(position).getUrl());
intent.putExtra("header", list.get(position).getNameOfIteme());
intent.putExtra("contentlink", list.get(position).getMainLinkToTheContent());
startActivity(intent);
// if(app.getIfAnimationsOrNot())
// overridePendingTransition(R.anim.trans_left_in,R.anim.trans_left_out);
}
else
{
// Intent intent = new Intent(MainActivity.this, GifAnimationClass.class);
// intent.putExtra("gifAdress", list.get(position).getUrl());
// System.out.println(list.get(position).getUrl());
// startActivity(intent);
Intent intent = new Intent(getActivity(), GifPlay.class);
intent.putExtra("webUrl", list.get(position).getUrl());
intent.putExtra("header", list.get(position).getNameOfIteme());
intent.putExtra("contentlink", list.get(position).getMainLinkToTheContent());
//overridePendingTransition(R.anim.trans_left_in,R.anim.trans_left_out);
startActivity(intent);
// if(app.getIfAnimationsOrNot())
// overridePendingTransition(R.anim.trans_left_in,R.anim.trans_left_out);
// //overridePendingTransition(R.anim.trans_left_in,R.anim.trans_left_out);
}
}
});
}
public void notifyNewInsertedData()
{
this.adapter.notifyDataSetChanged();
}
}
Upvotes: 0
Views: 202
Reputation: 1230
I think that your adapter is null. Try to initialise it inside the onStart method of your fragment.
@Override
public void onStart() {
adapter = new ListAdapter(getActivity(), R.id.list_item, list, useAnimations, animationSpinnerPosition);
}
Upvotes: 1