Android Dialog.show返回badtoken错误的代码
我建立了一个预算跟踪系统来跟踪我的开支 我在应用程序的某些部分有一个对话框弹出选项,在我升级android studio之前,这些都可以正常工作 现在我在dialog.show代码运行时遇到了一个错误,它与窗口的实例化方式有关,有人能帮忙吗 下面是代码片段Android Dialog.show返回badtoken错误的代码,android,dialog,Android,Dialog,我建立了一个预算跟踪系统来跟踪我的开支 我在应用程序的某些部分有一个对话框弹出选项,在我升级android studio之前,这些都可以正常工作 现在我在dialog.show代码运行时遇到了一个错误,它与窗口的实例化方式有关,有人能帮忙吗 下面是代码片段 private void checkforexbase() throws IOException { String pathcat=Environment.getExternalStorageDirectory()+"/"+ap
private void checkforexbase() throws IOException {
String pathcat=Environment.getExternalStorageDirectory()+"/"+appfolder+"/";
File file=new File(pathcat+"forex.txt");
if(file.exists())Toast.makeText(ctx,"Base set",Toast.LENGTH_LONG).show();
else {
Locale loc = Locale.getDefault();
Currency currency = Currency.getInstance(loc);
final String currentcurr=currency.getCurrencyCode().toString();
WindowManager windowManager;
int layoutParams=WindowManager.LayoutParams.TYPE_TOAST;
final Dialog forexch =new Dialog(ctx);//-> dialog initiated
View view= View.inflate(ctx,R.layout.forxchoose,null);
view.inflate(ctx,R.layout.forxchoose,null);
forexch.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//forexch.getWindow().setType(Window.FEATURE_CONTENT_TRANSITIONS);
forexch.getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
TextView currtex=(TextView) view.findViewById(R.id.currencytex);
forexch.setContentView(view);
final Spinner spinner = (Spinner) view.findViewById(R.id.forexsp);
final ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(ctx,
R.array.forexlis, android.R.layout.simple_spinner_item);
spinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view,
int i, long l) {
if(forxspinfirstrun==true)forexch.dismiss();
else forxspinfirstrun=true;
String array[] = getResources().getStringArray(R.array.forexlis);
basecurr=array[i].toString();
forcurr=basecurr;
try {
writecurrencybase();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Toast.makeText(ctx, array[i].toString(),Toast.LENGTH_LONG ).show();
}
public void onNothingSelected(AdapterView<?> adapterView) {
forxspinfirstrun=false;
//Toast.makeText(ctx,"Select a currency",Toast.LENGTH_LONG).show();
return;
}
});
currtex.setText(currentcurr);
final Button buttonok=(Button)view.findViewById(R.id.buttonokforx);
final Button buttonchange=(Button)view.findViewById(R.id.buttonchangeforx);
buttonok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
basecurr=currentcurr;
//Toast.makeText(ctx,"Base currency set",Toast.LENGTH_LONG).show();
forexch.dismiss();
try {
writecurrencybase();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
buttonchange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonok.setVisibility(View.INVISIBLE);
buttonchange.setOnClickListener(null);
spinner.setVisibility(View.VISIBLE);adapter.setDropDownViewResource(android.R.layout.select_dialog_multichoice);
adapter.setDropDownViewResource(R.layout.forexspinlay);
spinner.setAdapter(adapter);
}
});
forexch.show();//->this is the crash point
}
}
我猜有更好的方法来实现对话。
该功能主要检查这是否是应用程序第一次启动,如果是,则弹出一个对话框选择基础货币
forexch.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
并使用此权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
这不起作用,在我更新android studio之前它一直在工作,这是错误->,java.lang.RuntimeException:无法启动活动组件信息{com.spendo.sam.spendo/com.spendo.sam.spendo.mainscreen}:android.view.WindowManager$BadTokenException:无法添加窗口android.view.ViewRootImpl$W@c63bde6--窗口类型2003的权限被拒绝
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
final Dialog forexch =new Dialog(getApplicationContext());//-> dialog initiated