所以我创建了这个有两个按钮的类,一个用于登录,另一个用于使用facebook登录。但是,偶尔会出现这个问题。它已经完成了两次,上次出现后神秘地消失了。我没有在这个文件上工作,突然之间出现了问题。它没有从问题不存在时发生变化。
基本上,应用程序在第55行周围抛出一个空指针异常,它是buttonLogIn.setOnClickListener行,表示该按钮没有被创建。我已经在Web上查看了几个类似的问题,但是大多数问题似乎都是通过意识到super.onCreate()和setContentView()方法应该是第一个创建的东西来解决的。我已经这样做了。
此类已创建,并具有以下onCreate和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);
}
我在XML中声明了两个按钮:
<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"/>
我的堆栈跟踪是:
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
非常感谢任何想法/线索/提示。虽然输入这个问题已经消失了,所以我真的没有任何线索。
答案 0 :(得分:0)
听起来你需要从Eclipse运行Project->Clean
来重建你的R.java
。