从Activity中更改不同的包括Android中的ID

时间:2014-03-13 12:07:23

标签: android xml android-layout android-intent

有三种布局game_view.xmlgame_level_1.xmlgame_level_2.xml。当在Level布局中单击按钮时,game_level_1或game_level_2中的任何一个都包含在game_view中。

单击不同按钮时,我无法在不同级别之间切换。

当我运行应用时,未捕获的异常错误,并显示以下错误( android.widget.RelativeLayout cannot be cast to android.widget.TextView)

layout:game_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

  <include
         android:id="@+id/game_view_include"
         layout="@layout/game_level_2" />

  </RelativeLayout>

布局:game_level_1.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

        <ImageView
            android:id="@+id/level_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/shadow"
            android:contentDescription="@string/desc" />

   </RelativeLayout>

布局:game_level_2.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

        <ImageView
            android:id="@+id/level_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/shadow"
            android:contentDescription="@string/desc" />

   </RelativeLayout>

活动:LevelActivity(单击按钮1时,game_level_1应显示在布局中,单击按钮2时,应选择game_level_2)

 public class LevelActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.levels_view);

    //TODO: Add code for switching screens and starting a view and a controller.


    Button level1_button = (Button) findViewById(R.id.level1_button);
    level1_button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                //Move to the next view!
                Intent i = new Intent(LevelActivity.this, GameActivity.class);
                        i.putExtra("game_view_include", "level_1"); 
              startActivity(i);
            }
        }); 

    Button level2_button = (Button) findViewById(R.id.level2_button);
    level2_button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                //Move to the next view!
            Intent i = new Intent(LevelActivity.this, GameActivity.class);
                i.putExtra("game_view_include", "level_2");
              startActivity(i);
            }
        }); 
    }

 }

活动:GameActivity

  public class GameActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_view);

    //TODO: Add code for switching screens and starting a view and a controller.

    TextView game_view_include = (TextView) findViewById(R.id.game_view_include);

    Intent i = getIntent();
    // Receiving the Data
    final String game = i.getStringExtra("game_view_include");

    game_view_include.setText(game);

  }
}

logcat的

03-13 13:15:25.220: D/dalvikvm(23301): GC_FOR_ALLOC freed 42K, 1% free 16807K/16888K, paused 18ms, total 18ms
03-13 13:15:25.240: I/dalvikvm-heap(23301): Grow heap (frag case) to 22.590MB for 6442704-byte allocation
03-13 13:15:25.270: D/dalvikvm(23301): GC_FOR_ALLOC freed 47K, 1% free 23051K/23180K, paused 26ms, total 26ms
03-13 13:15:26.750: D/AndroidRuntime(23301): Shutting down VM
03-13 13:15:26.750: W/dalvikvm(23301): threadid=1: thread exiting with uncaught exception (group=0x41806ba8)
03-13 13:15:26.760: E/AndroidRuntime(23301): FATAL EXCEPTION: main
03-13 13:15:26.760: E/AndroidRuntime(23301): Process: se.kth.csc.iprog.matchme.android, PID: 23301
03-13 13:15:26.760: E/AndroidRuntime(23301): java.lang.RuntimeException: Unable to start activity ComponentInfo{se.kth.csc.iprog.matchme.android/se.kth.csc.iprog.matchme.android.GameActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
03-13 13:15:26.760: E/AndroidRuntime(23301):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at android.os.Looper.loop(Looper.java:136)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at android.app.ActivityThread.main(ActivityThread.java:5017)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at java.lang.reflect.Method.invoke(Method.java:515)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at dalvik.system.NativeStart.main(Native Method)
03-13 13:15:26.760: E/AndroidRuntime(23301): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
03-13 13:15:26.760: E/AndroidRuntime(23301):    at se.kth.csc.iprog.matchme.android.GameActivity.onCreate(GameActivity.java:21)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at android.app.Activity.performCreate(Activity.java:5231)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-13 13:15:26.760: E/AndroidRuntime(23301):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-13 13:15:26.760: E/AndroidRuntime(23301):    ... 11 more
03-13 13:15:29.920: D/dalvikvm(24842): GC_FOR_ALLOC freed 47K, 2% free 7677K/7764K, paused 21ms, total 21ms
03-13 13:15:29.940: I/dalvikvm-heap(24842): Grow heap (frag case) to 13.674MB for 6442704-byte allocation
03-13 13:15:29.960: D/dalvikvm(24842): GC_FOR_ALLOC freed 1K, 1% free 13967K/14056K, paused 20ms, total 20ms
03-13 13:15:30.180: D/libEGL(24842): loaded /system/lib/egl/libEGL_tegra.so
03-13 13:15:30.200: D/libEGL(24842): loaded /system/lib/egl/libGLESv1_CM_tegra.so
03-13 13:15:30.210: D/libEGL(24842): loaded /system/lib/egl/libGLESv2_tegra.so
03-13 13:15:30.230: D/OpenGLRenderer(24842): Enabling debug mode 0

0 个答案:

没有答案