Android单击按钮进入下一个活动,但冻结了布局

问题描述

| 我在每个布局上都有3个按钮。当我单击results.xml中的MOH按钮时,将调用MOHactivity并显示results_health布局。但是,当我想单击results_health中的任何按钮时,就像屏幕已冻结。但是,如果我单击“返回”按钮,它将返回我的主要活动。我不知道该怎么玩。谢谢。 ResultsActivity.java与Result_PDRM.java和Result_MOH.java(oncreate())相同
       ImageView myMOHButton = (ImageView) findViewById(R.id.MOH_Button);  
        myMOHButton.setonClickListener(new View.OnClickListener() {  
            public void onClick(View v)
            {  
                setContentView(R.layout.results_health);

            }  
        }); 

        ImageView myPDRMButton = (ImageView) findViewById(R.id.PDRM_Button);  
        myPDRMButton.setonClickListener(new View.OnClickListener() {  
            public void onClick(View v)
            {  
                setContentView(R.layout.results_pdrm);
            }  
        }); 

        ImageView myMainButton = (ImageView) findViewById(R.id.Main_Button);  
        myMainButton.setonClickListener(new View.OnClickListener() {  
            public void onClick(View v)
            {  
                setContentView(R.layout.main);

            }
        }
        ); 
result.xml也与results_health.xml和result_pdrm.xml相同(results_health没有MOH ImageView,results_pdrm没有PDRM ImageView)
        <?xml version=\"1.0\" encoding=\"utf-8\"?>
        <AbsoluteLayout
         android:id=\"@+id/widget56\"
         android:layout_width=\"fill_parent\"
         android:layout_height=\"fill_parent\"
         xmlns:android=\"http://schemas.android.com/apk/res/android\"
          android:background=\"@drawable/bgnd\">
        <TextView android:id=\"@+id/MOH\" android:textStyle=\"bold\" android:textColor=\"#ff00ff00\" android:text=\"Health Details\" android:layout_width=\"wrap_content\" android:textSize=\"25sp\" android:layout_height=\"wrap_content\" android:layout_x=\"6dip\" android:layout_y=\"9dip\"></TextView>
        <ImageView android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:id=\"@id/JPJ_Button\" android:src=\"@drawable/jpj1\" android:layout_y=\"425dip\" android:layout_x=\"2dip\"></ImageView>
        <ImageView android:layout_width=\"wrap_content\" android:id=\"@id/PDRM_Button\" android:layout_height=\"wrap_content\" android:src=\"@drawable/pdrm1\" android:layout_x=\"94dip\" android:layout_y=\"425dip\"></ImageView>
        <ImageView android:layout_width=\"wrap_content\" android:id=\"@id/JPN_Button\" android:layout_height=\"wrap_content\" android:src=\"@drawable/camera_48\" android:layout_x=\"186dip\" android:layout_y=\"425dip\"></ImageView>
      <ImageView android:layout_height=\"wrap_content\" android:layout_width=\"wrap_content\" android:id=\"@id/Main_Button\" android:src=\"@drawable/camera_small\" android:layout_x=\"277dip\" android:layout_y=\"425dip\"></ImageView>
       </AbsoluteLayout>
请帮忙。     

解决方法

        您在这里所做的是替换当前活动的XML布局。它似乎被冻结的原因是因为您在代码上打了一个新的UI,但是没有将任何ClickHandler附加到按钮上,因为在引擎盖下您仍在运行上一个活动中的代码。 您想要做的是从一项活动转到另一项活动。要进行其他活动,您需要声明一个意图。
ImageView myMainButton = (ImageView) findViewById(R.id.Main_Button);  
        myMainButton.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v)
            {  
                    Intent i = new Intent(ResultsActivity.this,Result_PDRM.class);
                    startActivityForResult(i,0);
            }
        }
        );
这会将您从ResultsActivity带到Result_PDRM。然后,在Result_PDRM中的onCreate中,调用:
setContentView(R.layout.results_pdrm);
确保将Result_PDRM中的clickHandlers连接到XML布局中的适当控件。     ,        我认为您最好使用
startActivity(new Intent(...))
,因为您的按钮(在旧版式中)仍在新版式中工作,所以您将更改版式(实际上是更改活动)