【问题标题】:MAIN exception when replacing code in MainActivity替换 MainActivity 中的代码时出现 MAIN 异常
【发布时间】:2013-04-30 10:55:25
【问题描述】:

我是编程和 Android 的菜鸟。

我和我的学校团队正在尝试开发我们的第一个 android 应用程序,我们实际上并没有很好地计划事情,我们最初的 MainActivity 类有一个输入表单,但是,当我们开始做应用程序的其余部分时,我们意识到主要活动需要是登录表单。因此,我们将 main Activity 中的代码移动到名为 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>

【问题讨论】:

  • 尝试清理您的项目??因为有一段时间我遇到了类转换异常。
  • 同时检查您的 AndroidManifest.xml 文件
  • 发布您的 AndroidManifest 文件

标签: android exception main


【解决方案1】:
log.setOnClickListener((OnClickListener) this);

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

implements OnClickListener 附加到MainActivity extends Activity,这应该可以工作。

【讨论】:

  • @bclymer 是对的.. 你不能只对 Onclicklistener 进行类型转换。
【解决方案2】:

你的类必须实现 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()

When to call activity context OR application context?

通过commonsware查看答案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-13
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多