gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:35:57 24 4
gpt4 key购买 nike

因此,我在仅使用一个 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);


}
});

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

简易类和普通类都有 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 是问题的原因

更新:

list :

类的声明如下:

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

</activity>

最佳答案

您需要在 AndroidManifest 中声明您的 Activity,该错误是特定的:它正在尝试查找名为“PractEnd”的 Activity,但您的 Manifest 中只声明了“EasyMain”。

关于java - Android:多个类使用相同的 XML 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25592628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com