onClick监听器和onClick on XML将无法正常工作

时间:2016-10-18 10:26:43

标签: java android button onclick crash

所以我在尝试使用sharedPreferences进行登录/注册时遇到了问题,如果我在onCreate方法内部使用onCickListener使用onCick,则该方法将无法运行,并且如果我在onCreate之外使用该方法方法调用XML会崩溃。

onClickListener的完整java代码:

package com.example.mrsanchez.tunalogs;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Region;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class register extends AppCompatActivity {
    //
    //Variables:
    //
    public static final String MyPREFERENCES = "MyPrefs";
    public static final String name = "nameKey";
    public static final String email = "emailKey";
    public static final String password = "passwordKey";
    SharedPreferences sharedpreferences;
    EditText edName;
    EditText edPassword;
    EditText edRepeat;
    EditText edEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        edName = (EditText) findViewById(R.id.txtUsername);
        edPassword = (EditText) findViewById(R.id.txtPassword);
        edRepeat = (EditText) findViewById(R.id.txtRepeat);
        edEmail = (EditText) findViewById(R.id.txtEmail);
        Button regist = (Button) findViewById(R.id.btnRegisterFinal);

        regist.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String n = edName.getText().toString();
                String p = edPassword.getText().toString();
                String pr = edRepeat.getText().toString();
                String e = edEmail.getText().toString();

                SharedPreferences.Editor editor = sharedpreferences.edit();

                if(p.equals(pr))
                {
                    editor.putString(name, n);
                    editor.putString(password, p);
                    editor.putString(email, e);
                    editor.commit();
                    Toast.makeText(register.this,"Your account has been successfully created!",Toast.LENGTH_LONG).show();

                }
                else {
                    Toast.makeText(register.this, "Your passwords do not correspond.", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

}

完整的XML代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_register"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mrsanchez.tunalogs.register">

    <EditText
        android:id="@+id/txtUsername"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:hint="Username"/>

    <EditText
        android:id="@+id/txtPassword"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/txtUsername"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:hint="Password"
        android:inputType="textPassword"/>

    <EditText
        android:id="@+id/txtRepeat"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/txtPassword"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:hint="Confirm Password"
        android:inputType="textPassword"/>

    <EditText
        android:id="@+id/txtEmail"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/txtRepeat"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:hint="E-Mail"
        android:inputType="textEmailAddress"
        />

    <Button
        android:id="@+id/btnRegisterFinal"
        android:layout_width="130dp"
        android:layout_height="60dp"
        android:layout_below="@+id/txtEmail"
        android:layout_marginTop="50dp"
        android:layout_centerHorizontal="true"
        android:text="Register"/>


</RelativeLayout>

当前的logcat(没有崩溃,但方法仍未运行):

10-18 14:44:48.681 16037-16037/? E/cutils: to chown(/mnt/shell/emulated/0, 0, 0)
10-18 14:44:48.681 16037-16037/? E/cutils: to chown(/mnt/shell/emulated/obb, 0, 0)
10-18 14:44:48.682 16037-16037/? E/cutils: to chown(/storage/emulated/0/Android, 0, 0)
10-18 14:44:48.682 16037-16037/? E/cutils: to chown(/storage/emulated/0/Android/obb, 0, 0)
10-18 14:44:48.804 16037-16037/com.example.mrsanchez.tunalogs E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
10-18 14:44:48.809 16037-16037/com.example.mrsanchez.tunalogs E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
10-18 14:44:48.912 16037-16037/com.example.mrsanchez.tunalogs E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering

请帮助我,我已经在堆栈溢出一段时间内漫游并且没有真正发现任何有用的东西,除非我过度插入或错过了什么。

2 个答案:

答案 0 :(得分:0)

在xml布局中的按钮上设置android:onClick="regist"

答案 1 :(得分:0)

所以我最终找到了答案。

我第一次尝试这样做时,我错过了一件事:在我的主要活动中,我用

打电话给第二个活动
setContentView(R.layout.activity_register);

这导致所有按钮和其他控件无法正常运行,因此您应该这样做:

Intent intent = new Intent(login.this, register.class);
startActivity(intent);