不明白为什么我的应用程序崩溃

时间:2015-11-06 04:33:35

标签: java android android-layout

嘿所以我是Android开发的新手,我正在创建一个应用程序。到目前为止,我想要做的就是链接我的主要活动上的按钮以打开登录活动页面。当我在手机上启动应用程序时,它会运行以下错误。有人有什么建议吗?提前谢谢!

11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime: FATAL EXCEPTION: main
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime: Process: com.example.damien.paysafe1_0_a, PID: 14967
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.damien.paysafe1_0_a/com.example.damien.paysafe1_0_a.MainActivity}: java.lang.ClassCastException: com.example.damien.paysafe1_0_a.MainActivity cannot be cast to android.view.View$OnClickListener
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.app.ActivityThread.access$1000(ActivityThread.java:198)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6837)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:  Caused by: java.lang.ClassCastException: com.example.damien.paysafe1_0_a.MainActivity cannot be cast to android.view.View$OnClickListener
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at com.example.damien.paysafe1_0_a.MainActivity.onCreate(MainActivity.java:20)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6500)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218) 
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.app.ActivityThread.access$1000(ActivityThread.java:198) 
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676) 
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6837) 
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
11-05 23:24:44.879 14967-14967/com.example.damien.paysafe1_0_a E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context="com.example.damien.paysafe1_0_a.MainActivity"
    android:orientation="vertical"
    android:weightSum="1">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:src="@drawable/paysafe_logo"
        android:layout_gravity="center_horizontal" />

    <Button android:id="@+id/new_user_button"
        android:layout_width="152dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:text="@string/new_user"
        android:textStyle="bold"
        android:layout_gravity="center_horizontal"
        android:backgroundTint="#b5baf6"
        android:layout_weight="0.08"
        android:textSize="24sp"
        android:typeface="normal" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.85"
        android:weightSum="1">

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/copyright"
        android:id="@+id/textView"
        android:password="false"
        android:layout_gravity="center_horizontal" />


    <!--Go to register page when pressed-->

</LinearLayout>

这是activity_login.xml

<LinearLayout 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:gravity="center_horizontal"
    android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" tools:context=".LoginActivity">

    <!-- Login progress -->
    <ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginBottom="8dp" android:visibility="gone" />

    <ScrollView android:id="@+id/login_form" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout android:id="@+id/email_login_form" android:layout_width="match_parent"
            android:layout_height="wrap_content" android:orientation="vertical">

            <android.support.design.widget.TextInputLayout android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <AutoCompleteTextView android:id="@+id/email" android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:hint="@string/prompt_email"
                    android:inputType="textEmailAddress" android:maxLines="1"
                    android:singleLine="true" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText android:id="@+id/password" android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:hint="@string/prompt_password"
                    android:imeActionId="@+id/login"
                    android:imeActionLabel="@string/action_sign_in_short"
                    android:imeOptions="actionUnspecified" android:inputType="textPassword"
                    android:maxLines="1" android:singleLine="true" />

            </android.support.design.widget.TextInputLayout>

            <Button android:id="@+id/email_sign_in_button" style="?android:textAppearanceSmall"
                android:layout_width="match_parent" android:layout_height="wrap_content"
                android:layout_marginTop="16dp" android:text="@string/action_sign_in"
                android:textStyle="bold" />

        </LinearLayout>
    </ScrollView>
</LinearLayout>

以下是我的Android Manifest以防万一:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.damien.paysafe1_0_a" >

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".LoginActivity" >
        </activity>
    </application>


</manifest>

编辑:主要活动代码:

package com.example.damien.paysafe1_0_a;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button new_user_button = (Button) findViewById(R.id.new_user_button);
        new_user_button.setOnClickListener((OnClickListener) this);
    }

    public void onClick(View v) {
        Log.i("clicks", "You Clicked New User");
        Intent i=new Intent(MainActivity.this, LoginActivity.class);
        startActivity(i);
    }

}

3 个答案:

答案 0 :(得分:2)

您必须在Activity上实现onClickListener。即。

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button new_user_button = (Button) findViewById(R.id.new_user_button);
    new_user_button.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    Log.i("clicks", "You Clicked New User");
    Intent i=new Intent(MainActivity.this, LoginActivity.class);
    startActivity(i);
}

}

答案 1 :(得分:1)

你可以尝试一下:

new_user_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.i("clicks", "You Clicked New User");
            Intent i=new Intent(MainActivity.this, LoginActivity.class);
            startActivity(i);
        }
    });

答案 2 :(得分:0)

您的Activity未实现OnClickListener,但您将活动强制转换为OnClickListener,因此它的案例类强制转换异常