我正在使用Tabs,并且也使用单个Tab进行多个活动。
我遇到了一个问题,即当我从父类转到子类时,该类中有日期选择器,然后单击显示日期对话框的按钮,从中可以选择日期。但是,当我单击该按钮(触发日期对话框)时,我的应用程序崩溃了
当以下
“无法启动组件信息”
需要帮助解决此问题
这是子活动代码
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class PlaceOrder extends Activity {
private TextView tvDisplayDate,personName,Cname,cAdrress;
private DatePicker dpResult;
private Button btnChangeDate;
Button cancel;
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.placeorder);
personName=(TextView)findViewById(R.id.firstName);
Cname=(TextView)findViewById(R.id.Cname);
cAdrress=(TextView)findViewById(R.id.CAddname);
personName.setText(AccountInfo.fnameNlName);
Cname.setText(AccountInfo.cName);
cAdrress.setText(AccountInfo.cAdd);
setCurrentDateOnView();
addListenerOnButton();
cancel=(Button)findViewById(R.id.Cancel);
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(FastPkgMainActivity.fastPackge==1)
{
Intent fastPkgQuote = new Intent(v.getContext(), FastPkgQuote.class);
StringBuffer urlString = new StringBuffer();
FastPkgQuote parentActivity = (FastPkgQuote)getParent();
parentActivity.replaceContentView("fastpkg1", fastPkgQuote);
}
else if(FastPkgMainActivity.fastPackge==2)
{
Intent fastpkgQuoteNew = new Intent(v.getContext(), FastPkgQuoteNew.class);
StringBuffer urlString = new StringBuffer();
FastPkgQuoteNew parentActivity = (FastPkgQuoteNew)getParent();
parentActivity.replaceContentView1("fastPkg2", fastpkgQuoteNew);
}
}
});
}
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
dpResult = (DatePicker) findViewById(R.id.dpResult);
dpResult.setVisibility(View.INVISIBLE);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
tvDisplayDate.setText(new StringBuilder()
.append(month + 1).append("-").append(day).append("-").append(year).append(" "));
dpResult.init(year, month, day, null);
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
public Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, datePickerListener, year, month,day);
}
return null;
}
public DatePickerDialog.OnDateSetListener datePickerListener = new
DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay)
{
year = selectedYear;
month = selectedMonth;
day = selectedDay;
tvDisplayDate.setText(new StringBuilder().append(month + 1).append("-").append(day).append("-").append(year).append(" "));
// set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
}
}
这是logcat的结果
FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException:
Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44ef0838
is not valid; is your activity running?
android.view.ViewRoot.setView(ViewRoot.java:505)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.view.Window$LocalWindowManager.addView(Window.java:424)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.app.Dialog.show(Dialog.java:241)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.app.DatePickerDialog.show(DatePickerDialog.java:129)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.app.Activity.showDialog(Activity.java:2556)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.app.Activity.showDialog(Activity.java:2514)
04-20 15:50:06.703: E/AndroidRuntime(323): at corrculator.fastPkg.PlaceOrder$3.onClick(PlaceOrder.java:108)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.view.View.performClick(View.java:2408)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.view.View$PerformClick.run(View.java:8816)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.os.Handler.handleCallback(Handler.java:587)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.os.Looper.loop(Looper.java:123)
04-20 15:50:06.703: E/AndroidRuntime(323): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-20 15:50:06.703: E/AndroidRuntime(323): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 15:50:06.703: E/AndroidRuntime(323): at java.lang.reflect.Method.invoke(Method.java:521)
04-20 15:50:06.703: E/AndroidRuntime(323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-20 15:50:06.703: E/AndroidRuntime(323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-20 15:50:06.703: E/AndroidRuntime(323): at dalvik.system.NativeStart.main(Native Method)
最佳答案
我相信问题可能出在这条线上:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
尝试将其修改为:
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivityName.this);
关于android - 无法启动组件信息DatePicker,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10242481/