0

I'm a total noob to programming and Android.

My school team and I were trying to develop our first android app, we didn't actually plan things well and our original MainActivity class had an input form, however, when we got down to doing the rest of the app, we realized that the main activity needed to be a login form. So, we moved the code inside the main activity to a class named Datospersonales.java and we put in the login form in the MainActivity.java however, ever since we did that, it won't run at all:

05-06 10:01:35.681: E/AndroidRuntime(278): FATAL EXCEPTION: main
05-06 10:01:35.681: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lania.saludandroidte/com.lania.saludandroidte.MainActivity}: java.lang.ClassCastException: com.lania.saludandroidte.MainActivity
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:123)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-06 10:01:35.681: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
05-06 10:01:35.681: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:521)
05-06 10:01:35.681: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-06 10:01:35.681: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-06 10:01:35.681: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)
05-06 10:01:35.681: E/AndroidRuntime(278): Caused by: java.lang.ClassCastException: com.lania.saludandroidte.MainActivity
05-06 10:01:35.681: E/AndroidRuntime(278):  at com.lania.saludandroidte.MainActivity.onCreate(MainActivity.java:19)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-06 10:01:35.681: E/AndroidRuntime(278):  ... 11 more
05-06 10:20:01.692: D/AndroidRuntime(309): Shutting down VM
05-06 10:20:01.702: W/dalvikvm(309): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-06 10:20:01.712: E/AndroidRuntime(309): FATAL EXCEPTION: main
05-06 10:20:01.712: E/AndroidRuntime(309): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lania.saludandroidte/com.lania.saludandroidte.MainActivity}: java.lang.ClassCastException: com.lania.saludandroidte.MainActivity
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.os.Looper.loop(Looper.java:123)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-06 10:20:01.712: E/AndroidRuntime(309):  at java.lang.reflect.Method.invokeNative(Native Method)
05-06 10:20:01.712: E/AndroidRuntime(309):  at java.lang.reflect.Method.invoke(Method.java:521)
05-06 10:20:01.712: E/AndroidRuntime(309):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-06 10:20:01.712: E/AndroidRuntime(309):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-06 10:20:01.712: E/AndroidRuntime(309):  at dalvik.system.NativeStart.main(Native Method)
05-06 10:20:01.712: E/AndroidRuntime(309): Caused by: java.lang.ClassCastException: com.lania.saludandroidte.MainActivity
05-06 10:20:01.712: E/AndroidRuntime(309):  at com.lania.saludandroidte.MainActivity.onCreate(MainActivity.java:19)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-06 10:20:01.712: E/AndroidRuntime(309):  ... 11 more
05-06 10:20:03.832: I/Process(309): Sending signal. PID: 309 SIG: 9

The code looks like this:

 package com.lania.saludandroidte;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button log = (Button) findViewById(R.id.btnLogin2);
        log.setOnClickListener((OnClickListener) this);

        TextView registerScreen = (TextView) findViewById(R.id.link_to_register);

        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // Switching to Register screen

                if (v.getId() == R.id.btnLogin2) {
                    startActivity(new Intent(MainActivity.this, Menu.class));
                }
                Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(i);
            }
        });
    }



}






**And this is the layout:**

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:fillViewport="true">
  <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:background="#ffffff">

        <!--  Header  Starts-->
        <LinearLayout android:id="@+id/header"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@layout/header_gradient"
                android:paddingTop="5dip"
                android:paddingBottom="5dip">
                <!-- Logo Start-->
                <!-- Logo Ends -->
        </LinearLayout>
        <!--  Header Ends -->
        <!-- Footer Start -->
        <LinearLayout android:id="@+id/footer"
                android:layout_width="fill_parent"
                android:layout_height="90dip"
                android:background="@layout/footer_repeat"
                android:layout_alignParentBottom="true">
        </LinearLayout>
        <!-- Footer Ends -->

        <!-- Login Form -->
        <LinearLayout

          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="10dip"
          android:layout_below="@id/header">
          <!--  Email Label -->
          <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#372c24"
                android:text="Email"/>
          <EditText android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:layout_marginBottom="20dip"
                android:singleLine="true"/>
          <!--  Password Label -->
          <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#372c24"
                android:text="Password"/>
          <EditText android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:singleLine="true"
                android:password="true"/>
          <!-- Login button -->

          <Button
              android:id="@+id/btnLogin2"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="10dip"
              android:text="Login" />

          <!-- Link to Registration Screen -->

          <TextView
              android:id="@+id/link_to_register"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_marginBottom="40dip"
              android:layout_marginTop="40dip"
              android:gravity="center"
              android:text="Registrarse "
              android:textColor="#0b84aa"
              android:textSize="20dip" />

        </LinearLayout>
        <!-- Login Form Ends -->
  </RelativeLayout>
</ScrollView>

2 Answers2

1
log.setOnClickListener((OnClickListener) this);

Is your issue, your class doesn't implement OnClickListener

Append implements OnClickListener to MainActivity extends Activity and this should work.

bclymer
  • 6,260
  • 2
  • 23
  • 34
0

Your class must implement OnClicListener and override onClick method

public class MainActivity extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button log = (Button) findViewById(R.id.btnLogin2);
    log.setOnClickListener(this);

    TextView registerScreen = (TextView) findViewById(R.id.link_to_register);

    // Listening to register new account link
    registerScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Switching to Register screen

            if (v.getId() == R.id.btnLogin2) {// this is never true
                startActivity(new Intent(MainActivity.this, Menu.class));
            }
            // always navigates to RegisterActivity on textview click
            // not sure what you want to achieve.   
            Intent i = new Intent(MainActivity.this, RegisterActivity.class);
            startActivity(i);
        }
    });
 }

@Override
public void onClick(View v) {

          // on button click if you want to navigate to menu activity
           startActivity(new Intent(MainActivity.this, Menu.class));

}
}

                             OR

You can use a anonymous inner class as below

  log.setOnClickListener(new OnClickListener()
            {
                  public void onClick(View v)
                  {
                   //do something
                   // on button click if you want to navigate to menu activity
                   startActivity(new Intent(MainActivity.this, Menu.class));  
                  }                 
            });

Also use Activity Context in place of getApplicationContext()

When to call activity context OR application context?

Check the answer by commonsware

Community
  • 1
  • 1
Raghunandan
  • 129,147
  • 24
  • 216
  • 249