gpt4 book ai didi

java - 对话框正按钮onClick调用接口(interface)时出现NullPointerException

转载 作者:行者123 更新时间:2023-11-29 21:36:24 24 4
gpt4 key购买 nike

我正在尝试使用接口(interface)从 AlertDialog.Builder.setPositiveButton 的 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

实现接口(interface)的Activity:

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 View 的方式导致了此 NPE?

我是 android/java 和一般编程的新手,感谢您的帮助。

最佳答案

mListener 为空

它为空,因为您从未将它分配给任何东西! (哦,是的,我听到你在想!)但你没有

您有一个 public voidonAttache(Activity activity)` 方法,该方法永远不会被调用,因为它应该被调用

public void onAttach(Activity activity)

您还应该为继承的方法添加一个@Override 并调用super。这将确保在方法声明错误时出现编译错误,并确保运行基类继承方法中的任何代码。即使该方法在您使用的 SDK 版本中为空,也不意味着它在将来的某个未知点不会为空。

关于java - 对话框正按钮onClick调用接口(interface)时出现NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18326804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com