应用程序在模拟器中崩溃

时间:2015-06-13 23:44:12

标签: java android

我搜索了很多,但没有找到任何可以防止此错误的内容。我正在制作一个简单的秒表应用程序,当我尝试测试它时它会在模拟器中崩溃。

重要的代码:

public class MyActivity extends Activity implements ActionBar.TabListener {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v13.app.FragmentStatePagerAdapter}.
     */

    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        Button start;
        final TextView stopwatch_num;
        final double mcount = 0.0;
        start = (Button)findViewById(R.id.start_button);
        stopwatch_num = (TextView)findViewById(R.id.timer_num);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mcount = mcount + .01;
                stopwatch_num.setText(""+mcount);
            }


        });

在onclicklistener中,在第一个mcount上,它表示“无法为最终变量'mcount'赋值”。

logcat:

06-13 19:29:04.280      851-851/martinproductions.time_me D/dalvikvm﹕ GC_FOR_ALLOC freed 60K, 10% free 3452K/3804K, paused 36ms, total 38ms
06-13 19:29:04.480      851-851/martinproductions.time_me D/dalvikvm﹕ GC_FOR_ALLOC freed 3K, 9% free 3670K/4028K, paused 21ms, total 21ms
06-13 19:29:04.500      851-851/martinproductions.time_me I/dalvikvm-heap﹕ Grow heap (frag case) to 6.283MB for 2536936-byte allocation
06-13 19:29:04.540      851-861/martinproductions.time_me D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 6% free 6148K/6508K, paused 37ms, total 37ms
06-13 19:29:04.720      851-851/martinproductions.time_me D/AndroidRuntime﹕ Shutting down VM
06-13 19:29:04.720      851-851/martinproductions.time_me W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1ab0ba8)
06-13 19:29:04.730      851-851/martinproductions.time_me E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: martinproductions.time_me, PID: 851
    java.lang.RuntimeException: Unable to start activity ComponentInfo{martinproductions.time_me/martinproductions.time_me.MyActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at martinproductions.time_me.MyActivity.onCreate(MyActivity.java:49)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
06-13 19:29:18.550      851-851/martinproductions.time_me I/Process﹕ Sending signal. PID: 851 SIG: 9
device not found

更新的logcat:

06-13 19:56:50.810      987-987/martinproductions.time_me D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
06-13 19:56:53.550      987-987/martinproductions.time_me D/dalvikvm﹕ GC_FOR_ALLOC freed 71K, 10% free 3452K/3812K, paused 36ms, total 38ms
06-13 19:56:53.690      987-987/martinproductions.time_me D/dalvikvm﹕ GC_FOR_ALLOC freed 3K, 10% free 3670K/4036K, paused 19ms, total 20ms
06-13 19:56:53.710      987-987/martinproductions.time_me I/dalvikvm-heap﹕ Grow heap (frag case) to 6.283MB for 2536936-byte allocation
06-13 19:56:53.740      987-996/martinproductions.time_me D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 6% free 6148K/6516K, paused 27ms, total 27ms
06-13 19:56:53.890      987-987/martinproductions.time_me D/AndroidRuntime﹕ Shutting down VM
06-13 19:56:53.890      987-987/martinproductions.time_me W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a8fba8)
06-13 19:56:53.900      987-987/martinproductions.time_me E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: martinproductions.time_me, PID: 987
    java.lang.RuntimeException: Unable to start activity ComponentInfo{martinproductions.time_me/martinproductions.time_me.MyActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at martinproductions.time_me.MyActivity.onCreate(MyActivity.java:38)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
06-13 19:56:57.120      987-987/martinproductions.time_me I/Process﹕ Sending signal. PID: 987 SIG: 9
06-13 19:57:03.190    1030-1030/martinproductions.time_me D/dalvikvm﹕ GC_FOR_ALLOC freed 79K, 10% free 3452K/3820K, paused 102ms, total 104ms
06-13 19:57:03.520    1030-1030/martinproductions.time_me D/dalvikvm﹕ GC_FOR_ALLOC freed 3K, 10% free 3670K/4044K, paused 20ms, total 21ms
06-13 19:57:03.540    1030-1030/martinproductions.time_me I/dalvikvm-heap﹕ Grow heap (frag case) to 6.283MB for 2536936-byte allocation
06-13 19:57:03.580    1030-1039/martinproductions.time_me D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 6% free 6148K/6524K, paused 33ms, total 33ms
06-13 19:57:03.730    1030-1030/martinproductions.time_me D/AndroidRuntime﹕ Shutting down VM
06-13 19:57:03.730    1030-1030/martinproductions.time_me W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a8fba8)
06-13 19:57:03.740    1030-1030/martinproductions.time_me E/AndroidRuntime﹕ FATAL EXCEPTION: main

1 个答案:

答案 0 :(得分:1)

由于mcount是最终的,因此只能分配一次。您可以mcount成为班级MyActivity的成员,因此可以在OnClickListener内访问,而不是最终版。

代码应如下所示:

public class MyActivity extends Activity implements ActionBar.TabListener {

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
double mcount;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Button start;
    final TextView stopwatch_num;
    mcount = 0.0;
    start = (Button)findViewById(R.id.start_button);
    stopwatch_num = (TextView)findViewById(R.id.timer_num);
    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mcount = mcount + .01;
            stopwatch_num.setText(""+mcount);
        }


    });