gpt4 book ai didi

java - 应用程序在模拟器中崩溃

转载 作者:行者123 更新时间:2023-12-01 11:25:38 27 4
gpt4 key购买 nike

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

重要的代码:

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' 赋值”。

日志猫:

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

最佳答案

由于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);
}


});

关于java - 应用程序在模拟器中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30824656/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com