2

I have a splash page that opens up when my app is open, and then it is replaced by a LoginActivity that gives the option to the user of logging into Twitter with the app. This is the onCreate method of 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);
        }
    });


}

When the app is started up, the splash displays fine, but the LoginActivity brings about a NullPointerException pointing specifically at the line 29, which is: loginTwitter.setOnClickListener(new View.OnClickListener() {

I cannot even begin to understand why this is happening. Does anyone have any idea? Logcat below:

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

I should add that this was working fine as is until updating my ADT, Play services, etc. Thank you for the help!

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>
user2163853
  • 889
  • 4
  • 14
  • 24

3 Answers3

3

Try this code may be help to you

Just change

 loginTwitter.setOnClickListener(new OnClickListener() {

instead of

 loginTwitter.setOnClickListener(new View.OnClickListener() {

Use this solution

 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);
        }
    });
Dixit Patel
  • 6,051
  • 1
  • 30
  • 42
0

Probably because you have no control with the ID R.id.twitterLoginButton, so findViewById returns NULL.

If you see this control in your layout file, you should try and build again (Project menu, Clean build files).

zmbq
  • 35,452
  • 13
  • 80
  • 153
0

Check the manifest LoginActivity is in right path with package name or not..

if it is proper copy below files form any working project and replace in your project..

ant.properties
build.xml
proguard-project.txt
project.properties
.classpath
.project
Sandeep P
  • 3,962
  • 2
  • 22
  • 43