我搜索了很多並且沒有發現任何內容來防止此錯誤。我正在製作一個簡單的秒錶應用程序,當我嘗試測試它時,它正在模擬器中崩潰。應用程序在模擬器中崩潰
的事項代碼:
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