Android:多个类使用相同的XML布局

时间:2014-08-31 13:53:21

标签: java android xml class share

所以我在使用多个类只有一个xml文件时遇到了一些麻烦。我想要做的是我有一个困难的选择器越困难越难用户在游戏中的时间越少。

我已经成功设置了我的正常困难,从xml文件的按钮单击加载。为了方便和其他困难保存多个xml文件,我创建了一个时间较少的类,并将其与setContentView(R.layout.activity_main);相关联。一次只有一个类似乎能够将它的contentview设置为一个xml文件。单击简单按钮后,即使相同的代码难以正常工作,应用程序也会崩溃。

调用每个类的代码如下:

 private void setButtonOnClickListeners(){
         normalBut.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                 Intent playgameN = new Intent(getApplicationContext(),MainActivity.class);
                    startActivity(playgameN);

            }
        });
         easyBut.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                     Intent playgameE = new Intent(getApplicationContext(),EasyMain.class);
                        startActivity(playgameE);


                }
            }); 

此代码可以成功运行,但在轻松按下按钮时会崩溃。

easy class和normal class都有setContentView(R.layout.activity_main);在他们各自的创造。

按钮导致崩溃时的日志如下:

01-07 20:29:48.630: I/Adreno200-EGLSUB(23442): <ConfigWindowMatch:2081>: Format RGBA_8888.
01-07 20:29:48.630: D/memalloc(23442): /dev/pmem: Mapped buffer base:0x51851000 size:4915200 offset:4300800 fd:67
01-07 20:29:48.780: D/memalloc(23442): /dev/pmem: Mapped buffer base:0x51f78000 size:1228800 offset:614400 fd:70
01-07 20:29:50.790: I/Adreno200-EGLSUB(23442): <ConfigWindowMatch:2081>: Format RGBA_8888.
01-07 20:29:50.800: D/memalloc(23442): /dev/pmem: Mapped buffer base:0x522a4000 size:1843200 offset:1228800 fd:73
01-07 20:29:50.820: D/memalloc(23442): /dev/pmem: Mapped buffer base:0x52466000 size:4300800 offset:3686400 fd:79
01-07 20:29:50.870: D/memalloc(23442): /dev/pmem: Unmapping buffer base:0x51851000 size:4915200 offset:4300800
01-07 20:29:50.870: D/memalloc(23442): /dev/pmem: Unmapping buffer base:0x51f78000 size:1228800 offset:614400
01-07 20:29:52.610: D/memalloc(23442): /dev/pmem: Mapped buffer base:0x51751000 size:1228800 offset:614400 fd:64
01-07 20:29:52.700: W/dalvikvm(23442): threadid=1: thread exiting with uncaught exception (group=0x40af09f0)
01-07 20:29:52.710: E/AndroidRuntime(23442): FATAL EXCEPTION: main
01-07 20:29:52.710: E/AndroidRuntime(23442): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.phil3992.colourguess/com.phil3992.colourguess.PractEnd}; have you declared this activity in your AndroidManifest.xml?
01-07 20:29:52.710: E/AndroidRuntime(23442):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at android.app.Activity.startActivityForResult(Activity.java:3195)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at android.app.Activity.startActivity(Activity.java:3302)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at com.phil3992.colourguess.dif$2.onClick(dif.java:38)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at android.view.View.performClick(View.java:3528)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at android.view.View$PerformClick.run(View.java:14235)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at android.os.Handler.handleCallback(Handler.java:605)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at android.os.Looper.loop(Looper.java:137)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at android.app.ActivityThread.main(ActivityThread.java:4424)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at java.lang.reflect.Method.invokeNative(Native Method)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at java.lang.reflect.Method.invoke(Method.java:511)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-07 20:29:52.710: E/AndroidRuntime(23442):    at dalvik.system.NativeStart.main(Native Method)

我如何才能获得它,以便我的所有类在调用时可以在相同的布局上运行?它真的让我感到困惑,因为代码完全适用于一个类而不是另一个类因此导致我相信setContent是问题的原因

更新:

清单:

类声明如下:

  <activity
            android:name=".EasyMain"
            android:label="@string/app_name" 
            android:screenOrientation="portrait">

        </activity>

1 个答案:

答案 0 :(得分:1)

你需要在AndroidManifest中声明你的Activity,错误是特定的:它试图找到名为“PractEnd”的Activity,但你只在你的Manifest中声明了“EasyMain”。