Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android单击事件错误_Java_Android_Events_Click - Fatal编程技术网

Java Android单击事件错误

Java Android单击事件错误,java,android,events,click,Java,Android,Events,Click,当我激活clickListener时,我出现了一个奇怪的错误 尝试调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.view$OnClickListener)”的操作 关于空对象引用 @覆盖 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); 如果(Build.VERSION.SDK_INT

当我激活clickListener时,我出现了一个奇怪的错误

尝试调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.view$OnClickListener)”的操作 关于空对象引用

@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(Build.VERSION.SDK_INT<16){
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG\u discouse\u KEYGUARD);
setContentView(R.layout.activity_main);
WebView WebView=(WebView)findViewById(R.id.WebView);
加载数据(“测试”、“文本/html”、“utf-8”);
webView.loadUrl(“https://www.google.de/");
webView.getSettings().setDomStorageEnabled(true);
setKioskModeActive(true,getApplicationContext());
}
@凌驾
public void onBackPressed(){
Context=getApplicationContext();
CharSequence text=“停用模式的密码!”;
int duration=Toast.LENGTH\u SHORT;
Toast.makeText(上下文、文本、持续时间).show();
myDialog=新建对话框(此对话框);
myDialog.setContentView(R.layout.dialog\u sign);
myDialog.setCancelable(false);
密码=(EditText)myDialog.findViewById(R.id.password);
myDialog.show();
//错误可能是因为这个
按钮lbtn=(按钮)findViewById(R.id.loginButton);
lbtn.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
if(password.getText().toString().equals(“123”)){
Log.d(“myapp”、“test1”);
}否则{
Log.d(“myapp”、“test2”);
}
}
});
}
因此,当我点击后退按钮时,基本上会出现一个文本字段窗口对话框。在这里面我检查123的密码是否正确

这是我的对话框_sign.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>

您正在尝试在活动或片段布局中查找按钮,而不是在对话框中:

    Button lbtn = (Button)findViewById(R.id.loginButton);
应该是

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

您正在尝试在活动或片段布局中查找按钮,而不是在对话框中:

    Button lbtn = (Button)findViewById(R.id.loginButton);
应该是

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

您需要从它所在的视图中获取
按钮。如果在没有任何视图引用的情况下使用
findViewById
,那么它将尝试在活动xml中查找视图,在本例中为
activity\u main.xml
loginButton
不在此xml中,而是在您创建的对话框中,这就是您获得NPE的原因。所以,改变

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


您需要从它所在的视图中获取
按钮。如果在没有任何视图引用的情况下使用
findViewById
,那么它将尝试在活动xml中查找视图,在本例中为
activity\u main.xml
loginButton
不在此xml中,而是在您创建的对话框中,这就是您获得NPE的原因。所以,改变

Button lbtn = (Button)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");
            }
        }
    });

将代码替换为以下内容:

@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");
            }
        }
    });

请同时发布你的onCreate方法。请同时发布你的onCreate方法。很高兴我能帮忙。如果这对你有用,请不要忘记接受答案。很高兴我能帮上忙。如果这对你有效,请不要忘记接受答案。