我正在尝试使用ActionBarSherlock和SherlockFragment制作带有多个标签的应用(带有不同RSS源的5个标签和带有shoutcast专用流播放器的标签)。我已经通过制作两个不同的应用程序实现了我的目标,一个用于RSS,一个用于播放器,但现在我希望它能够在一个应用程序中加入它们。
首先,我在每个片段中使用简单文本制作了测试应用程序(例如,这是片段1,2等等)以查看它的外观,并且它可以工作。现在我想实现上一个应用程序中的列表和Web视图到这个,我收到了findViewById和getIntent的错误。
这是我的片段代码:
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);
}
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);
}
}
我的代码中缺少什么?
答案 0 :(得分:0)
应该是这样的(只有在创建后才能对视图执行任何操作):
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);
}
}