从片段选项卡布局启动活动

时间:2018-07-04 12:32:45

标签: android android-fragments fragment android-tablayout

我从片段启动活动时遇到问题。

“我的应用程序”的构建类似于选项卡布局,在后台具有“活动”,而片段具有选项卡。在我要设置一个按钮的一个选项卡中,此按钮必须启动“活动”。我尝试了所有操作,但想知道为什么它不起作用。

Logcat也什么也不显示。

这是带有按钮的片段

public class Freestyle extends Fragment {

public Freestyle() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_freestyle, container, false);
    Button btnOpen;
         btnOpen   =(Button)view.findViewById(R.id.btnOpenPlan);
    btnOpen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(getContext(),Plan.class);
            startActivity(intent);
        }

    });
    return view;
}

}

这是活动中的代码,必须启动

public class Plan extends AppCompatActivity {

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

        }

}

这是后台活动中的代码

public class MainActivity extends AppCompatActivity {


private SectionsPagerAdapter mSectionsPagerAdapter;


private ViewPager mViewPager;

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



    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container2);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }


    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = null;
        switch (getArguments().getInt(ARG_SECTION_NUMBER)) {

            case 1:
                //eine Serververbindung
                rootView = inflater.inflate(R.layout.fragment_freestyle, container, false);

                break;
            case 2:
                //eine weitere
                rootView = inflater.inflate(R.layout.fragment_events, container, false);

                break;
            case 3:
                //eine weitere
                rootView = inflater.inflate(R.layout.fragment_map, container, false);

                break;
            case 4:
                //eine weitere
                rootView = inflater.inflate(R.layout.fragment_tipp, container, false);

                break;
            case 5:
                //eine weitere
                rootView = inflater.inflate(R.layout.fragment_person, container, false);

                break;

        }
        return rootView;
    }
}


public static class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        return 5;
    }
}

4 个答案:

答案 0 :(得分:0)

使用此

btnOpen.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     Intent intent = new Intent(getActivity(), Plan.class);
     getActivity().startActivity(intent); 
    }

});

答案 1 :(得分:0)

尝试一下

    Intent intent=new Intent(getActivity(),Plan.class);
    getActivity().startActivity(intent);

答案 2 :(得分:0)

尝试使用getActivity(类型转换为您的Activity实例)从片段开始新的Activity

btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(((MainActivity) getActivity()), Plan.class);
            ((MainActivity) getActivity()).startActivity(intent);

        }
    });

希望这会有所帮助。

答案 3 :(得分:0)

尝试这样的事情

btnLaunchActivity.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Check if getActivity() is actually not null, could be null if fragment detatched, you could also check if its an instance you expect to be
        if(getActivity() != null && getActivity() instanceof MyActivity) {
            Intent intent = new Intent(getActivity(), TargetActivity.class);
            //Launch the intent from the activity reference
            getActivity().startActivity(intent); 
        }
    }
 });