So I'm creating this class that has two buttons, one for logging in, and one for logging in using facebook. However, occasionally this problem will crop up. It has done so twice now, and last time it mysteriously went away after appearing. I wasn't working on this file, and suddenly the problem was there. It hasn't changed from when the problem wasn't there.
Basically the application throws a null pointer exception around line 55 which is the buttonLogIn.setOnClickListener line, indicating that the button doesn't get created. I've looked at a couple of similar problems across the web, but most of them seem to be resolved around by realising that the super.onC开发者_Go百科reate() and setContentView() methods should be the first things created. I do this already.
This class gets created and has the following onCreate and fields:
private Button buttonLogIn;
private Button buttonFacebookLogIn;
private EditText usernameText;
private EditText passwordText;
private SharedPreferences p;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
p = PreferenceManager.getDefaultSharedPreferences(ThisClass.this);
Integer storedID = p.getInt("id", 0);
String storedUsername = p.getString("username", "");
String storedPassword = p.getString("password", "");
if(storedUsername != null && !storedUsername.equalsIgnoreCase("") && storedPassword != null && !storedPassword.equalsIgnoreCase("") && storedID != null && storedID != 0){
startHomeActivity(storedID);
}
buttonLogIn = (Button) findViewById(R.id.login);
buttonLogIn.setOnClickListener(this);
buttonFacebookLogIn = (Button) findViewById(R.id.facebook);
buttonFacebookLogIn.setOnClickListener(this);
usernameText = (EditText) findViewById(R.id.usernameEdit);
passwordText = (EditText) findViewById(R.id.passwordEdit);
}
I have the two buttons declared in my XML like so:
<Button android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/password"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="Log In" />
<Button android:id="@+id/facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/login"
android:layout_alignParentRight="true"
android:text="Use Facebook Log In"/>
And my stack trace is:
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tigatag.android/com.tigatag.android.Tigatag}: java.lang.NullPointerException
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at android.os.Looper.loop(Looper.java:123)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at java.lang.reflect.Method.invoke(Method.java:521)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at dalvik.system.NativeStart.main(Native Method)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): Caused by: java.lang.NullPointerException
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at com.tigatag.android.Tigatag.onCreate(Tigatag.java:55)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-06 16:41:11.068: ERROR/AndroidRuntime(12464): ... 11 more
Any ideas/clues/hints would be very much appreciated. Whilst typing this up the problem has gone away again, so I really don't even have a clue.
That sounds like you need to run Project->Clean
from Eclipse to rebuild your R.java
.
精彩评论