如何解决这个问题:无法添加 window -- 令牌 null 不适用于应用程序
How to solve this: Unable to add window -- token null is not for an application
我在Whosebug中多次发现这个问题,但我找不到类似mine.So的问题,我不得不问这个问题。
我在viewpager中有一个自定义按钮,当我按下这个按钮时,会出现一个对话框。
但它总是崩溃,因为 无法添加 window -- 令牌 null 不适用于应用程序
谁能帮我解决这个问题,非常感谢!
下面是自定义按钮:
public class RecordButton extends CircleButton {
....
private Dialog recordIndicator;
private void initDialogStartRecord() {
startTime = System.currentTimeMillis();
recordIndicator = new Dialog(context,
R.style.like_toast_dialog_style);
recordIndicator.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
imageView = new ImageView(getContext());
imageView.setImageResource(res[0]);
recordIndicator.setContentView(
imageView,
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
recordIndicator.setOnDismissListener(onDismiss);
WindowManager.LayoutParams lp = recordIndicator.getWindow().getAttributes();
lp.gravity = Gravity.CENTER;
startRecording();
recordIndicator.show();
}
这是 viewpager:
public Object instantiateItem(ViewGroup container, int position) {
View viewLayout = inflater.inflate(R.layout.item_photoword,
container, false);
ImageView imageView = (ImageView) viewLayout.findViewById(R.id.iv_photoword);
Bitmap bitmap = ImageUtil.decodeBitmapFromFile(pathList.get(position), AppConstant.SCREEN_WIDTH, 50);
imageView.setImageBitmap(bitmap);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP)
RecordButton button = (RecordButton) viewLayout.findViewById(R.id.bt_record);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Test", Toast.LENGTH_LONG).show();
}
});
FileUtil.getFileFolder("/Myapp/Record/");
String path = "/Myapp/Record/" + System.currentTimeMillis() + ".amr";
System.out.println("path------>" + path);
button.setSavePath(path);
button.setOnFinishedRecordListener(new RecordButton.OnFinishedRecordListener() {
@Override
public void onFinishedRecord(String audioPath) {
System.out.println("audiopath:---->" + audioPath);
}
});
((ViewPager) container).addView(viewLayout, 0);
return viewLayout;
}
这是context
问题。在这里,
中的“getContext()”是什么
AlertDialog.Builder builder = new AlertDialog.Builder.(getContext());
使用Activity上下文。这将解决您的问题,在调用自定义按钮时将 context
从您的 activity 传递到该自定义按钮,然后使用该 context
创建 Dialog
.
在创建Dialog和ImageView时你必须通过Activity 引用不是 Context
因此,如果您在 Activity
中进行更改,请更改以下行
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
imageView = new ImageView(getContext());
到
AlertDialog.Builder builder = new AlertDialog.Builder(this);
imageView = new ImageView(this);
(或)
如果你在 Fragment
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
imageView = new ImageView(getActivity());
希望对您有所帮助。
我在Whosebug中多次发现这个问题,但我找不到类似mine.So的问题,我不得不问这个问题。
我在viewpager中有一个自定义按钮,当我按下这个按钮时,会出现一个对话框。
但它总是崩溃,因为 无法添加 window -- 令牌 null 不适用于应用程序
谁能帮我解决这个问题,非常感谢!
下面是自定义按钮:
public class RecordButton extends CircleButton {
....
private Dialog recordIndicator;
private void initDialogStartRecord() {
startTime = System.currentTimeMillis();
recordIndicator = new Dialog(context,
R.style.like_toast_dialog_style);
recordIndicator.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
imageView = new ImageView(getContext());
imageView.setImageResource(res[0]);
recordIndicator.setContentView(
imageView,
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
recordIndicator.setOnDismissListener(onDismiss);
WindowManager.LayoutParams lp = recordIndicator.getWindow().getAttributes();
lp.gravity = Gravity.CENTER;
startRecording();
recordIndicator.show();
}
这是 viewpager:
public Object instantiateItem(ViewGroup container, int position) {
View viewLayout = inflater.inflate(R.layout.item_photoword,
container, false);
ImageView imageView = (ImageView) viewLayout.findViewById(R.id.iv_photoword);
Bitmap bitmap = ImageUtil.decodeBitmapFromFile(pathList.get(position), AppConstant.SCREEN_WIDTH, 50);
imageView.setImageBitmap(bitmap);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP)
RecordButton button = (RecordButton) viewLayout.findViewById(R.id.bt_record);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Test", Toast.LENGTH_LONG).show();
}
});
FileUtil.getFileFolder("/Myapp/Record/");
String path = "/Myapp/Record/" + System.currentTimeMillis() + ".amr";
System.out.println("path------>" + path);
button.setSavePath(path);
button.setOnFinishedRecordListener(new RecordButton.OnFinishedRecordListener() {
@Override
public void onFinishedRecord(String audioPath) {
System.out.println("audiopath:---->" + audioPath);
}
});
((ViewPager) container).addView(viewLayout, 0);
return viewLayout;
}
这是context
问题。在这里,
AlertDialog.Builder builder = new AlertDialog.Builder.(getContext());
使用Activity上下文。这将解决您的问题,在调用自定义按钮时将 context
从您的 activity 传递到该自定义按钮,然后使用该 context
创建 Dialog
.
在创建Dialog和ImageView时你必须通过Activity 引用不是 Context
因此,如果您在 Activity
中进行更改,请更改以下行AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
imageView = new ImageView(getContext());
到
AlertDialog.Builder builder = new AlertDialog.Builder(this);
imageView = new ImageView(this);
(或)
如果你在 Fragment
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
imageView = new ImageView(getActivity());
希望对您有所帮助。