如何在片段中找到我的活动按钮

时间:2013-12-04 08:07:02

标签: android

刚开始学习android。请帮忙。我有三个片段,每个片段都有一个必须运行媒体播放器其中一个流的按钮。如何在片段中找到我的活动按钮,该按钮稍后会附加到特定的流中? 试图引用该按钮,以便:

btnStart = (Button) titleAdapter.frags[0].getView().findViewById(R.id.btnStart);

没用,错误java.lang.NullPointerException,我做错了

public class TitleAdapter extends FragmentPagerAdapter {
    private final String titles[] = new String[] { "FragA", "FragB", "FragC" };
    private final Fragment frags[] = new Fragment[titles.length];
    public TitleAdapter(FragmentManager fm) {
        super(fm);
        frags[0] = new FragmentA();
        frags[1] = new FragmentB();
        frags[2] = new FragmentC();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Log.v("TitleAdapter - getPageTitle=", titles[position]);
        return titles[position];
    }

    @Override
    public Fragment getItem(int position) {
        Log.v("TitleAdapter - getItem=", String.valueOf(position));
        return frags[position];
    }

    @Override
    public int getCount() {
        return frags.length;
    }
}

代码MainActivity.java:

public class MainActivity extends FragmentActivity {

  final static String LOG_TAG = "myLogs";
  static MediaPlayer mediaPlayer;
  static AudioManager am;
  static CheckBox pdaStream;
  static Button btnNews;
  ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mViewPager = (ViewPager) findViewById(R.id.pager);


    TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(titleAdapter);
    mViewPager.setCurrentItem(1);
    mViewPager.setOffscreenPageLimit(2);

    pdaStream = (CheckBox) findViewById(R.id.pdaStream);
    am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    SeekBar music = (SeekBar)findViewById(R.id.seekBar1);
    initBar(music, AudioManager.STREAM_MUSIC);



}




@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);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.news:
        Intent intent = new Intent(this, RSSActivity.class);
        startActivity(intent);
      break;
    default:
      break;
    }
    return super.onOptionsItemSelected(item);
}


public static void initBar(SeekBar bar, final int stream) {
      bar.setMax(am.getStreamMaxVolume(stream));
      bar.setProgress(am.getStreamVolume(stream));

      bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar bar, int progress,
                                      boolean fromUser) {
          am.setStreamVolume(stream, progress,
                              AudioManager.FLAG_PLAY_SOUND);
        }

        public void onStartTrackingTouch(SeekBar bar) {
          // no-op
        }

        public void onStopTrackingTouch(SeekBar bar) {
          // no-op
        }
      });
    }

}

代码片段类似,这里是其中之一

代码FragmentA.java:

public class FragmentA extends Fragment {

  final static String LOG_TAG = "myLogs";  
  private static ProgressBar progressBar;
      static CheckBox pdaStream;



    public FragmentA() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_a, container, false);

    final Button Play = (Button) rootView.findViewById(R.id.btnStart);
    progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
    progressBar.setVisibility(View.GONE);
    pdaStream = (CheckBox) getActivity().findViewById(R.id.pdaStream);

       return rootView;
    }   }

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity" >


<android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="@drawable/bg_pager"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textColor="#fff" />
</android.support.v4.view.ViewPager>

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="60dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:thumb="@drawable/seek" />

<CheckBox
    android:id="@+id/pdaStream"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="70dp"
    android:text="@string/chkbox" />

</RelativeLayout>

fragment_a.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/btnStart"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textSize="16sp" 
    android:onClick="onClick"
    android:background="@drawable/play_button"/>

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />


</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

你可以像这样访问,例如你想要访问片段frags [1]的按钮:

Button btnFragment = frags[1].getView().findViewById(R.id.yourButtonID);
希望它有所帮助!

答案 1 :(得分:0)

您必须将活动中的引用传递给片段。当您从PagerAdapter创建Fragments时,它也必须具有引用。您可以在构造函数(新的片段(活动a))中执行此操作,也可以在创建PagerAdapter后从活动中初始化的公共变量Activity执行此操作。

活动中的

 x = new TitleAdapter();
 x.mActivity = this; (while mActivity must be declared public in the TitleAdapter()
适配器中的

frags[0] = new FragmentA();
frags[0].mActivity = this.mActivity;

然后在你的Fragment中你可以为你的按钮添加一个OnClickListener。

相关问题