打开NFC阅读器应用程序时出现IllegalStateException(已附加)

时间:2016-12-14 21:23:05

标签: android android-activity oncreate illegalstateexception activity-lifecycle

我正在尝试开发一个能够读取NFC标签并显示读取数据的应用。我从a blog获取了大部分代码,但是在运行应用程序时遇到错误,因为我试图将它与滑块应用程序集成。

以下是我的错误和我的代码:

INT_MAX

代码:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.jeffk11.tag, PID: 20123
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jeffk11.tag/com.example.jeffk11.tag.MainActivity}: java.lang.IllegalStateException: Already attached
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                   Caused by: java.lang.IllegalStateException: Already attached
                      at android.support.v4.app.FragmentManagerImpl.attachController(FragmentManager.java:2137)
                      at android.support.v4.app.FragmentController.attachHost(FragmentController.java:104)
                      at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:317)
                      at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:88)
                      at com.example.jeffk11.tag.MainActivity.onCreate(MainActivity.java:68)
                      at android.app.Activity.performCreate(Activity.java:6679)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

1 个答案:

答案 0 :(得分:1)

您可以在自己的viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); 方法中多次调用超类(onCreate)的AppCompatActivity方法。不要这样做。 onCreate必须只调用一次(通常这是在您的活动开始时super.onCreate()方法完成的。所以只需删除

的第二次出现
onCreate