我开发了一款应用,但是用户注册似乎会让应用崩溃。如果有人可以提供帮助的话,我已经检查了一切,但找不到原因。我的代码如下。
java class
public class SignUp extends AppCompatActivity {
DatabaseHelper helper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
}
public void onSignupClick (View view)
{
if (view.getId() == R.id.BsignUp)
{
EditText name = (EditText)findViewById(R.id.TFusername);
EditText email = (EditText)findViewById(R.id.TFemail);
EditText uname = (EditText)findViewById(R.id.TFuname);
EditText pass1 = (EditText)findViewById(R.id.TFpass1);
//EditText pass2 = (EditText)findViewById(R.id.TFpass2);
String namestr = name.getText().toString();
String emailstr = email.getText().toString();
String unamestr = uname.getText().toString();
String pass1str = pass1.getText().toString();
Contact c = new Contact();
c.setName(namestr);
c.setEmail(emailstr);
c.setUname(unamestr);
c.setPass(pass1str);
helper.insertContact(c);
}
}
}
xml布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:src ="@drawable/saica"
android:layout_height="120dp" />
<TextView
android:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/TFname" />
<TextView
android:text="Email Address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/TFemail" />
<TextView
android:text="Username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView4" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/TFuname" />
<TextView
android:text="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView5" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/TFpass1"
android:password="true" />
<Button
android:text="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BsignUp"
android:onClick="onSignupClick"/>
</LinearLayout>
错误记录
FATAL EXCEPTION: main
Process: com.example.pooveshin.saica_sgb, PID: 10321
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.pooveshin.saica_sgb.SignUp.onSignupClick(SignUp.java:30)
at java.lang.reflect.Method.invoke(Native Method)
答案 0 :(得分:1)
您没有任何名为EditText
的{{1}},您的实际EditText ID为TFusername
。所以它应该是
TFname