TextView&;在Android中添加日期选择器后按钮不显示

TextView&;在Android中添加日期选择器后按钮不显示,android,android-layout,datepicker,Android,Android Layout,Datepicker,我是Android新手。我有一个应用程序,可以用这个来选择用户的出生日期。当我添加此功能时,XML元素如TextView&Button不会显示。怎么可能呢?我可以知道实现我的目标的正确方法是什么吗?也许这个问题太基本了,但我没有找到合适的解决办法。请帮我解决 这是我的密码: public class UserDobActivity extends Activity { DatePicker datePickerBirthday; TextView textViewUserDate; @Ove

我是Android新手。我有一个应用程序,可以用这个来选择用户的出生日期。当我添加此功能时,XML元素如TextView&Button不会显示。怎么可能呢?我可以知道实现我的目标的正确方法是什么吗?也许这个问题太基本了,但我没有找到合适的解决办法。请帮我解决

这是我的密码:

public class UserDobActivity extends Activity {

DatePicker datePickerBirthday;
TextView textViewUserDate;

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



    ActionBar ab = getActionBar(); 
    ab.setDisplayUseLogoEnabled(false);
    ab.setDisplayShowHomeEnabled(true);
    ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    ab.setCustomView(R.layout.actionbar);
    ab.setDisplayHomeAsUpEnabled(true);
    ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#33CCFF"));     
    ab.setBackgroundDrawable(colorDrawable);

 // create the date picker
    datePickerBirthday = new DatePicker(this);

    // hide the whole calendar view (works in api 11 or greater)
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= 11) {
        datePickerBirthday.setCalendarViewShown(false);
    }
 // create the TextView
    textViewUserDate = new TextView(this);
    textViewUserDate.setGravity(Gravity.CENTER);

    // initialize the date to current date
    SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    String dateStr = sdfDateTime.format(new Date(System.currentTimeMillis()));

    String[] dateSplit = dateStr.split("-");
    int currentYear = Integer.parseInt(dateSplit[0]);
    int currentMonth = Integer.parseInt(dateSplit[1]);
    int currentDay = Integer.parseInt(dateSplit[2]);

    // to show date and day of week in the TextView
    setHumanReadableDate(currentYear, currentMonth, currentDay);

    // initialize date picker listener
    // currentMonth - 1, because on the picker, 0 is January
    datePickerBirthday.init(currentYear, currentMonth - 1, currentDay, birthdayListener);

    // add to the container
    LinearLayout linearLayoutCalTvContainer = new LinearLayout(this);
    linearLayoutCalTvContainer.setOrientation(LinearLayout.VERTICAL);
    linearLayoutCalTvContainer.addView(datePickerBirthday);
    linearLayoutCalTvContainer.addView(textViewUserDate);

 // set the views for the activity
    setContentView(linearLayoutCalTvContainer);

    RelativeLayout layoutCalTvContainer = (RelativeLayout) findViewById(R.id.layout);
    layoutCalTvContainer.addView(datePickerBirthday);
}

// the date picker listener
OnDateChangedListener birthdayListener = new OnDateChangedListener() {

    public void onDateChanged(DatePicker birthDayDatePicker,
            int newYear, int newMonth, int newDay) {

        try{

            // currentMonth + 1, to retrieve proper month
            setHumanReadableDate(newYear, newMonth + 1, newDay);

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};

// show the user a better date format
@SuppressLint("SimpleDateFormat") public void setHumanReadableDate(int newYear, int newMonth, int newDay){
    try {

        String clickedDate = newYear + "-" + newMonth + "-" + newDay;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = null;
        try {
            d = sdf.parse(clickedDate);
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        SimpleDateFormat sdfDateTime = new SimpleDateFormat("MMMM dd, yyyy 'is' EEEE", Locale.US);
        String dateStr = sdfDateTime.format(d);

        textViewUserDate.setText(dateStr);

    } catch (ParseException e) {
        e.printStackTrace();
    }
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.user_dob, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go home
            Intent intent = new Intent(this, UserDetailsActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
            default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onBackPressed() {
   moveTaskToBack(true); 
   UserDobActivity.this.finish();
}}
以下是XML:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@android:color/white">

   <TextView
        android:id="@+id/text_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/dobdetails"
        android:textSize="30sp"
        android:layout_marginTop="50dp" 
        android:gravity="center_horizontal" />


 <RelativeLayout
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >



</RelativeLayout>

    <Button
     android:id="@+id/ButtonUserDOB" 
     android:layout_width="match_parent"
     android:layout_height="60dp"
     android:text="@string/next"
     android:background="@drawable/customised_button_click"
     android:textSize="20sp"

      />


</LinearLayout>

您正在调用
setContentView(linearLayoutCalTvContainer)R.layout.activity\u user\u dob
设置为内容视图后,再次执行code>。这将覆盖以前设置的布局,该布局中包含的所有内容都将消失。为什么不单独用xml创建完整的布局?试着这样做:

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

    // Get the instances of your DatePicker and TextView from the layout
    DatePicker datePickerBirthday = findViewById(R.id.datePicker);
    TextView textViewUserDate = findViewById(R.id.textViewUserDate);

    // Continue with the rest of the setup
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= 11) {
        datePickerBirthday.setCalendarViewShown(false);
    }
    ...
}
将DatePicker和TextView放入您的布局中,无论它们位于何处:

<DatePicker
        android:id="@+id/datePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

<TextView 
        android:id="@+id/textViewUserDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
但是一定不要再调用
setContentView(…)最初将布局设置为
R.layout.activity\u user\u dob

Done(y)后。你的逻辑是正确的