我正在制作一个游戏,我正在制作一个有3种不同视角的LevelSelect课程(监狱,院子和沙子),我曾经有多个意图,但如果我这样做,没有好办法关闭我的应用程序,它不会很好地返回。
我也试过使用setContentView()
方法,但是当它转出来时它无法加载能够胜任视图的类,然后我尝试组合类,但是类不能初始化对象外部正在进行的当前观点
所以我的问题是:
如何在不启动新活动的情况下使用多个视图?
package com.muttgames.shadowpuzzle;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class LevelSelect extends Activity implements OnClickListener{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.jail_level_view);
//Defining Inflater
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Defining sandView layout
RelativeLayout jailView = (RelativeLayout) findViewById(R.id.jail_level);
View innerViewsJailView = inflater.inflate(R.id.yard_level, jailView, true);
View jailMenu = innerViewsJailView.findViewById(R.id.jail2menu);
jailMenu.setOnClickListener(this);
//Defining sandView layout
RelativeLayout yardView = (RelativeLayout) findViewById(R.id.yard_level);
View innerViewsYardView = inflater.inflate(R.id.yard_level, yardView, true);
View yardMenu = innerViewsYardView.findViewById(R.id.yard2menu);
yardMenu.setOnClickListener(this);
//Defining sandView layout
RelativeLayout sandView = (RelativeLayout) findViewById(R.id.sand_level);
View innerViewsSandView = inflater.inflate(R.id.sand_level, sandView, true);
View sandMenu = innerViewsSandView.findViewById(R.id.sand2menu);
sandMenu.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.jail2menu:
finish();
break;
case R.id.yard2menu:
finish();
break;
case R.id.sand2menu:
finish();
break;
}
}
}
我确实知道这是一个非常小的课程,但我会做一些小试用,以防止我不得不一次又一次地编写大量的代码。
这部分的错误是我初始化yardMenu和sandMenu按钮,而它们不在R.layout.jail_level_view
xml文件中。
此代码用于在有人想要返回main_activity
时停止水平选择活动。
我怎么能改变它,而不是关闭活动,我可以把它改成activity_main
xml文件。
xml文件(它们看起来几乎都是一样的,颜色的微调等......:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/jail_level"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_bg"
android:orientation="vertical" >
<TextView
android:id="@+id/view_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Jail Levels"
android:textSize="30sp" />
<Button
android:id="@+id/level1"
style="@drawable/jail_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/view_title"
android:layout_marginLeft="45dp"
android:layout_marginTop="30dp"
android:background="@drawable/jail_button"
android:text="1" />
<Button
android:id="@+id/level2"
style="@drawable/jail_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/view_title"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:layout_toRightOf="@id/level1"
android:background="@drawable/jail_button"
android:text="2" />
<Button
android:id="@+id/level3"
style="@drawable/jail_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/view_title"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:layout_toRightOf="@id/level2"
android:background="@drawable/jail_button"
android:text="3" />
<Button
style="@drawable/yard_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_marginLeft="225dp"
android:background="@drawable/yard_button"
android:text="Next" />
<Button
android:id="@+id/jail2menu"
style="@drawable/menu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_marginLeft="122dp"
android:background="@drawable/menu_button"
android:text="Menu" />
</RelativeLayout>
logcat的:
02-14 12:27:58.309: E/AndroidRuntime(8647): FATAL EXCEPTION: main
02-14 12:27:58.309: E/AndroidRuntime(8647): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.muttgames.shadowpuzzle/com.muttgames.shadowpuzzle.LevelSelect}: android.content.res.Resources$NotFoundException: Resource ID #0x7f08000d type #0x12 is not valid
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.os.Looper.loop(Looper.java:137)
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-14 12:27:58.309: E/AndroidRuntime(8647): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 12:27:58.309: E/AndroidRuntime(8647): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 12:27:58.309: E/AndroidRuntime(8647): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-14 12:27:58.309: E/AndroidRuntime(8647): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-14 12:27:58.309: E/AndroidRuntime(8647): at dalvik.system.NativeStart.main(Native Method)
02-14 12:27:58.309: E/AndroidRuntime(8647): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f08000d type #0x12 is not valid
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2144)
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.content.res.Resources.getLayout(Resources.java:853)
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
02-14 12:27:58.309: E/AndroidRuntime(8647): at com.muttgames.shadowpuzzle.LevelSelect.onCreate(LevelSelect.java:25)
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.app.Activity.performCreate(Activity.java:5104)
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-14 12:27:58.309: E/AndroidRuntime(8647): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-14 12:27:58.309: E/AndroidRuntime(8647): ... 11 more
答案 0 :(得分:0)
您需要布局inflater和容器。 例如,将“sandMenu”设为LinearLayout,然后使用代码:
sandMenu.removeAllViews();
LayoutInflater inflater = (LayoutInflater) .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View innerViewsSandMenu = inflater.inflate(R.layout.yourLAYOUT, sandMenu, true);
之后您可以通过以下方式访问sandMenu的内部视图:
innerViewsSandMenu.findViewById(R.id.....