我有一个类似于对话框的类:
interface DiagInterface{
void selectionDone(boolean accepted);
}
public class CardSelectionDialog extends Dialog {
private android.view.View.OnClickListener positive;
private android.view.View.OnClickListener negative;
private boolean AcceptState;
private DiagInterface ExtComm;
public CardSelectionDialog(Context context){
super(context);
AcceptState = false;
positive = new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
AcceptState = true;
OnSelectionDone();
dismiss();
}
};
negative= new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
AcceptState = false;
OnSelectionDone();
dismiss();
}
};
}
private void OnSelectionDone(){
System.err.println("Before selection");
ExtComm.selectionDone(AcceptState);
}
然后在我的主要课程上,我有以下内容:
这是我调用对话框(MainActivity)的地方:
public void SelectCardDialog(){
CardSelectionDialog diag = new CardSelectionDialog(this);
CardDeck deck = new CardDeck();
deck.addNOfCard(CardType.WOUNDCARD, 30, this);
diag.setSelection(deck);
diag.show();
}
这是interfase的实现:
@Override
public void selectionDone(boolean accepted){
if (accepted){
System.err.println("ACCEPTED");
}
else{
System.err.println("REJECTED");
}
}
然而,每当我按下任何按钮时,我都会收到以下错误(出现选择前信息)
01-01 16:59:42.180: E/AndroidRuntime(29810): java.lang.NullPointerException
01-01 16:59:42.180: E/AndroidRuntime(29810): at legen.dary.CardSelectionDialog.OnSelectionDone(CardSelectionDialog.java:106)
01-01 16:59:42.180: E/AndroidRuntime(29810): at legen.dary.CardSelectionDialog.access$1(CardSelectionDialog.java:103)
01-01 16:59:42.180: E/AndroidRuntime(29810): at legen.dary.CardSelectionDialog$1.onClick(CardSelectionDialog.java:35)
01-01 16:59:42.180: E/AndroidRuntime(29810): at android.view.View.performClick(View.java:4084)
01-01 16:59:42.180: E/AndroidRuntime(29810): at android.view.View$PerformClick.run(View.java:16966)
01-01 16:59:42.180: E/AndroidRuntime(29810): at android.os.Handler.handleCallback(Handler.java:615)
01-01 16:59:42.180: E/AndroidRuntime(29810): at android.os.Handler.dispatchMessage(Handler.java:92)
01-01 16:59:42.180: E/AndroidRuntime(29810): at android.os.Looper.loop(Looper.java:137)
01-01 16:59:42.180: E/AndroidRuntime(29810): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-01 16:59:42.180: E/AndroidRuntime(29810): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 16:59:42.180: E/AndroidRuntime(29810): at java.lang.reflect.Method.invoke(Method.java:511)
01-01 16:59:42.180: E/AndroidRuntime(29810): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-01 16:59:42.180: E/AndroidRuntime(29810): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-01 16:59:42.180: E/AndroidRuntime(29810): at dalvik.system.NativeStart.main(Native Method)
我做错了什么想法?
非常感谢.....
答案 0 :(得分:1)
您尚未初始化
private DiagInterface ExtComm;
你可能想要
public CardSelectionDialog(Context context){
super(context);
ExtComm= (DiagInterface)context;
AcceptState = false;
}
此外,您可能希望在构造函数外部移动
positive = new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
AcceptState = true;
OnSelectionDone();
dismiss();
}
};
negative= new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
AcceptState = false;
OnSelectionDone();
dismiss();
}
};
答案 1 :(得分:0)
我认为这个NPE异常来自这一行:
ExtComm.selectionDone(AcceptState);
可能ExtComm为空