Java 查看寻呼机的片段需要从主活动中编程

Java 查看寻呼机的片段需要从主活动中编程,java,android,android-fragments,Java,Android,Android Fragments,我使用的是浏览寻呼机。我在其中有三个片段,我在其中一个片段中添加了一个按钮,并尝试在fragment类中为其创建一个方法,但这不起作用。在那之后,我试着把这个方法放在主要的活动课上,它成功了 这是我的主要活动代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.la

我使用的是浏览寻呼机。我在其中有三个片段,我在其中一个片段中添加了一个按钮,并尝试在fragment类中为其创建一个方法,但这不起作用。在那之后,我试着把这个方法放在主要的活动课上,它成功了

这是我的主要活动代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setDisplayShowTitleEnabled(false);
        mAuth = FirebaseAuth.getInstance();
        mViewPager = findViewById(R.id.viewpager);
        adapter = new SamplePagerAdapter(this);
        tabLayout = findViewById(R.id.tablayout);
        mViewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(mViewPager);

        mViewPager.setCurrentItem(1);


        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText("تعلم");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.learn, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText("الصفحة الرئيسية");
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.home, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);

        TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabThree.setText("التحديات");
        tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.challenges, 0, 0);
        tabLayout.getTabAt(2).setCustomView(tabThree);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabPosition = tab.getPosition();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.myQuestions:
                //newGame();
                return true;
            case R.id.aboutApp:
                //showHelp();
                return true;
            case R.id.bestStudents:
                //showHelp();
                return true;
            case R.id.myAccount:
                //showHelp();
                return true;
            case R.id.signOut:
                mAuth.signOut();
                Intent i = new Intent(MainActivity.this, GeneralSignActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if(tabPosition != 1){
            mViewPager.setCurrentItem(1);
        }
        else{
            this.finishAffinity();
            System.exit(0);
        }
    }
}
这是我的片段代码:

public static LearnFragment newInstance(String param1, String param2) {
    LearnFragment fragment = new LearnFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

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

    return myView;
}
这是主要活动的xml代码:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/sample_main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/color1">

                <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/app_name"
                    android:textColor="@color/white"
                    android:textSize="@dimen/text_size_in_toolbar" />
            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tablayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/color1">

            </android.support.design.widget.TabLayout>
        </android.support.design.widget.AppBarLayout>


        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1"
            android:background="@android:color/white" />

    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

这是片段xml代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mk.playAndLearn.fragment.LearnFragment">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:onClick="click3"
    android:text="click me"/>

</FrameLayout>

并试图在fragment类中为其创建一个方法,但 不起作用

如果已将
按钮添加到
片段
,则应使用视图前缀初始化
片段
中的
按钮
,并将
按钮
添加到
片段
布局中。因此:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.fragment_learn,container,false);
    Button myBtn = myView.findViewById(R.id.thebuttonid);

    // use onClickListener here to handle the button

    return myView;
}
我想您在
MainActivity
类中处理
按钮
,这可能不起作用


您使用xml中的
onClick
来处理
按钮
,因此,将当前代码放在
片段
内和
onCreateView
外或任何其他方法:

public void click3(View v) {
    switch(v.getId()) {
        // do your stuff here
    }
} 

按钮在片段xml中,不在主活动中。我想您是在主活动中处理
按钮的。这就是为什么我首先尝试在fragment类中处理按钮,但当它不起作用时,我将它放在主活动中,然后只有它起作用,所以我的答案是正确的。如果将
按钮
放置在
片段
中,只需处理
按钮
并在
片段
中创建方法即可。很简单,对吗?:)当我把按钮放在碎片里时,它不起作用