我收到此错误
尝试在空对象引用上调用虚方法'void com.firebase.client.Firebase.createUser(java.lang.String,java.lang.String,com.firebase.client.Firebase $ ValueResultHandler)' 在com.test.test2.myfirebasetest.MainActivity.onClick(MainActivity.java:50)
你能告诉我我做错了什么吗?
public class MainActivity extends ActionBarActivity implements OnClickListener {
TextView email, pass;
Firebase myFirebaseRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
Button register = (Button) findViewById(R.id.register);
Button signin = (Button) findViewById(R.id.signin);
pass = (TextView) findViewById(R.id.pass);
email = (TextView) findViewById(R.id.email);
register.setOnClickListener(this);
signin.setOnClickListener(this);
Firebase myFirebaseRef = new Firebase(**LINK**);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.register:
myFirebaseRef.createUser(email.getText().toString(), pass.getText().toString(), new Firebase.ValueResultHandler<Map<String, Object>>() {
@Override
public void onSuccess(Map<String, Object> result) {
//do something
}
@Override
public void onError(FirebaseError firebaseError) {
// there was an error
}
});
break;
case R.id.signin:
break;
}
}
答案 0 :(得分:0)
问题是我重新声明了myFirebaseRef,因此onclick正在使用尚未初始化的全局变量。