Я искал много и не нашел ничего, чтобы предотвратить эту ошибку. Я делаю простое приложение секундомера, оно сбой в эмуляторе, когда я пытаюсь его протестировать.Ошибка приложения в эмуляторе
Код, который имеет значение:
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
пожалуйста, вы можете разместить свой 'activity_my.xml' макет? – OleGG
@OleGG У меня есть 2 файла XML. У меня есть Activity_my.XML, который почти пуст, и фрагмент, который я использую, который имеет все проекты. Что вы хотите? – user3289794
Если у вас есть весь макет в фрагменте, вы должны манипулировать им содержимым внутри класса фрагмента. Вы пытаетесь найти представление, которое принадлежит фрагменту из активности. Когда вызывается метод действия onCreate, фрагменты еще не создаются, поэтому 'findViewById' возвращает null. Это дает вам NPE, когда вы пытаетесь установить прослушиватель кликов. – OleGG