0

我是编程和 Android 的菜鸟。

我和我的学校团队正在尝试开发我们的第一个 android 应用程序,我们实际上并没有很好地计划事情,我们最初的 MainActivity 类有一个输入表单,但是,当我们开始做应用程序的其余部分时,我们意识到主要活动需要是登录表单。因此,我们将主活动中的代码移动到名为 Datospersonales.java 的类中,并将登录表单放入 MainActivity.java 中,但是,自从我们这样做以来,它根本不会运行:

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

代码如下所示:

 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>
4

2 回答 2

1
log.setOnClickListener((OnClickListener) this);

是你的问题,你的类没有实现 OnClickListener

附加implements OnClickListenerMainActivity extends Activity,这应该工作。

于 2013-05-06T16:23:58.350 回答
0

您的类必须实现 OnClicListener 并覆盖 onClick 方法

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

您可以使用匿名内部类,如下所示

  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));  
                  }                 
            });

也使用 Activity Context 代替 getApplicationContext()

何时调用活动上下文或应用程序上下文?

通过 commonsware 检查答案

于 2013-05-06T16:45:45.933 回答