单击“SignUp”按钮时,NullPointerException
出现问题。有人可以帮忙吗? Thanx提前...
MainActivity.java:
public class MainActivity extends Activity {
Button loginButton,signUpBtn;
EditText txtUsername,txtPassword;
private AppDataSouce datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginButton = (Button) findViewById(R.id.btnLogin);
txtUsername = (EditText) findViewById(R.id.txtUsername);
txtPassword = (EditText) findViewById(R.id.txtPassword);
signUpBtn=(Button) findViewById(R.id.signUpBtn);
signUpBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("nulll1"+txtUsername.getText().toString());
datasource.createUser(txtUsername.getText().toString(), txtPassword.getText().toString());
System.out.println("nulll2" + txtUsername);
Toast.makeText(MainActivity.this, "Registered Sucessfully", 3000).show();
System.out.println("nulll3" + txtUsername.getText().toString());
Intent listActivityIntent = new Intent(MainActivity.this,ShowListActivity.class);
MainActivity.this.startActivity(listActivityIntent);
}
});
}
logcat的:
08-11 13:18:46.772: E/AndroidRuntime(14331): at com.example.androidlower.MainActivity$1.onClick(MainActivity.java:35)
08-11 13:18:46.772: E/AndroidRuntime(14331): at android.view.View.performClick(View.java:4204)
08-11 13:18:46.772: E/AndroidRuntime(14331): at android.view.View$PerformClick.run(View.java:17355)
08-11 13:18:46.772: E/AndroidRuntime(14331): at android.os.Handler.handleCallback(Handler.java:725)
08-11 13:18:46.772: E/AndroidRuntime(14331): at android.os.Handler.dispatchMessage(Handler.java:92)
08-11 13:18:46.772: E/AndroidRuntime(14331): at android.os.Looper.loop(Looper.java:137)
08-11 13:18:46.772: E/AndroidRuntime(14331): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-11 13:18:46.772: E/AndroidRuntime(14331): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 13:18:46.772: E/AndroidRuntime(14331): at java.lang.reflect.Method.invoke(Method.java:511)
08-11 13:18:46.772: E/AndroidRuntime(14331): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-11 13:18:46.772: E/AndroidRuntime(14331): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-11 13:18:46.772: E/AndroidRuntime(14331): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
我没有看到你在创建AppDataSouce datasource;
对象的任何地方
并且您在不创建对象的情况下使用datasource
,因此它只是一个引用而不是对象。
datasource.createUser(txtUsername.getText().toString(), txtPassword.getText().toString());
因此它正在给予nullpointer exception.
答案 1 :(得分:0)
在使用数据源之前,您缺少声明数据源!
datasource = new AppDataSouce(whatever);
答案 2 :(得分:0)
datasource
似乎无法在onCreate()
中的任何位置进行初始化。如果一个对象没有初始化,那么打算如何使用它。
在设置clickListener之前,请创建此对象。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new AppDataSouce();
....
}