Android开发基础练习,代码报错无法打开

activity_main.xml代码


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:background="@color/gdcpRed"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="个人信息"
            android:textSize="20sp"
            android:gravity="center"
            android:textColor="@color/gdcpGreen"
            android:textStyle="bold"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:text="日常安排"
            android:textSize="20sp"
            android:gravity="center"
            android:textColor="@color/gdcpGreen"
            android:textStyle="bold"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:orientation="horizontal">

        <View
            android:layout_width="0dp"
            android:layout_height="3dp"
            android:layout_weight="1"
            android:background="@color/gdcpGreen"/>

        <View
            android:layout_width="0dp"
            android:layout_height="10dp"
            android:layout_weight="1"/>

    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:text="姓名  "
            android:textSize="25sp"
            android:textStyle="bold"
            android:gravity="right|center_vertical"
            android:background="@color/gdcpAsh"/>

        <EditText
            android:id="@+id/Information_Name"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="2"
            android:paddingLeft="7dp"
            android:hint="输入您的名字"
            android:background="@null"
            android:textColor="@color/gdcpSizeAsh"
            android:maxLength="10"
            android:maxLines="1"/>

    </LinearLayout>

<!--    分割线-->
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/gdcpTransverseAsh"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:text="头像  "
            android:textSize="25sp"
            android:textStyle="bold"
            android:gravity="right|center_vertical"
            android:background="@color/gdcpAsh"/>

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"/>

    </LinearLayout>

    <!--    分割线-->
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/gdcpTransverseAsh"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:text="年龄  "
            android:textSize="25sp"
            android:textStyle="bold"
            android:gravity="right|center_vertical"
            android:background="@color/gdcpAsh"/>

        <EditText
            android:id="@+id/Information_Age"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="2"
            android:paddingLeft="7dp"
            android:hint="输入您的年龄"
            android:background="@null"
            android:textColor="@color/gdcpSizeAsh"
            android:maxLength="3"
            android:inputType="number"/>

    </LinearLayout>

    <!--    分割线-->
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/gdcpTransverseAsh"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:text="性别  "
            android:textSize="25sp"
            android:textStyle="bold"
            android:gravity="right|center_vertical"
            android:background="@color/gdcpAsh"/>

        <RadioGroup
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:orientation="horizontal">

            <RadioButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="男"
                android:textSize="22sp"
                android:textStyle="bold"
                android:textColor="@color/gdcpSizeAsh"/>

            <RadioButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="女"
                android:textSize="22sp"
                android:textStyle="bold"
                android:textColor="@color/gdcpSizeAsh"/>

        </RadioGroup>

    </LinearLayout>

    <!--    分割线-->
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/gdcpTransverseAsh"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/gdcpAsh"
            android:gravity="right|center_vertical"
            android:text="班级  "
            android:textSize="25sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn_class"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@android:color/white"
            android:text="  点击选择班级"
            android:textSize="18sp"
            android:gravity="left|center_vertical"
            android:textStyle="bold"
            android:textColor="@color/gdcpSizeAsh"/>

    </LinearLayout>

    <!--    分割线-->
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/gdcpTransverseAsh"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/gdcpAsh"
            android:gravity="right|center_vertical"
            android:text="兴趣  "
            android:textSize="25sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn_interesting"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@android:color/white"
            android:text="  点击选择兴趣"
            android:textSize="18sp"
            android:gravity="left|center_vertical"
            android:textStyle="bold"
            android:textColor="@color/gdcpSizeAsh"/>

    </LinearLayout>

    <!--    分割线-->
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/gdcpTransverseAsh"/>

<!--    TODO:生日选择器-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/gdcpAsh"
            android:gravity="right|center_vertical"
            android:text="出生日期  "
            android:textSize="25sp"
            android:textStyle="bold" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:orientation="vertical">

                <Button
                    android:id="@+id/btn_date"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@android:color/white"
                    android:text="  选择出生日期"
                    android:textSize="18sp"
                    android:gravity="left|center_vertical"
                    android:textStyle="bold"
                    android:textColor="@color/gdcpSizeAsh" />

                <DatePicker
                    android:id="@+id/dp_date"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:calendarViewShown="false"
                    android:datePickerMode="calendar"
                    android:gravity="center"
                    android:spinnersShown="true"/>

            </LinearLayout>

    </LinearLayout>

    <!--    分割线-->
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/gdcpTransverseAsh"/>

<!--    TODO:提交信息并且输出之Toast-->

</LinearLayout>

colors.xml代码


```xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="gdcpRed">#8F000B</color>
    <color name="gdcpGreen">#008000</color>
    <color name="gdcpAsh">#EDEBEC</color>
    <color name="gdcpTransverseAsh">#CDCBCC</color>
    <color name="gdcpLongitudinalAsh">#E4E2E3</color>
    <color name="gdcpSizeAsh">#A9A9A9</color>
</resources>

MainActivity.java代码

package com.example.myapplication2;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public abstract class MainActivity extends AppCompatActivity {

    // 多选兴趣修改
    private String[] interesting = {"跑步", "游泳", "跳绳", "骑行"};
    private boolean[] clickInteresting = {false, false, false, false};

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

        EditText editText = findViewById(R.id.Information_Name);

        Button selectClassButton = findViewById(R.id.btn_class);

        Button selectIntButton = findViewById(R.id.btn_interesting);

        // 姓名填写
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showInputDialog(editText);
            }
        });

        // 班级选择
        selectClassButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showClassSelectionDialog(selectClassButton);
            }
        });

        // 兴趣选择
        selectIntButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showIntselectionDialog(selectIntButton);
            }
        });

    }

    // 退出系统功能
    public void onBackPressed(){
        // 声明对象
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("是否退出应用?")
                .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss(); // 关闭对话框
                    }
                })
                // 添加取消按钮
                .setNegativeButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        MainActivity.this.finish(); // 关闭MainActivity
                    }
                });
        dialog = builder.create();
        dialog.show();
    }


    // 姓名输入对话框
    // 监听器
    private void showInputDialog(final EditText editText){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("输入您的名字");

        // 创建一个EditText视图用于用户输入
        final EditText inputEditText = new EditText(this);
        builder.setView(inputEditText);

        // 添加确定按钮和取消按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 获取用户输入的文本
                String inputText = inputEditText.getText().toString();
                editText.setText(inputText);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.show();
    }

    // 选择班级
    private void showClassSelectionDialog(final Button selectClassButton) {
        final String[] classes = {"22软件技术1班", "22软件技术2班", "22软件技术3班", "22软件技术4班"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("选择班级")
                .setSingleChoiceItems(classes, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 用户点击了某个班级
                        String selectedClass = classes[which];
                        // 更新Button文本
                        selectClassButton.setText("  " + selectedClass);

                        // Toast提示欢迎加入班级
                        Toast.makeText(MainActivity.this, selectedClass + " 欢迎您加入", Toast.LENGTH_SHORT).show();

                        dialog.dismiss(); // 关闭对话框
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    // 选择兴趣
    private void showIntselectionDialog(final Button selectIntButton){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("选择兴趣")
                .setMultiChoiceItems(interesting, clickInteresting, new DialogInterface.OnMultiChoiceClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        clickInteresting[which] = isChecked;
                    }
                })
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        updateButtonLabel(selectIntButton);
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    // 兴趣多选判断
    private void updateButtonLabel(final Button selectIntButton) {
        StringBuilder selectedInterests = new StringBuilder();
        for (int i = 0; i < interesting.length; i++) {
            if (clickInteresting[i]) {
                selectedInterests.append(interesting[i]).append(", ");
            }
        }
        if (selectedInterests.length() > 0) {
            selectedInterests.delete(selectedInterests.length() - 2, selectedInterests.length());
        } else {
            selectedInterests.append("无");
        }

        selectIntButton.setText("  " + selectedInterests.toString());
    }

}


报错代码
FATAL EXCEPTION: main
                                                                                                    Process: com.example.myapplication2, PID: 4120
                                                                                                    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myapplication2/com.example.myapplication2.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.myapplication2.MainActivity> cannot be instantiated
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2888)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3093)
                                                                                                        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
                                                                                                        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
                                                                                                        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1823)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loop(Looper.java:193)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6825)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
                                                                                                    Caused by: java.lang.InstantiationException: java.lang.Class<com.example.myapplication2.MainActivity> cannot be instantiated
                                                                                                        at java.lang.Class.newInstance(Native Method)
                                                                                                        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
                                                                                                        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
                                                                                                        at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2876)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3093) 
                                                                                                        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
                                                                                                        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
                                                                                                        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1823) 
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                                        at android.os.Looper.loop(Looper.java:193) 
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6825) 
                                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860
我本来想添加新功能是关于日期选择器,在操作过程中,报错就用Alt + 回车 修改,发现一直报错,打不开软件,我就把与日期选择器有关的代码删除依旧进不去

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/512749757496136.jpg "#left")

已经找到原因了
MainActivity.java中第十一行多了abstract 所以导致无法加载

【以下回答由 GPT 生成】

我很抱歉,但是你没有提供具体的问题,因此我无法为你提供解决方案。请提供一个具体的问题,我将尽力帮助你解决。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^