0

我有一个包含 onCreate 和 OnResume 的应用程序,如下所示:

菜单活动

public class MenuActivity extends Activity   {
    AdView  adView ;
    TextView tv;
    int weekSince;
    int numberOfDaysSince;
    static String DueDateChanged = "com.app.antiwal7amel.DueDateChanged";

    @Override   
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activitymain);

        tv = (TextView)findViewById(R.id.tv);
      adView = new AdView   (this, AdSize.BANNER , "a15013b6f790555");

         AdRequest adRequest = new AdRequest();
         adRequest.addTestDevice(AdRequest.TEST_EMULATOR);

         RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
         RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT);
         relativeParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

         adView.loadAd(adRequest);

//         this.addContentView(adView, relativeParams);
         RelativeLayout rootView = (RelativeLayout)findViewById(R.id.rl);
         rootView.addView(adView, relativeParams);

        ImageButton menu1 = (ImageButton)findViewById(R.id.menu1);
        ImageButton menu2 = (ImageButton)findViewById(R.id.menu2);
        ImageButton menu3 = (ImageButton)findViewById(R.id.menu3);
       ImageButton menu4 = (ImageButton)findViewById(R.id.menu4);
       ImageButton menu5 = (ImageButton)findViewById(R.id.menu5);
       ImageButton menu6 = (ImageButton) findViewById(R.id.menu6);
       ImageButton menu7 = (ImageButton) findViewById(R.id.menu7);
       ImageButton menu9 = (ImageButton)findViewById(R.id.menu9);
       ImageButton menu10 = (ImageButton)findViewById(R.id.menu10);
       ImageButton menu11 = (ImageButton)findViewById(R.id.menu11);

       SharedPreferences periodDatePreferences = PreferenceManager.getDefaultSharedPreferences(this);
         int chosenDay= periodDatePreferences.getInt("chosenDay", 0);
         int chosenMonth =  periodDatePreferences.getInt("chosenMonth", 0);
        int chosenYear =   periodDatePreferences.getInt("chosenYear", 0);
          System.err.println("Shared Preferences main menu "+chosenYear+chosenMonth+chosenDay);


       menu1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            Intent i = new Intent(MenuActivity.this,birthDate.class);
            startActivity(i);
                  }

        });


       menu2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            Intent i = new Intent(MenuActivity.this,You.class);
            startActivity(i);
                  }

        });

       menu3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            Intent i = new Intent(MenuActivity.this,YourBaby.class);
            startActivity(i);
                  }

        });

       menu4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            Intent i = new Intent(MenuActivity.this,TataworatYawmeeh.class);
            startActivity(i);
                  }

        });


       menu5.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            Intent i = new Intent(MenuActivity.this,DoctorVisitsActivity.class);
            startActivity(i);
                  }

        });

       menu6.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        Intent i = new Intent(MenuActivity.this,QuestionsActivity.class);
        startActivity(i);
              }

    });


       menu7.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        Intent i = new Intent(MenuActivity.this,AdviceActivity.class);
        startActivity(i);
              }

    });



       menu9.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            Intent i = new Intent(MenuActivity.this,AboutUsActivity.class);
            startActivity(i);
                  }

        });


      menu10.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Dialog settingsDialog = new Dialog(MenuActivity.this); 
            settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
            settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.buttonlayout, null)); 
            settingsDialog.show();

            }
    });

      menu11.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MenuActivity.this,Settings.class);
                startActivity(i);
                }
        });
        }

//  @Override
//  protected void onResume() {
//      // TODO Auto-generated method stub
//      
//      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
//      int birthChosenYear = prefs.getInt("birthChosenYear", MODE_APPEND);
//      int birthChosenMonth = prefs.getInt("birthChosenMonth", MODE_APPEND);
//      int birthChosenDay = prefs.getInt("birthChosenDay", MODE_APPEND);
//      System.err.println("Shared preferences data " + birthChosenDay + "/"
//              + birthChosenMonth + "/" + birthChosenYear);
//      
//      Calendar birthDate = Calendar.getInstance();
//      birthDate.set(birthChosenYear, birthChosenMonth - 1, birthChosenDay);
//      Calendar todayDate = Calendar.getInstance();
//
//      Date todayDated = todayDate.getTime();
//      Date birthDated = birthDate.getTime();
//      SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
//
//      long dd = birthDated.getTime() - todayDated.getTime();
//      long weeksTillBirth = dd / (1000 * 60 * 60 * 24 * 7);
//
//      System.err.println("todayDateeeeeeee = "
//              + dateFormat.format(todayDated.getTime()));
//      System.err.println("birthDateeeeeee = "
//              + dateFormat.format(birthDated.getTime()));
//      weekSince = (int) (40 - weeksTillBirth);
//      System.err.println("WeeksTillBirth = " + weeksTillBirth);
//      System.err.println("weekSince  = " + weekSince);
//      numberOfDaysSince = weekSince * 7;
//      System.err.println("You are in day  " + numberOfDaysSince);
//
//      
//      System.err.println("onResume");
//      Intent intent = getIntent();
//      String message = intent.getStringExtra("DueDateChanged");
//      System.err.println("message"+message);
//      tv.setText("               You are in day "+numberOfDaysSince + " From your "+weekSince+" Week");
//      super.onResume();
//
//
//  }

当我从 onResume 中删除注释标志时,这会导致我的 XML 中的图标变大,并且布局中的所有内容都变得凌乱!但是当我评论 onResume 里面的每一件事时,每一件事都像布局一样好!

我想知道他为什么会这样!

活动主.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/rl"

    android:orientation="vertical" android:background="@drawable/mainbackgroundiphone">

 <TableLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true"
     android:background="@drawable/mainbackgroundipad"
     android:orientation="vertical" >

      <TableRow
      android:layout_width="fill_parent"
           android:layout_height="fill_parent" 
           android:layout_marginTop="50dp"> 
      <TextView android:id="@+id/tv"
          android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:textColor="#FFFFFF"/>

      </TableRow>

        <TableRow android:id="@+id/tableRow1" 
            android:layout_marginTop="10dp">

            <ImageButton
                android:id="@+id/menu1"
                android:layout_width="75dip"
                android:layout_height="74dip"
                android:layout_marginTop="0dp"
                android:background="@android:color/transparent"
                android:scaleType="fitXY"
                android:src="@drawable/menu1" />

            <ImageButton
                android:id="@+id/menu2"
                android:layout_width="75dp"
                android:layout_height="59dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="1dp"
                android:background="@android:color/transparent"
                android:scaleType="fitXY"
                android:src="@drawable/menu2" />

            <ImageButton
                android:id="@+id/menu3"
                android:layout_width="75dip"
                android:layout_height="63dip"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1dp"
                android:background="@android:color/transparent"
                android:scaleType="fitXY"
                android:src="@drawable/menu3" />

            <ImageButton
                android:id="@+id/menu4"
                android:layout_width="60dp"
                android:layout_height="83dp"
                android:layout_marginRight="100dp"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:src="@drawable/menu4" />
        </TableRow>

        <TableRow android:id="@+id/tableRow2"
            android:layout_marginTop="0dp" >

            <ImageButton
                android:id="@+id/menu5"
                android:layout_width="50dp"
                android:layout_height="75dp"
                android:layout_marginLeft="12dp"
                android:layout_marginTop="60dp"
                android:background="@android:color/transparent"
                android:scaleType="fitXY"
                android:src="@drawable/menu5" />

            <ImageButton
                android:id="@+id/menu6"
                android:layout_width="60dp"
                android:layout_height="79dp"
                android:layout_marginTop="60dp"
                android:background="@android:color/transparent"
                android:scaleType="fitXY"
                android:src="@drawable/menu6" />

            <ImageButton
                android:id="@+id/menu7"
                android:layout_width="60dip"
                android:layout_height="86dip"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="60dp"
                android:background="@android:color/transparent"
                android:scaleType="fitStart"
                android:src="@drawable/menu7" />

            <ImageButton
                android:id="@+id/menu8"
                android:layout_width="75dip"
                android:layout_height="50dip"
                android:layout_centerVertical="true"
                android:layout_marginRight="110dp"
                android:layout_marginTop="80dp"
                android:background="@android:color/transparent"
                android:scaleType="fitXY"
                android:src="@drawable/menu8" />
        </TableRow>

        <TableRow android:id="@+id/tableRow3" android:layout_marginTop="1dp" 
           >

            <ImageButton
                android:id="@+id/menu9"
                android:layout_width="60px"
                android:layout_height="80px"
                android:layout_marginLeft="2dp"
                android:layout_marginTop="40dp"
                android:background="@android:color/transparent"
                android:scaleType="fitXY"
                android:src="@drawable/menu9" />

            <ImageButton
                android:id="@+id/menu10"
                android:layout_width="75dip"
                android:layout_height="56dip"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="2dp"
                android:layout_marginTop="40dp"
                android:background="@android:color/transparent"
                android:scaleType="fitXY"
                android:src="@drawable/menu10" />

            <ImageButton
                android:id="@+id/menu11"
                android:layout_width="75dip"
                android:layout_height="65dip"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="2dp"
                android:layout_marginTop="40dp"
                android:background="@android:color/transparent"
                android:scaleType="fitXY"
                android:src="@drawable/menu11" />

            <ImageButton
                android:id="@+id/menu12"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_marginRight="100dp"
                android:layout_marginTop="40dp"
                android:background="@android:color/transparent"
                android:scaleType="fitXY"
                android:src="@drawable/menu12" />
        </TableRow>
    </TableLayout>

</RelativeLayout>

这就是 onResume 没有评论时的样子!!

在此处输入图像描述

4

1 回答 1

0

我认为你应该先打电话给超级!给你;

  @Override

protected void onResume() { TODO 自动生成的方法存根 super.onResume(); SharedPreferences 首选项 = PreferenceManager.getDefaultSharedPreferences(this); intbirthChosenYear = prefs.getInt("birthChosenYear", MODE_APPEND); intbirthChosenMonth = prefs.getInt("birthChosenMonth", MODE_APPEND); intbirthChosenDay = prefs.getInt("birthChosenDay", MODE_APPEND); System.err.println("共享偏好数据" +birthChosenDay +"/"+birthChosenMonth +"/"+birthChosenYear);

  Calendar birthDate = Calendar.getInstance();
  birthDate.set(birthChosenYear, birthChosenMonth - 1, birthChosenDay);
  Calendar todayDate = Calendar.getInstance();

  Date todayDated = todayDate.getTime();
  Date birthDated = birthDate.getTime();
  SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

  long dd = birthDated.getTime() - todayDated.getTime();
  long weeksTillBirth = dd / (1000 * 60 * 60 * 24 * 7);

  System.err.println("todayDateeeeeeee = "
          + dateFormat.format(todayDated.getTime()));
  System.err.println("birthDateeeeeee = "
          + dateFormat.format(birthDated.getTime()));
  weekSince = (int) (40 - weeksTillBirth);
  System.err.println("WeeksTillBirth = " + weeksTillBirth);
  System.err.println("weekSince  = " + weekSince);
  numberOfDaysSince = weekSince * 7;
  System.err.println("You are in day  " + numberOfDaysSince);


  System.err.println("onResume");
  Intent intent = getIntent();
  String message = intent.getStringExtra("DueDateChanged");
  System.err.println("message"+message);
  tv.setText("               You are in day "+numberOfDaysSince + " From your "+weekSince+" Week");

}

于 2013-01-13T14:10:34.623 回答