2

タブ付きアクティビティのアクティビティ グループの子アクティビティに 2 つの日付ピッカーを表示する必要があります。日付を表示する 2 つのテキストビューと to ボタンのコードは次のとおりです。

  incorp_date=(TextView)findViewById(R.id.edt_incorpdate);
    incorp_date_image=(Button)findViewById(R.id.incorp_date);


    incorp_date_cal=Calendar.getInstance();

    incorp_date_image.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            showDialog(DATE_PICKER_INCORP);
        }
    });

    final Calendar c = Calendar.getInstance();
    incorp_year = c.get(Calendar.YEAR);
    incorp_month = c.get(Calendar.MONTH);
    incorp_day = c.get(Calendar.DAY_OF_MONTH);

    /* display the current date (this method is below)  */
    updateIncorpDisplay();


    estb_date=(TextView)findViewById(R.id.edt_estabdate);
    estb_date_image=(Button)findViewById(R.id.estb_date);
    estb_date_cal=Calendar.getInstance();

    estb_date_image.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            showDialog(DATE_PICKER_ESTB);
        }
    });
    final Calendar c1 = Calendar.getInstance();
    estb_year = c1.get(Calendar.YEAR);
    estb_month = c1.get(Calendar.MONTH);
    estb_day = c1.get(Calendar.DAY_OF_MONTH);

    /* display the current date (this method is below)  */
    updateEstbDisplay();

日付ピッカー ダイアログを表示するコードは次のとおりです。

incorp_dateListener=new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            // TODO Auto-generated method stub
            incorp_year = year;
            incorp_month = monthOfYear;
            incorp_day = dayOfMonth;
            updateIncorpDisplay();
        }
    };

    estb_dateListener=new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            // TODO Auto-generated method stub
            estb_year = year;
            estb_month = monthOfYear;
            estb_day = dayOfMonth;
            updateEstbDisplay();
        }
    };
  @Override
protected Dialog onCreateDialog(int id) {

    switch(id){
        case DATE_PICKER_INCORP:
                return new DatePickerDialog(getParent(), incorp_dateListener, incorp_year, incorp_month, incorp_day); 
            case DATE_PICKER_ESTB:
                return new DatePickerDialog(getParent(), estb_dateListener, estb_year, estb_month, estb_day);
    }
        return null;
}

日付ピッカー ダイアログを表示できません。ボタンをクリックするとアプリケーションが終了し、BadTokenException の例外が発生します..どうすればよいですか??? 問題が何であるかを理解できないようです??? おそらくそれはアクティビティグループの子アクティビティであるためだと思います..しかし、関連する解決策が見つかりません..助けてください!!!!

4

2 に答える 2

1

このようなことを試してください。まず、 TabActivityクラスにtabcontextオブジェクトを作成します。このように

package com.loanreminder;

import android.app.TabActivity;
import android.os.Bundle;

/**
 * @author Adil Soomro
 * 
 */
public class TabSample extends TabActivity {
    /** Called when the activity is first created. */
    public static TabSample tabContext;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabhost);
        tabContext = this;

    }

}

あなたの活動でこのようなオブジェクトを使用した後。

protected Dialog onCreateDialog(int id) {

    switch(id){
        case DATE_PICKER_INCORP:
                return new DatePickerDialog(TabSample.tabContext, incorp_dateListener, incorp_year, incorp_month, incorp_day); 
            case DATE_PICKER_ESTB:
                return new DatePickerDialog(TabSample.tabContext, estb_dateListener, estb_year, estb_month, estb_day);
    }
        return null;
}
于 2013-05-22T09:23:47.630 に答える