我有一个启动页面,当我的应用程序打开时会打开,然后它被LoginActivity
替换,它为用户提供了使用该应用程序登录Twitter的选项。这是LoginActivity
的onCreate方法:
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() {
我甚至无法理解为什么会这样。有谁有想法吗? Logcat下面:
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服务等。感谢您的帮助!
activity_login.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>
答案 0 :(得分:3)
尝试此代码可能对您有所帮助
只需更改
loginTwitter.setOnClickListener(new OnClickListener() {
而不是
loginTwitter.setOnClickListener(new View.OnClickListener() {
使用此解决方案
Button loginTwitter = (Button) findViewById(R.id.twitterLoginButton);
Button noLogin = (Button) findViewById(R.id.noLoginButton);
loginTwitter.setOnClickListener(new OnClickListener() {
/**
* Login via Twitter
*/
@SuppressLint("NewApi")
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), PrepareRequestTokenActivity.class);
startActivity(i);
}
});
noLogin.setOnClickListener(new OnClickListener() {
/**
* Proceed to use app without login
*/
@SuppressLint("NewApi")
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
答案 1 :(得分:0)
可能是因为您无法使用ID R.id.twitterLoginButton
进行控制,因此findViewById
将返回NULL。
如果在布局文件中看到此控件,则应该再次尝试构建(“项目”菜单,“清理构建文件”)。
答案 2 :(得分:0)
检查清单LoginActivity是否在包含名称的正确路径中。
如果正确复制下面的文件形成任何工作项目并在项目中替换..
ant.properties
build.xml
proguard-project.txt
project.properties
.classpath
.project