Android 点击事件错误

Android Click Event Error

我在激活 clickListener 时遇到了这个奇怪的错误

Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT < 16) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

    setContentView(R.layout.activity_main);
    WebView webView = (WebView) findViewById(R.id.webView);
    webView.loadData("test ", "text/html", "utf-8");
    webView.loadUrl("https://www.google.de/");
    webView.getSettings().setDomStorageEnabled(true);
    PrefUtils.setKioskModeActive(true, getApplicationContext());

}
   @Override
    public void onBackPressed() {
        Context context = getApplicationContext();
        CharSequence text = "password to deactivate mode!";
        int duration = Toast.LENGTH_SHORT;
        Toast.makeText(context, text, duration).show();
        myDialog = new Dialog(this);
        myDialog.setContentView(R.layout.dialog_signin);
        myDialog.setCancelable(false);
        password = (EditText) myDialog.findViewById(R.id.password);
        myDialog.show();
        // Error probably because of this
        Button lbtn = (Button)findViewById(R.id.loginButton);
        lbtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (password.getText().toString().equals("123")) {
                    Log.d("myapp", "test1");
                } else {
                    Log.d("myapp", "test2");
                }
            }
        });
    }

所以当我单击后退按钮时,基本上会出现一个对话框文本字段 windows。在里面我检查123的密码是否正确。

这是我的 dialog_signin.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="16dp"
        android:fontFamily="sans-serif" />
        <!--android:hint="@string/password"-->
    <Button
        android:id="@+id/loginButton"
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:background="@color/red"/>
</LinearLayout>

这是我的 activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true">

    </WebView>
</RelativeLayout>

您正试图在 Activity 或 Fragment 布局中找到按钮,而不是在对话框中:

    Button lbtn = (Button)findViewById(R.id.loginButton);

应该是

    Button lbtn = (Button)myDialog.findViewById(R.id.loginButton);

您需要从它所在的视图中获取 Button。如果您使用 findViewById 而没有任何视图引用,那么它会尝试在您 activity xml 在这种情况下 activity_main.xmlloginButton 不在此 xml 中,但在您创建的对话框中,这就是您获得 NPE 的原因。所以,改变

Button lbtn = (Button)findViewById(R.id.loginButton);

Button lbtn = (Button)myDialog.findViewById(R.id.loginButton);

将您的代码替换为:

@Override
public void onBackPressed() {
    Context context = getApplicationContext();
    CharSequence text = "password to deactivate mode!";
    int duration = Toast.LENGTH_SHORT;
    Toast.makeText(context, text, duration).show();
    myDialog = new Dialog(this);
    myDialog.setContentView(R.layout.dialog_signin);
    myDialog.setCancelable(false);
    password = (EditText) myDialog.findViewById(R.id.password);
    myDialog.show();
    // Error probably because of this
    Button lbtn = (Button)myDialog.findViewById(R.id.loginButton);
    lbtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (password.getText().toString().equals("123")) {
                Log.d("myapp", "test1");
            } else {
                Log.d("myapp", "test2");
            }
        }
    });