我正试图在我的Android项目中做一些标签。这是我的标签主页:
public class EventDetailMain extends FragmentActivity {
Context context = this;
ViewPager Tab;
EventDtlTabPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_detail_main);
TabAdapter = new EventDtlTabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager) findViewById(R.id.pager);
Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
// Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabReselected(android.app.ActionBar.Tab tab,
android.app.FragmentTransaction ft) {
}
public void onTabSelected(android.app.ActionBar.Tab tab,
android.app.FragmentTransaction ft) {
// on tab selected show respected fragment view
Tab.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(android.app.ActionBar.Tab tab,
android.app.FragmentTransaction ft) {
}
};
// Add New Tab
actionBar.addTab(actionBar.newTab().setText("Event Detail")
.setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Chat Room")
.setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Drop Review")
.setTabListener(tabListener));
}
我的第一个标签的内容:
public class EventDetail extends Fragment {
Context context;
View eventDetail;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
eventDetail = inflater.inflate(R.layout.event_detail, container, false);
context = this;
Intent i = getIntent();
Event eventModel = (Event) i.getSerializableExtra("eventObj");
}
}
但是,我收到错误消息“类型不匹配:无法从EventDetail转换为Context”在context = this和“getIntent()方法getIntent()未定义类型EventDetail”。
有什么想法吗?是因为我没有扩展活动吗?
提前致谢。
修改
我收到错误消息:
11-13 20:24:49.634: E/AndroidRuntime(16503): FATAL EXCEPTION: main
11-13 20:24:49.634: E/AndroidRuntime(16503): java.lang.NullPointerException
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:116)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.support.v4.view.ViewPager.populate(ViewPager.java:1053)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.view.View.measure(View.java:12892)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.view.View.measure(View.java:12892)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.view.View.measure(View.java:12892)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
11-13 20:24:49.634: E/AndroidRuntime(16503): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2257)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.view.View.measure(View.java:12892)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.os.Looper.loop(Looper.java:137)
11-13 20:24:49.634: E/AndroidRuntime(16503): at android.app.ActivityThread.main(ActivityThread.java:4512)
11-13 20:24:49.634: E/AndroidRuntime(16503): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 20:24:49.634: E/AndroidRuntime(16503): at java.lang.reflect.Method.invoke(Method.java:511)
11-13 20:24:49.634: E/AndroidRuntime(16503): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
11-13 20:24:49.634: E/AndroidRuntime(16503): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
11-13 20:24:49.634: E/AndroidRuntime(16503): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
是。在Fragment中你有getActivity()。从那里你可以使用getIntent()。
修改所以: 第一个错误 - context = this; 应该是 context = getActivity(); 第二个错误 - getIntent()应该是 getActivity()。getIntent();
EDIT2: 所以你有第三个错误:D你需要返回一个视图:
return eventDetail;
而且,你没有在任何地方使用eventModel。
答案 1 :(得分:0)
您需要setArguments
将片段附加到Fragmentmanager
,然后您可以将Bundle
作为参数发送。
Bundle bundle = new Bundle();
EventDetail toOpenFragment = new EventDetail();
bundle.putInt("index", 1);
toOpenFragment.setArguments(bundle);
并在片段使用中接收此传递的int
,如此。
final int index = getArguments().getInt("index");
答案 2 :(得分:0)
只需通过调用
来使用您的活动意图即可 getActivity.getIntent();
Intent catid_intent ;
catid_intent = getActivity().getIntent();
// sharePref = SharePref.getInstance(getActivity());
String cat_id = catid_intent.getStringExtra("cat_id");
Toast.makeText(getActivity(), "Cat id premium" + cat_id, Toast.LENGTH_LONG).show();