我建立了预算跟踪器来跟踪我的费用。
在应用程序的某些部分,我有一个对话框弹出选项,在我升级android studio之前,它们一直工作正常。
现在,在dialog.show代码运行时出现错误,这与实例化窗口的方式有关,任何主体都可以帮忙吗?
下面是代码段
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
}
}
这是logcat中的错误:
Process: com.spendo.sam.spendo, PID: 10874
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spendo.sam.spendo/com.spendo.sam.spendo.mainscreen}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? ---> ?_?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:765)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92)
at android.app.Dialog.show(Dialog.java:330)
at com.spendo.sam.spendo.mainscreen.checkforexbase(mainscreen.java:427)
我猜有更好的方法来实现对话框。 该功能基本上会检查这是否是应用程序的首次启动,如果是,则会弹出一个对话框以选择基本货币
答案 0 :(得分:0)
使用此
forexch.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
并使用此权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
并在初始化对话框时使用应用程序上下文
final Dialog forexch =new Dialog(getApplicationContext());//-> dialog initiated