我試圖用AlertDialog.Builder.setPositiveButton的onClick事件發送一個單擊事件的接口。NullPointerException當對話框正面的按鈕onClick調用接口
請幫我一把。我一直在尋找類似的問題來指導。
的NPE是在這一行:
mListener.onSetBalanceDialogPositiveClick(SetBalanceDialog.this);
這裏是logcat的:
08-19 20:05:50.469: E/AndroidRuntime(5795): FATAL EXCEPTION: main
08-19 20:05:50.469: E/AndroidRuntime(5795): java.lang.NullPointerException
08-19 20:05:50.469: E/AndroidRuntime(5795): at com.smb.allowance.SetBalanceDialog$2.onClick(SetBalanceDialog.java:75)
08-19 20:05:50.469: E/AndroidRuntime(5795): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
08-19 20:05:50.469: E/AndroidRuntime(5795): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 20:05:50.469: E/AndroidRuntime(5795): at android.os.Looper.loop(Looper.java:123)
08-19 20:05:50.469: E/AndroidRuntime(5795): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-19 20:05:50.469: E/AndroidRuntime(5795): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 20:05:50.469: E/AndroidRuntime(5795): at java.lang.reflect.Method.invoke(Method.java:507)
08-19 20:05:50.469: E/AndroidRuntime(5795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
08-19 20:05:50.469: E/AndroidRuntime(5795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
08-19 20:05:50.469: E/AndroidRuntime(5795): at dalvik.system.NativeStart.main(Native Method)
這裏是對話的代碼:
package com.smb.allowance;
import java.text.DecimalFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
public class SetBalanceDialog extends DialogFragment implements OnEditorActionListener {
private EditText editTextSetBalance;
private static double dInput = 0;
public interface SetBalanceDialogListener{
public void onSetBalanceDialogPositiveClick(DialogFragment dialog);
} // end interface
SetBalanceDialogListener mListener;
public void onAttache(Activity activity){
super.onAttach(activity);
try {
mListener = (SetBalanceDialogListener) activity;
}catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement SetBalanceDialogListener");
}
} // end onAttach
public SetBalanceDialog(){
// default empty constructor
}// end constructor
public Dialog onCreateDialog(Bundle savedInstanceState){
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_balance_set, null);
editTextSetBalance = (EditText)view.findViewById(R.id.editTextSetBalance);
editTextSetBalance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!editTextIsEmpty(editTextSetBalance)){
editTextSetBalance.setText("");
}
clearEditText(editTextSetBalance);
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view)
.setCancelable(false)
.setMessage(R.string.set_begin_balance)
.setCancelable(false)
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mListener.onSetBalanceDialogPositiveClick(SetBalanceDialog.this);
}
});
// show soft keyboard automatically
editTextSetBalance.requestFocus();
editTextSetBalance.setOnEditorActionListener(this);
return builder.create();
} // end onCreateDialog
實現接口的活動:
package com.smb.allowance;
import java.text.DecimalFormat;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import com.smb.allowance.ResetBalanceDialogFragment.ResetBalanceDialogListener;
public class MainActivity extends FragmentActivity
implements ResetBalanceDialogListener, SetBalanceDialog.SetBalanceDialogListener {
@Override
public void onSetBalanceDialogPositiveClick(DialogFragment dialog) {
// For testing functionality
onReset();
}
是否有可能我處理/錯誤處理EditText視圖導致這個NPE?
我是新來的android/java和編程一般,感謝任何幫助。
你在哪裏執行那個接口? –
我已經將實現界面的活動部分添加到了我的問題中。 – PitterPatter
你有答案:) Infact兩個答案完全同時發佈,告訴你同樣的東西:)選擇你的選擇並接受其中的一個。 – jamesc