无法将联系人 Activity 转换为 android.app.Activity

Contact Activity cannot be cast to android.app.Activity

我正在开发学校应用程序,刚开始学习 android 和 java。我创建了按钮并将它们链接起来以打开一个新的 activity,但我收到错误消息 ContactActivity cannot be caste to android.app.Activity。这是我的代码,需要帮助。

错误信息

info.academypk.parentsportal/info.academypk.parentsportal.SplashScreen})
05-01 04:32:25.894 9341-9380/info.academypk.parentsportal W/EGL_emulation: eglSurfaceAttrib not implemented
05-01 04:32:25.894 9341-9380/info.academypk.parentsportal W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xf3f98dc0, error=EGL_SUCCESS
05-01 04:32:26.369 1333-1359/system_process I/ActivityManager: Displayed info.academypk.parentsportal/.MainActivity: +731ms
05-01 04:32:29.375 1333-1349/system_process I/ActivityManager: START u0 {cmp=info.academypk.parentsportal/.ContactActivity} from uid 10064 on display 0
05-01 04:32:29.378 1333-1349/system_process V/WindowManager: addAppToken: AppWindowToken{2e14b321 token=Token{bea9d88 ActivityRecord{15eba52b u0 info.academypk.parentsportal/.ContactActivity t25}}} to stack=1 task=25 at 1
05-01 04:32:29.378 1333-1393/system_process W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
05-01 04:32:29.386 9341-9341/info.academypk.parentsportal D/AndroidRuntime: Shutting down VM
05-01 04:32:29.386 9341-9341/info.academypk.parentsportal E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: info.academypk.parentsportal, PID: 9341
                                                                            java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{info.academypk.parentsportal/info.academypk.parentsportal.ContactActivity}: java.lang.ClassCastException: info.academypk.parentsportal.ContactActivity cannot be cast to android.app.Activity
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                                at android.app.ActivityThread.access0(ActivityThread.java:151)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:135)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                             Caused by: java.lang.ClassCastException: info.academypk.parentsportal.ContactActivity cannot be cast to android.app.Activity
                                                                                at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                                at android.app.ActivityThread.access0(ActivityThread.java:151) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:135) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
05-01 04:32:29.387 1333-2045/system_process W/ActivityManager:   Force finishing activity 1 info.academypk.parentsportal/.ContactActivity
05-01 04:32:29.388 1333-2045/system_process W/ActivityManager:   Force finishing activity 2 info.academypk.parentsportal/.MainActivity
05-01 04:32:29.488 1333-2680/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
05-01 04:32:29.551 1333-2680/system_process W/EGL_emulation: eglSurfaceAttrib not implemented
05-01 04:32:29.551 1333-2680/system_process W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe14e8100, error=EGL_SUCCESS
05-01 04:32:30.104 1333-1354/system_process I/Choreographer: Skipped 30 frames!  The application may be doing too much work on its main thread.
05-01 04:32:30.143 1333-1354/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{15eba52b u0 info.academypk.parentsportal/.ContactActivity t25 f}
05-01 04:32:30.458 1588-2048/com.android.launcher3 W/EGL_emulation: eglSurfaceAttrib not implemented
05-01 04:32:30.458 1588-2048/com.android.launcher3 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe25cd720, error=EGL_SUCCESS
05-01 04:32:31.234 1588-2048/com.android.launcher3 W/OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
05-01 04:32:31.356 1427-1427/com.android.systemui W/ResourceType: No package identifier when getting value for resource number 0x00000000
05-01 04:32:31.356 1427-1427/com.android.systemui W/PackageManager: Failure retrieving resources for info.academypk.parentsportal: Resource ID #0x0
05-01 04:32:31.895 1333-1393/system_process W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
05-01 04:32:31.925 9341-9341/info.academypk.parentsportal I/Process: Sending signal. PID: 9341 SIG: 9
05-01 04:32:31.943 1333-1378/system_process W/InputDispatcher: channel '28d7efe9 info.academypk.parentsportal/info.academypk.parentsportal.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
05-01 04:32:31.943 1333-1378/system_process E/InputDispatcher: channel '28d7efe9 info.academypk.parentsportal/info.academypk.parentsportal.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
05-01 04:32:31.970 1333-1780/system_process I/ActivityManager: Process info.academypk.parentsportal (pid 9341) has died
05-01 04:32:31.971 1333-1350/system_process I/WindowState: WIN DEATH: Window{28d7efe9 u0 info.academypk.parentsportal/info.academypk.parentsportal.MainActivity}
05-01 04:32:31.971 1333-1350/system_process W/InputDispatcher: Attempted to unregister already unregistered input channel '28d7efe9 info.academypk.parentsportal/info.academypk.parentsportal.MainActivity (server)'
05-01 04:32:32.017 1333-2680/system_process D/OpenGLRenderer: endAllStagingAnimators on 0xded36680 (RippleDrawable) with handle 0xdec3ae70
05-01 04:32:32.021 1333-2045/system_process W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 9341 uid 10064

AndroidManifest 文件:

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
        </activity>
        <activity android:name=".ContactActivity">
        </activity>
        <activity android:name=".DailyDiaryActivity">
        </activity>
        <activity android:name=".EventsActivity">
        </activity>
        <activity android:name=".NoticeActivity">
        </activity>
        <activity android:name=".ResultsActivity">
        </activity>
        <activity android:name=".SyllabusActivity">
        </activity>

    </application>

</manifest>

MainActivity 文件:

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {

    Button b1;
    Button b2;
    Button b3;
    Button b4;
    Button b5;
    Button b6;

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

       b1=(Button)findViewById(R.id.Button1);
       b1.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {
               // TODO Auto-generated method stub
               Intent i = new Intent(MainActivity.this, ContactActivity.class);
               startActivity(i);
           }
       });

       b2=(Button)findViewById(R.id.Button2);
       b2.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {
               // TODO Auto-generated method stub
               Intent i = new Intent(getApplicationContext(),NoticeActivity.class);
               startActivity(i);
           }
       });

       b3=(Button)findViewById(R.id.Button3);
       b3.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {
               // TODO Auto-generated method stub
               Intent i = new Intent(getApplicationContext(),DailyDiaryActivity.class);
               startActivity(i);
           }
       });

       b4=(Button)findViewById(R.id.Button4);
       b4.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {
               // TODO Auto-generated method stub
               Intent i = new Intent(getApplicationContext(),EventsActivity.class);
               startActivity(i);
           }
       });

       b5=(Button)findViewById(R.id.Button5);
       b5.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {
               // TODO Auto-generated method stub
               Intent i = new Intent(getApplicationContext(),SyllabusActivity.class);
               startActivity(i);
           }
       });

       b6=(Button)findViewById(R.id.Button6);
       b6.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {
               // TODO Auto-generated method stub
               Intent i = new Intent(getApplicationContext(),ResultsActivity.class);
               startActivity(i);
           }
       });

    }


}

ContactActivity 文件

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

/**
 * Created by Haris on 5/1/2017.
 */

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

您的代码没有任何错误。错误来自模拟器。

转到 Tools > Android > AVD Manager > Edit 虚拟设备并取消选中“Use Host GPU

实际上,模拟器太慢了,我建议您使用真正的 android 设备来 运行 应用程序。