0

Before tabActivity got deprecated, I was having an tabactivity handling content in one of my 5 tabs... since everything got replaced with fragment nowdays, i m having trouble migrate my tabactivity to fragment

here is my old code for tabactivity, it is a list of youtube video feeds display on the tab, listview is adapted by a custom adaptor...:

   public class HomePageTab extends TabActivity {
        private VideosListView listView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.video_tab_list);
            listView = (VideosListView) findViewById(R.id.videosListView);
            getUserYoutubeFeed();
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {

                    String url = ((TextView) view.findViewById(R.id.url)).getText().toString();

                    Intent in = new Intent(getApplicationContext(), PlayVideosActivity.class);
                    in.putExtra("url", url);
                    startActivity(in);
                }
            });
        }
}

now here is the new code for my fragment:

public class HomePageTab extends TabActivity {
 private VideosListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.video_tab_list, container, false);
        listView = (VideosListView)getView().findViewById(R.id.videosListView);
        getUserYoutubeFeed();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                String url = ((TextView) view.findViewById(R.id.url)).getText().toString();

                Intent in = new Intent(getApplicationContext(), PlayVideosActivity.class);
                in.putExtra("url", url);
                startActivity(in);
            }
        });
    }
        return v;
    }

when i run my app, i got an error message:

java.lang.NullPointerException
            at com.demo.HomePageTab.onCreateView(HomePageTab.java:33)
sefirosu
  • 2,558
  • 7
  • 44
  • 69

1 Answers1

1

Try this..

Change this..

listView = (VideosListView)getView().findViewById(R.id.videosListView);

to

listView = (VideosListView) v.findViewById(R.id.videosListView);

EDIT

Intent in = new Intent(getActivity(), PlayVideosActivity.class);
in.putExtra("url", url);
getActivity().startActivity(in);
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • err, but when i try to click on the list row and get new activity to start: Intent in = new Intent(getApplicationContext(), PlayVideosActivity.class); in.putExtra("url", url); startActivity(in); this line,,getApplicationContext() wont be resolved, why is that? it was able to resolve in tabactivity... – sefirosu Jun 25 '14 at 09:33
  • ummm,,, getActivity() caused the whole onItemclick block went into red lines.....-.- – sefirosu Jun 25 '14 at 09:36