我有一个启动页面,当我的应用程序打开时会打开,然后它被替换为一个LoginActivity
,它为用户提供了使用该应用程序登录 Twitter 的选项。这是 onCreate 方法LoginActivity
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Show the Up button in the action bar.
setupActionBar();
Log.i("here", "login has started");
Button loginTwitter = (Button) findViewById(R.id.twitterLoginButton);
Button noLogin = (Button) findViewById(R.id.noLoginButton);
loginTwitter.setOnClickListener(new View.OnClickListener() {
/**
* Login via Twitter
*/
@SuppressLint("NewApi")
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), PrepareRequestTokenActivity.class);
startActivity(i);
}
});
noLogin.setOnClickListener(new View.OnClickListener() {
/**
* Proceed to use app without login
*/
@SuppressLint("NewApi")
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
}
当应用程序启动时,启动画面显示正常,但 LoginActivity 带来了一个 NullPointerException,专门指向第 29 行,即:loginTwitter.setOnClickListener(new View.OnClickListener() {
我什至无法理解为什么会这样。有人有什么主意吗?日志如下:
06-25 00:06:21.000: E/AndroidRuntime(4809): FATAL EXCEPTION: main
06-25 00:06:21.000: E/AndroidRuntime(4809): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tforan.blobtag4/com.tforan.blobtag4.LoginActivity}: java.lang.NullPointerException
06-25 00:06:21.000: E/AndroidRuntime(4809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1852)
06-25 00:06:21.000: E/AndroidRuntime(4809): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1873)
06-25 00:06:21.000: E/AndroidRuntime(4809): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
06-25 00:06:21.000: E/AndroidRuntime(4809): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
06-25 00:06:21.000: E/AndroidRuntime(4809): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 00:06:21.000: E/AndroidRuntime(4809): at android.os.Looper.loop(Looper.java:150)
06-25 00:06:21.000: E/AndroidRuntime(4809): at android.app.ActivityThread.main(ActivityThread.java:4358)
06-25 00:06:21.000: E/AndroidRuntime(4809): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 00:06:21.000: E/AndroidRuntime(4809): at java.lang.reflect.Method.invoke(Method.java:507)
06-25 00:06:21.000: E/AndroidRuntime(4809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
06-25 00:06:21.000: E/AndroidRuntime(4809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
06-25 00:06:21.000: E/AndroidRuntime(4809): at dalvik.system.NativeStart.main(Native Method)
06-25 00:06:21.000: E/AndroidRuntime(4809): Caused by: java.lang.NullPointerException
06-25 00:06:21.000: E/AndroidRuntime(4809): at com.tforan.blobtag4.LoginActivity.onCreate(LoginActivity.java:29)
06-25 00:06:21.000: E/AndroidRuntime(4809): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
06-25 00:06:21.000: E/AndroidRuntime(4809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
06-25 00:06:21.000: E/AndroidRuntime(4809): ... 11 more
我应该补充一点,在更新我的 ADT、Play 服务等之前,这一切正常。谢谢你的帮助!
活动登录.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#D0E4F7"
tools:context=".LoginActivity" >
<ImageView
android:id="@+id/loginLogo"
android:contentDescription="@string/logo"
android:layout_centerHorizontal="true"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:src="@drawable/logo" />
<Button
android:id="@+id/twitterLoginButton"
style="@style/buttonText"
android:layout_below="@+id/loginLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/signinwithtwitter"
android:textColor="#000000" />
<Button
android:id="@+id/noLoginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/twitterLoginButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:paddingTop="5dp"
android:textSize="12dp"
android:text="@string/declineLogin" />
</RelativeLayout>