我有一个带有3个标签的应用程序,我在新建Android项目时使用操作栏标签(使用ViewPager)创建。 (作为一个初学者,我对ViewPager或Fragment或Activity一无所知,我只是使用了ADT中的工具。你可能想在考虑那个困扰时回答我:))
我的问题是: 在第三个选项卡中有两个按钮,例如B1和B2。我想在B1点击时显示一个片段(SearchForSaleFragment),在B2点击时显示另一个片段。
我想我应该为我的按钮添加OnClickListener。 像这样:(?)
RadioButton rb = (RadioButton) findViewById(R.id.radio32);
rb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getActivity(),SearchForSaleFragment.class);
startActivity(i);
}
});
当我把它放在MainActivity.OnCreate()中时,rb得到null值。 我在正确的道路上吗?如果是这样,我应该将上述代码放在ActivityMain中?
请考虑解释甚至明显的要点,因为我是新人,而不是所有人都有资格和经验。
提前致谢。
编辑: ActivityMain代码:
package com.khoonat.khoonat2;
公共类MainActivity扩展了ActionBarActivity实现 ActionBar.TabListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a {@link FragmentPagerAdapter}
* derivative, which will keep every loaded fragment in memory. If this
* becomes too memory intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(Farsi.Convert(getString(R.string.app_name_farsi)));
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
//ff code: To make the right most tab selected at the beginning.
mViewPager.setCurrentItem(mSectionsPagerAdapter.getCount()-1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
//ff code
RadioButton rb = (RadioButton) findViewById(R.id.radio32);
Toast.makeText(getApplicationContext(), "this is my Toast in main!!! =)" +rb,
Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
// ****************************************** ********************** 内班 / ** *一个{@link FragmentPagerAdapter},返回对应的片段 *其中一个部分/标签/页面。 * / 公共类SectionsPagerAdapter扩展了FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0){
RadioButton rb = (RadioButton) findViewById(R.id.radio32);
Toast.makeText(getApplicationContext(), "2: this is my Toast in main!!! =)" +rb,
Toast.LENGTH_LONG*2).show();
return QuickCreatePropertyListingFragment.newInstance(position+1);
}
else if (position ==1)
return SearchForRentFragment.newInstance(position+1);
else
return SearchForSaleFragment.newInstance(position+1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0: return R.string.label_create;
case 1:
return R.string.label_rent;
case 2:
return R.string.label_sell;
}
return null;
}
}
}
第三个标签:(注释部分给出NUllPointerException)
public class QuickCreatePropertyListingFragment extends Fragment {
public static QuickCreatePropertyListingFragment newInstance(int sectionNumber){
QuickCreatePropertyListingFragment fragment = new QuickCreatePropertyListingFragment();
Bundle args = new Bundle();
args.putInt("section_number", sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.quick_create_property_listing, container,false);
/*
RadioButton rb = (RadioButton) container.findViewById(R.id.radio32);
Toast.makeText(getActivity().getApplicationContext(), "this is my Toast message!!! =)" +rb,
Toast.LENGTH_LONG).show();
rb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getActivity(),SearchForSaleFragment.class);
startActivity(i);
}
});
*/
return rootView;
}
}
第三个标签.XML:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingRight="10dp">
<RadioGroup
android:id="@+id/radioGroupInTab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<RadioButton
android:id="@+id/radio31"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:background="@drawable/yourbuttonbackground"
android:button="@android:color/transparent"
android:checked="true"
android:gravity="center"
android:text="test" />
<RadioButton
android:id="@+id/radio32"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:background="@drawable/yourbuttonbackground"
android:button="@android:color/transparent"
android:checked="false"
android:gravity="center"
android:text="test" />
</RadioGroup>
</HorizontalScrollView>
答案 0 :(得分:1)
问题在于:RadioButton rb = (RadioButton) findViewById(R.id.radio32);
您应该显示您获得RadioButton的视图,它应该是这样的:
View v = inflater.inflate(R.layout.your_layout, container, false);
RadioButton rb = (RadioButton) v.findViewById(R.id.radio32);
答案 1 :(得分:1)
将RadioButton rb = (RadioButton) container.findViewById(R.id.radio32);
更改为RadioButton rb = (RadioButton) rootView.findViewById(R.id.radio32);
。应该这样做。