功能的片段,使模拟器崩溃模拟器、片段、功能

2023-09-07 12:03:06 作者:藏清欢

当我把在 menu1_Fragment.java onCreateView 函数发生错误。对不起事先我会放在我的日志中,以及因此它可能使我的文章有点大,所以我会告诉你的布局。我是新的遗憾问题的载荷。

在code完美的作品,如果我不把功能 setClick()我的片段中。快速注的功能基本上使得我的形象在 menu1_layout.xml 3的方式按钮又名开始停止复位就是当你重新设置,它会弹出你有确切的时间

1) menu1_Fragment.java

 公共类menu1_Fragment扩展片段{
    天文台mChronometer;
    INT循环= 0;
    双重的startTime;
    双米利斯;

    查看rootview;
    @Nullable
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        rootview = inflater.inflate(R.layout.menu1_layout,集装箱,假);
        setClick();
        返回rootview;
    }



    Button按钮;
    公共无效setClick(){


        mChronometer =(天文台)getActivity()findViewById(R.id.aChronometer)。

        按钮=(按钮)getActivity()findViewById(R.id.stopWatch)。
        button.setOnClickListener(mStartListener);

        按钮=(按钮)getActivity()findViewById(R.id.stopWatch)。
        button.setOnClickListener(mStartListener);

        按钮=(按钮)getActivity()findViewById(R.id.stopWatch)。
        button.setOnClickListener(mStartListener);
    }

    View.OnClickListener mStartListener =新View.OnClickListener(){
        公共无效的onClick(视图v){
            如果(循环== 0){
                mChronometer.setBase(SystemClock.elapsedRealtime());
                mChronometer.start();
                的startTime = System.currentTimeMillis的();
                环++;
            }否则如果(环== 1){
                mChronometer.stop();
                米利斯= System.currentTimeMillis的() -  startTime时;
                环++;
            }否则,如果(循环== 2){
                mChronometer.setBase(SystemClock.elapsedRealtime());
                上下文的背景下= getActivity()getApplicationContext()。
                CharSequence的文字=你的时间是:+(米利斯/ 1000);
                INT持续时间= Toast.LENGTH_SHORT;

                吐司面包= Toast.makeText(背景,文本,持续时间);
                toast.show();
                循环= 0;
            }
        }
    };
}
 

2)日志

  10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu D / AndroidRuntime:关闭虚拟机
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:致命异常:主要
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:工艺:app.z0nen.menu,PID:2912
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:java.lang.RuntimeException的:无法启动的活动ComponentInfo {app.z0nen.menu/app.z0nen.slidemenu.MyActivity}:JAVA。 lang.NullPointerException:尝试调用虚拟方法无效android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'对空对象引用
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.ActivityThread.-wrap11(ActivityThread.java)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:102)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.os.Looper.loop(Looper.java:148)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.ActivityThread.main(ActivityThread.java:5417)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在java.lang.reflect.Method.invoke(本机方法)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:致:显示java.lang.NullPointerException:尝试调用虚拟方法无效android.widget.Button.setOnClickListener(android.view.View $一个空对象引用OnClickListener)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在app.z0nen.slidemenu.menu1_Fragment.setClick(menu​​1_Fragment.java:39)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在app.z0nen.slidemenu.menu1_Fragment.onCreateView(menu​​1_Fragment.java:26)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.Fragment.performCreateView(Fragment.java:2220)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.BackStackRecord.run(BackStackRecord.java:793)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.FragmentController.execPendingActions(FragmentController.java:325)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.Activity.performStart(Activity.java:6252)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.ActivityThread.-wrap11(ActivityThread.java)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:102)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.os.Looper.loop(Looper.java:148)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在android.app.ActivityThread.main(ActivityThread.java:5417)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在java.lang.reflect.Method.invoke(本机方法)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
10-28 16:52:26.552 2912年至2912年/ app.z0nen.menu E / AndroidRuntime:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-28 16:52:28.662 2912年至2912年/? I /工艺:发送信号。 PID:2912 SIG:9
 
MINE模拟器使用教程带崩溃处理教程

3) MyActivity.Java

 公共类MyActivity扩展活动
        实现NavigationDrawerFragment.NavigationDrawerCallbacks {

    / **
     *片段管理导航抽屉的行为,相互作用和presentation。
     * /
    私人NavigationDrawerFragment mNavigationDrawerFragment;

    / **
     *用于存储最后屏幕标题。对于使用{@link #restoreActionBar()}。
     * /
    私人的CharSequence mTitle;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_my);

        mNavigationDrawerFragment =(NavigationDrawerFragment)
                。getFragmentManager()findFragmentById(R.id.navigation_drawer);
        mTitle =的getTitle();

        //设置抽屉。
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout)findViewById(R.id.drawer_layout));
    }

    @覆盖
    公共无效onNavigationDrawerItemSelected(INT位置){

        片段objFragment = NULL;

        开关(位置){
            情况下0:
                objFragment =新menu1_Fragment();
                打破;
            情况1:
                objFragment =新menu2_Fragment();
                打破;
        }
        //更新替换片段的主要内容
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container,objFragment)
                。承诺();
    }

    公共无效onSectionAttached(INT编号){
        开关(数){
            情况1:
                mTitle =的getString(R.string.title_section1);
                打破;
            案例2:
                mTitle =的getString(R.string.title_section2);
                打破;
        }
    }

    公共无效restoreActionBar(){
        动作条动作条= getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(真正的);
        actionBar.setTitle(mTitle);
    }


    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        如果(!mNavigationDrawerFragment.isDrawerOpen()){
            //只显示有关该屏幕中的操作栏项目
            //如果抽屉没有显示。否则,让抽屉
            //决定如何操作栏中显示。
            。getMenuInflater()膨胀(R.menu.my,菜单);
            restoreActionBar();
            返回true;
        }
        返回super.onCreateOptionsMenu(菜单);
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。将操作栏
        //自动在主/向上按钮操作的点击,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT的id = item.getItemId();

        // noinspection SimplifiableIfStatement
        如果(ID == R.id.action_settings){
            返回true;
        }

        返回super.onOptionsItemSelected(项目);
    }

    / **
     *包含一个简单的视图的占位符片段。
     * /
    公共静态类PlaceholderFragment扩展片段{
        / **
         *片段参数重新presenting本章节号
         * 分段。
         * /
        私有静态最后弦乐ARG_SECTION_NUMBER =section_number标;

        / **
         *对于给定的部分中返回此片段的一个新实例
         * 数。
         * /
        公共静态PlaceholderFragment的newInstance(INT sectionNumber){
            PlaceholderFragment片段=新PlaceholderFragment();
            捆绑的args =新包();
            args.putInt(ARG_SECTION_NUMBER,sectionNumber);
            fragment.setArguments(参数);
            返回片段;
        }

        公共PlaceholderFragment(){
        }

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.fragment_my,集装箱,假);
            返回rootView;
        }

        @覆盖
        公共无效onAttach(活动活动){
            super.onAttach(活动);
            ((MyActivity)活动).onSectionAttached(
                    。getArguments()调用getInt(ARG_SECTION_NUMBER));
        }
    }

}
 

4) Menu1_layout.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent机器人:layout_height =match_parent
    机器人:重力=center_horizo​​ntal>

    <天文台表
        机器人:ID =@ + ID / aChronometer
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =40SP
        机器人:文字颜色=#111111
        机器人:layout_marginTop =27dp
        机器人:layout_below =@ + ID /秒表
        机器人:layout_centerHorizo​​ntal =真/>

    <按钮
        机器人:ID =@ + ID /秒表
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:背景=@可绘制/ stopwatch2
        机器人:layout_marginTop =38dp
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真/>

< / RelativeLayout的>
 

解决方案

您无法使用 getActivity() onCreateView()该片段的行为还没有被创建。你应该使用不使用 getActivity()直到 onActivityCreated()被调用,或者更好的是,使用 rootview 你虚增做你的 findViewByID()的电话。

此外,要初始化你的按钮三次。你应该摆脱对其中的两个:)

Error occurs when I place a function inside onCreateView in menu1_Fragment.java. Sorry in advance as I will be placing my logs in as well so it might make my post a bit big so I will tell you the layout. I am new sorry for the loads of questions.

The code works perfectly if I just don't put the function setClick() inside my fragment. quick note the function basically makes my image in menu1_layout.xml a 3 way button aka start stop reset where when you reset, it pops up a toast of exact time you got

1) menu1_Fragment.java

public class menu1_Fragment extends Fragment {
    Chronometer mChronometer;
    int loop = 0;
    double startTime;
    double millis;

    View rootview;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.menu1_layout, container, false);
        setClick();
        return rootview;
    }



    Button button;
    public void setClick() {


        mChronometer = (Chronometer) getActivity().findViewById(R.id.aChronometer);

        button = (Button) getActivity().findViewById(R.id.stopWatch);
        button.setOnClickListener(mStartListener);

        button = (Button) getActivity().findViewById(R.id.stopWatch);
        button.setOnClickListener(mStartListener);

        button = (Button) getActivity().findViewById(R.id.stopWatch);
        button.setOnClickListener(mStartListener);
    }

    View.OnClickListener mStartListener = new View.OnClickListener() {
        public void onClick(View v) {
            if (loop == 0) {
                mChronometer.setBase(SystemClock.elapsedRealtime());
                mChronometer.start();
                startTime = System.currentTimeMillis();
                loop++;
            } else if (loop == 1) {
                mChronometer.stop();
                millis = System.currentTimeMillis()-startTime;
                loop++;
            } else if (loop == 2) {
                mChronometer.setBase(SystemClock.elapsedRealtime());
                Context context = getActivity().getApplicationContext();
                CharSequence text = "Your Time is: " + (millis/1000);
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
                loop = 0;
            }
        }
    };
}

2) LOGS

10-28 16:52:26.552 2912-2912/app.z0nen.menu D/AndroidRuntime: Shutting down VM
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime: FATAL EXCEPTION: main
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime: Process: app.z0nen.menu, PID: 2912
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{app.z0nen.menu/app.z0nen.slidemenu.MyActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at app.z0nen.slidemenu.menu1_Fragment.setClick(menu1_Fragment.java:39)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at app.z0nen.slidemenu.menu1_Fragment.onCreateView(menu1_Fragment.java:26)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.Fragment.performCreateView(Fragment.java:2220)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.BackStackRecord.run(BackStackRecord.java:793)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.FragmentController.execPendingActions(FragmentController.java:325)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.Activity.performStart(Activity.java:6252)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
10-28 16:52:26.552 2912-2912/app.z0nen.menu E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
10-28 16:52:28.662 2912-2912/? I/Process: Sending signal. PID: 2912 SIG: 9

3) MyActivity.Java

public class MyActivity extends Activity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

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

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {

        Fragment objFragment = null;

        switch (position) {
            case 0:
                objFragment = new menu1_Fragment();
                break;
            case 1:
                objFragment = new menu2_Fragment();
                break;
        }
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, objFragment)
                .commit();
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.my, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @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();

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

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        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;
        }

        public PlaceholderFragment() {
        }

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

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MyActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }

}

4) Menu1_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <Chronometer
        android:id="@+id/aChronometer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:textColor="#111111"
        android:layout_marginTop="27dp"
        android:layout_below="@+id/stopWatch"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/stopWatch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/stopwatch2"
        android:layout_marginTop="38dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

解决方案

You can't use getActivity() from onCreateView() as the fragment's activity has not been created yet. You should use not use getActivity() until onActivityCreated() is called, or better yet, use the rootview you inflated to do your findViewByID() calls.

Also, you are initializing your button three times. You should get rid on two of them :)