你好我点击我的按钮时出现了奇怪的错误。之前它运作良好:它停止工作,我不知道为什么。你能帮我吗?这是代码:
public void onClick(View view) {
switch (view.getId()) {
case R.id.bPrisijungti:
String elpastas = etElpastas.getText().toString();
String slaptazodis = etSlaptazodis.getText().toString();
User user = new User(elpastas, slaptazodis,"","","");
authenticate(user);
break;
case R.id.tvRegistracija:
Intent registerIntent = new Intent(Login.this, Register.class);
startActivity(registerIntent);
break;
}
}
我在String elpastas上收到错误......
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:text="El paštas"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/etelpastas"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:text="Slaptažodis"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/etSlaptazodis"
android:layout_width="match_parent"
android:inputType="textPassword"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bPrisijungti"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content"
android:text="Prisijungti" />
<TextView
android:id="@+id/tvRegistracija"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Naujas vartotojas"
android:textStyle="bold" />
</LinearLayout>
Logcat:
05-25 21:04:57.094 9100-9100/com.tonikamitv.loginregister D/OpenGLRenderer﹕ Enabling debug mode 0
05-25 21:04:58.384 9100-9100/com.tonikamitv.loginregister D/AndroidRuntime﹕ Shutting down VM
05-25 21:04:58.384 9100-9100/com.tonikamitv.loginregister W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41241438)
05-25 21:04:58.424 9100-9100/com.tonikamitv.loginregister E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.tonikamitv.loginregister.Login.onClick(Login.java:40)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
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:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
你是否在onclick之前完成了这项工作。我认为这是根本原因。
EditText etElpastas=(EditText)findViewById(R.id.etelpastas);