[英]Android: Multiple Classes to use same XML Layout
So I am having some trouble using multiple classes with just the one xml file. 所以我在仅使用一个xml文件使用多个类时遇到了一些麻烦。 What I am trying to do is I have a difficultly selector the harder the difficultly the less time the user has in the game.
我想要做的是,我很难选择一个用户,游戏中花费的时间越少,就越难。
I have successfully set up my normal difficultly which loads from a button click with the xml file. 我已经成功地设置好了我的常规设置,该设置很难通过单击xml文件来加载。 To save making multiple xml files for easy and other difficulties I created a class with less time and linked it with
setContentView(R.layout.activity_main);
为了省去制作多个xml文件的麻烦和其他麻烦,我创建了一个用较少时间的类,并将其与
setContentView(R.layout.activity_main);
. 。 Only one class at a time seems to be able to have it's contentview set to one xml file.
一次只有一个类似乎可以将它的contentview设置为一个xml文件。 Upon clicking the easy button the app crashes even though the same code works for the normal difficultly.
单击“轻松”按钮后,即使相同的代码很难正常运行,应用也会崩溃。
the code that calls each class is as follows: 调用每个类的代码如下:
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);
}
});
This code works successfully but crashes on easy button press. 该代码可以成功运行,但是在按下按钮时会崩溃。
Both easy class and normal class have setContentView(R.layout.activity_main); 简单类和普通类都具有setContentView(R.layout.activity_main); in their respective on create.
在各自的创造上。
The log I have when the button causes the crash is as follows: 当按钮导致崩溃时,我的日志如下:
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)
How can I get it so all my classes can individual run on the same layout when called? 我如何获得它,以便我所有的类都可以在调用时分别在同一布局上运行? It's really confused me as the code work perfectly for one class but not the other thus leading me to believe the setContent is the cause of the issue
这真的让我感到困惑,因为该代码可以完美地在一个类上工作,但不能在另一个类上工作,因此使我相信setContent是问题的原因
UPDATE: 更新:
Manifest: 表现:
class is declared like so: 类的声明如下:
<activity
android:name=".EasyMain"
android:label="@string/app_name"
android:screenOrientation="portrait">
</activity>
您需要在AndroidManifest中声明您的Activity,该错误是特定的:它试图找到名为“ PractEnd”的Activity,但您在Manifest中仅声明了“ EasyMain”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.