Question

I am trying to make app with multiple tabs ( 5 tabs with different RSS feeds and one tab with shoutcast dedicated stream player) using ActionBarSherlock and SherlockFragment. I have already achieved what I want by making two different apps, one for RSS and one for player, but now I want it to join them in one single app.

First I made test app with simple text inside each fragment (e.g. this is fragment 1, 2... etc) to look how it looks, and it works. Now I want to implement list and web view from previous app into this one and I'm getting errors for findViewById and getIntent.

This is my fragment code:

public class Fragment_1 extends SherlockFragment {

RSSFeed feed;
TextView title;
WebView desc;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment_1, container, false);

        // Enable the vertical fading edge (by default it is disabled)
            ScrollView sv = (ScrollView) findViewById(R.id.svs);
            sv.setVerticalFadingEdgeEnabled(true);

            // Get the feed object and the position from the Intent
            feed = (RSSFeed) getIntent().getExtras().get("feed");
            int pos = getIntent().getExtras().getInt("pos");

            // Initialize the views
            title = (TextView) findViewById(R.id.titles);
            desc = (WebView) findViewById(R.id.descs);


            // set webview properties
            WebSettings ws = desc.getSettings();
            ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
            ws.getPluginState();
            ws.setPluginState(PluginState.ON);
            ws.setJavaScriptEnabled(true);
            ws.setBuiltInZoomControls(true);



            // Set the views
            title.setText(feed.getItem(pos).getTitle());
            desc.loadDataWithBaseURL(), feed
                    .getItem(pos).getDescription(), "text/html", "UTF-8", null);
}

Code for MainActivity:

public class MainActivity extends SherlockFragmentActivity {

    private ViewPager mViewPager;
    private TabAdapter mTabsAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);
        setContentView(mViewPager);

        final ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mTabsAdapter = new TabAdapter(this, mViewPager);
        mTabsAdapter.addTab(bar.newTab().setText("Naslovna"), Fragment_1.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Estrada & poznati"), Fragment_2.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Zanimljivosti"), Fragment_3.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Vicevi"), Fragment_4.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Smješni video"), Fragment_5.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Stream"), Fragment_6.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("O nama"), Fragment_7.class, null);



    }
}

What I'm missing in my code?

Was it helpful?

Solution

It should be like this (you should do any actions with your view only after it is created):

public class Fragment_1 extends SherlockFragment {

    RSSFeed feed;
    TextView title;
    WebView desc;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.fragment_1, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // Enable the vertical fading edge (by default it is disabled)
        ScrollView sv = (ScrollView) view.findViewById(R.id.svs);
        sv.setVerticalFadingEdgeEnabled(true);

        // Get the feed object and the position from the Intent
        feed = (RSSFeed) getActivity().getIntent().getExtras().get("feed");
        int pos = getActivity().getIntent().getExtras().getInt("pos");

        // Initialize the views
        title = (TextView) view.findViewById(R.id.titles);
        desc = (WebView) view.findViewById(R.id.descs);


        // set webview properties
        WebSettings ws = desc.getSettings();
        ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        ws.getPluginState();
        ws.setPluginState(PluginState.ON);
        ws.setJavaScriptEnabled(true);
        ws.setBuiltInZoomControls(true);



        // Set the views
        title.setText(feed.getItem(pos).getTitle());
        desc.loadDataWithBaseURL(), feed
                .getItem(pos).getDescription(), "text/html", "UTF-8", null);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top