使用EditText Android Studio的java.lang.NullPointerException

时间:2016-03-25 13:13:59

标签: java android android-studio android-edittext android-sdk-tools

我之前已经解决了这个问题,但我不记得我做了什么。基本上它是一个简单的EditText并通过ID查找它的视图,但在运行应用程序时我收到错误messagejava.lang.NullPointerException

public class MainActivity extends Activity {

CallbackManager callbackManager;
public static final String SECRET_KEY = "65EB1D3A-F8D8-D6DC-FF21-E111F9409600";
public static final String APP_ID = "F5931D23-2392-1B65-FF65-BC4256BB9300";
RegisterAccount register;
EditText mName = (EditText) findViewById(R.id.userName);
EditText mEmail = (EditText) findViewById(R.id.userEmail);
EditText mPassword = (EditText) findViewById(R.id.userPassword);
EditText mConfirmPassword = (EditText) findViewById(R.id.userConfirmPassword);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this.getApplicationContext());
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);
    Backendless.initApp(MainActivity.this, APP_ID, SECRET_KEY, "v1");

    BackendlessUser user = new BackendlessUser();
    user.setEmail(mEmail.getText().toString());
    user.setPassword(mPassword.getText().toString());

    Backendless.UserService.register(user, new AsyncCallback<BackendlessUser>() {
        @Override
        public void handleResponse(BackendlessUser backendlessUser) {

            Intent logIntent = new Intent(MainActivity.this, ContentActivity.class);
            logIntent.putExtra("Name", mEmail.getText().toString());
            startActivity(logIntent);
        }

        @Override
        public void handleFault(BackendlessFault backendlessFault) {
            // On failure
        }

});

1 个答案:

答案 0 :(得分:0)

请填写以下内容:

EditText mName = (EditText) findViewById(R.id.userName);
EditText mEmail = (EditText) findViewById(R.id.userEmail);
EditText mPassword = (EditText) findViewById(R.id.userPassword);
EditText mConfirmPassword = (EditText) findViewById(R.id.userConfirmPassword);

下方:setContentView(R.layout.activity_main);方法中的onCreate()

首先使用setContentView()方法加载XML视图,然后再实例化视图对象。