当前位置: 首页 > 面试题库 >

片段中的功能会使模拟器崩溃

彭鸿文
2023-03-14
问题内容

当我把一个函数内发生的错误onCreateViewmenu1_Fragment.java。抱歉,我也将放置我的日志,因此它可能会使我的帖子过大,所以我将告诉您布局。我很抱歉有很多问题。

如果我只是不将函数setClick()放在片段中,则代码可以完美工作。快速说明该功能基本上使我的图像处于menu1_layout.xml3向按钮(也称为开始停止重置)的位置,当您重置时,它将弹出您确切时间的提示

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)日志

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>

问题答案:

您无法使用getActivity()from,onCreateView()因为尚未创建片段的活动。您应该使用not use
getActivity()直到onActivityCreated()被调用,或者更好的方法是使用rootview膨胀的findViewByID()电话进行调用。

此外,您正在初始化按钮三次。您应该摆脱其中的两个:)



 类似资料:
  • 问题内容: 我正在通过编写一个小型个人项目来学习Go。即使很小,我还是决定从头开始进行严格的单元测试,以学习Go的良好习惯。 琐碎的单元测试都很好而且花哨的,但是我现在对依赖项感到困惑;我希望能够用模拟函数替换一些函数调用。这是我的代码片段: 我希望能够测试downloader()而不实际通过http获取页面- 即通过模拟get_page(更容易使用,因为它仅将页面内容作为字符串返回)或http.

  • 问题内容: 我正在尝试使用python模拟模块的函数(返回一些外部内容)。 我在导入模块的模拟函数时遇到了一些麻烦。 例如,在我有 我想嘲笑,以便它返回其他内容。 我正在尝试: 如果在另一个模块中被调用,它实际上似乎永远不会返回模拟对象。我在使用方法方面缺少什么吗? 请注意,如果我调用以下内容,则一切正常: 但是,如果从另一个模块内部调用,它将调用原始函数而不是模拟版本: 内容 所以我想我的问题是

  • 我被代码卡住了,无法继续。模拟器崩溃,堆栈指向第47行。请帮助初学者! 堆栈错误: 致命的例外:主java。lang.NumberFormatException:无效的int:“t”在java中。整型。invalidInt(Integer.java:138)

  • 我想在地图上画一条折线 进口com.google.android.gms.maps.model.Polyline;进口com.google.android.gms.maps.model.PolylineOptions; 导入java.util。ArrayList;导入java.util.List; 公共类 MainActivity extends AppCompatActivity 实现了 OnM

  • 问题内容: 如何在MySQL中的此表上做滞后处理以打印引号中的差异,例如: 问题答案: 这是我最喜欢的MySQL hack。 这是模拟滞后函数的方式: 保存上一行报价的值。对于第一行,@ quot是-1。 保留当前行的报价的值。 笔记: 子句在这里很重要,就像在常规窗口函数中一样。 您可能还想使用lag 来确保计算相同引号的差异。 您也可以以相同的方式实现行计数器 与该方案相比,与使用聚合函数,存

  • 问题内容: 首先,我是和的新手。 我有一个实例化的类,我想测试一下。 这是我的代码: 我想测试一下我的功能。我的头,我认为测试fs.existsSync的状态是个好主意。如果返回,则必须调用。所以我尝试写一些测试: 但是,我有一个错误: 您能帮我调试和测试我的功能吗? 问候。 问题答案: 出现错误是因为它正在寻找在您的对象上调用的方法,该方法不存在。如果您可以访问测试中的模块,则可以监视如下方法: