我在申请时遇到了微调器问题。
微调器应该将所选项传递给SQlite DB并保存数据。
但是到目前为止,每次按下我的注册按钮都会引发javaNullPointerException
错误。
这是我的班级:
public class RegisterMember extends Activity implements OnItemSelectedListener{
DbAdapter dbAdapter;
EditText txtName;
EditText txtPassword;
EditText txtPasswordConf;
EditText txtEmail;
EditText txtEmailConf;
EditText txtSchool;
Button btnJoin;
//Button btnRegister;
private String[] state= {"USB","Stanford","Harvard","UCV","USM","Tor vergata", "La Sapienza"};
Spinner spinner_school;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_members_register);
System.out.println(state.length);
spinner_school = (Spinner) findViewById(R.id.spinner_school);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, state);
adapter_state
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_school.setAdapter(adapter_state);
spinner_school.setOnItemSelectedListener(this);
//btnRegister = (Button) findViewById(R.id.btn_reg);
dbAdapter = new DbAdapter(this);
dbAdapter.open();
btnJoin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtName.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtPasswordConf.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtEmail.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtEmailConf.getWindowToken(), 0);
//imm.hideSoftInputFromWindow(txtSchool.getWindowToken(), 0);
//imm.hideSoftInputFromWindow(spinner_school.getWindowToken(), 0);
try {
String name = txtName.getText().toString();
String password = txtPassword.getText().toString();
String passwordconf = txtPasswordConf.getText().toString();
String email = txtEmail.getText().toString();
String emailconf = txtEmailConf.getText().toString();
//String school = txtSchool.getText().toString();
String school = spinner_school.getSelectedItem().toString();
long i = dbAdapter.register(name, password, email, school);
if(i != -1)
Toast.makeText(RegisterMember.this, "You have successfully registered",Toast.LENGTH_LONG).show();
else {
if(!passwordconf.equals(password))
{
Toast.makeText(getApplicationContext(), "Password Does Not Matches", Toast.LENGTH_LONG).show();
}
if(!emailconf.equals(email))
{
Toast.makeText(getApplicationContext(), "Email Does Not Matches", Toast.LENGTH_LONG).show();
}
}
} catch (SQLException e) {
Toast.makeText(RegisterMember.this, "Some problem occurred",
Toast.LENGTH_LONG).show();
}
}
});
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
spinner_school.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
布局上的微调器:
<TableRow
android:id="@+id/tableRow10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner_school"
android:spinnerMode="dropdown" />
</TableRow>
堆栈跟踪,它从方法btnJoin.setOnClickListener(new OnClickListener()
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shairlook/com.kkoci.shairlook.RegisterMember}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.kkoci.shairlook.RegisterMember.onCreate(RegisterMember.java:57)
有什么想法吗?我已经查看过示例,但我仍然无法弄清楚这个
提前致谢!
重复编辑
不,重复并不能解决我的问题,因为这里涉及SQLite,这是一个简单的方法问题,谢谢。
第二次编辑
这就是btnJoin.setOnClickListener(new OnClickListener() {
方法现在的样子,同样的错误:
btnJoin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtName.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtPasswordConf.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtEmail.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtEmailConf.getWindowToken(), 0);
//imm.hideSoftInputFromWindow(txtSchool.getWindowToken(), 0);
//imm.hideSoftInputFromWindow(spinner_school.getWindowToken(), 0);
try {
txtName = (EditText) findViewById(R.id.et_user);
String name = txtName.getText().toString();
txtPassword = (EditText) findViewById(R.id.et_pw_reg);
String password = txtPassword.getText().toString();
txtPasswordConf = (EditText) findViewById(R.id.et_pw_conf);
String passwordconf = txtPasswordConf.getText().toString();
txtEmail = (EditText) findViewById(R.id.et_email_reg);
String email = txtEmail.getText().toString();
txtEmailConf = (EditText) findViewById(R.id.et_email_conf);
String emailconf = txtEmailConf.getText().toString();
//String school = txtSchool.getText().toString();
spinner_school = (Spinner) findViewById(R.id.spinner_school);
String school = spinner_school.getSelectedItem().toString();
long i = dbAdapter.register(name, password, email, school);
if(i != -1)
Toast.makeText(RegisterMember.this, "You have successfully registered",Toast.LENGTH_LONG).show();
else {
if(!passwordconf.equals(password))
{
Toast.makeText(getApplicationContext(), "Password Does Not Matches", Toast.LENGTH_LONG).show();
}
if(!emailconf.equals(email))
{
Toast.makeText(getApplicationContext(), "Email Does Not Matches", Toast.LENGTH_LONG).show();
}
}
} catch (SQLException e) {
Toast.makeText(RegisterMember.this, "Some problem occurred",
Toast.LENGTH_LONG).show();
}
}
});
答案 0 :(得分:3)
我认为你忘记了“查找”并从你的视角中膨胀小部件。因此,当你打电话时 txtName.getText()的toString(); 它抛出nullPointerException,因为你的txtName仍为null。
要解决此问题,您需要在尝试访问代码之前对代码中的所有小部件进行充气。所以在你调用setContentView()之后,你会这样做:
txtName = (EditText) findViewById(R.id.the_id_of_the_widget)
用于所有小部件。
编辑:你仍然得到同样的错误,因为你还没有给按钮充气。
添加:
btnJoin = (Button) findViewById(...)
在调用setOnClickListener之前。
答案 1 :(得分:2)
您尚未为btnJoin
分配任何值。这导致异常。
编辑1 - 如何解决问题
要解决此问题,以及单击按钮时可能发生的其他问题,请在setContentView(R.layout.activity_members_register)
之后将以下行添加到onCreate()方法:
txtName = (EditText)findViewById(R.id.register_member_name);
txtPassword = (EditText)findViewById(R.id.register_member_password);
txtPasswordConf = (EditText)findViewById(R.id.register_member_password_confirm);
txtEmail = (EditText)findViewById(R.id.register_member_email);
txtEmailConf = (EditText)findViewById(R.id.register_member_email_confirm);
txtSchool = (EditText)findViewById(R.id.register_member_school);
btnJoin = (Button)findViewById(R.id.register_member_button_join);
只要你的布局文件中有这样的东西:
...
<EditText android:id="@+id/register_member_name" ... />
...
<EditText android:id="@+id/register_member_password" ... />
...
<EditText android:id="@+id/register_member_password_confirm" ... />
...
<EditText android:id="@+id/register_member_email" ... />
...
<EditText android:id="@+id/register_member_email_confirm" ... />
...
<EditText android:id="@+id/register_member_school" ... />
...
<Button android:id="@+id/register_member_button_join" ... />
...
其中register_member_name
,register_member_password
,register_member_password_confirm
,register_member_email
,register_member_email_confirm
,register_member_school
和register_member_button_join
是小工具ID。你不必使用这些名字。只要你在布局文件和java实现中使用相同的值,一切都会好起来的。
答案 2 :(得分:1)
你应该在onClick方法中尝试这个
spinner_school = (Spinner) findViewById(R.id.spinner_school);
String school = spinner_school.getSelectedItem().toString();
并且请将findviewByid放在所有小部件中
答案 3 :(得分:1)
你的问题就在那里:
Caused by: java.lang.NullPointerException
at com.kkoci.shairlook.RegisterMember.onCreate(RegisterMember.java:57)
查看RegisterMember.java的第57行,您正在尝试访问一些未初始化的变量。看不到行号,所以我无法再帮你了......这是你的第57行?