当前位置: 首页 > 知识库问答 >
问题:

当我点击按钮时,第二个活动不会出现,我的Android Studio应用程序关闭

童宏富
2023-03-14

当我在activity_main中单击id为addNotice的cardView时。xml第二个活动上传通知应该会出现,但我的Android studio应用程序关闭,我创建的第二个活动从未出现。我是Android Studio和Java的新手,如果有人能帮助我,我将非常感激!

这是我的主要活动。爪哇:

package com.example.adminuniversityapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    CardView uploadNptice;

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


        uploadNptice = findViewById(R.id.addNotice);

        uploadNptice.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.addNotice:
                Intent intent = new Intent( MainActivity.this,UploadNotice.class);
                startActivity(intent);
                break;
        }


    }
}

这是我的主要活动。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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:gravity="center"
        android:orientation="horizontal">
        <com.google.android.material.card.MaterialCardView
            android:id="@+id/addNotice"
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            app:cardElevation="5dp">

            <LinearLayout
                android:layout_width="135dp"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circle_green"
                    android:padding="15dp"
                    android:src="@drawable/ic_notice" />

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:padding="5dp"
                    android:text="Upload Notice"
                    android:textColor="@color/textColor"
                    android:textStyle="bold" />


            </LinearLayout>
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/addGalleryImage"
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            app:cardElevation="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circle_purple"
                    android:padding="15dp"
                    android:src="@drawable/ic_gallery"/>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="10dp"
                    android:background="@color/lightGray"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:padding="5dp"
                    android:text="Upload Image"
                    android:textColor="@color/textColor"
                    android:textStyle="bold"/>


            </LinearLayout>
        </com.google.android.material.card.MaterialCardView>



    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:gravity="center"
        android:orientation="horizontal">
        <com.google.android.material.card.MaterialCardView
            android:id="@+id/addEbook"
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            app:cardElevation="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circle_pink"
                    android:padding="15dp"
                    android:src="@drawable/ic_upload"/>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="10dp"
                    android:background="@color/lightGray"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:padding="5dp"
                    android:text="Upload Ebook"
                    android:textColor="@color/textColor"
                    android:textStyle="bold"/>


            </LinearLayout>
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/faculty"
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            app:cardElevation="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circle_yellow"
                    android:padding="15dp"
                    android:src="@drawable/ic_group"/>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="10dp"
                    android:background="@color/lightGray"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:padding="5dp"
                    android:text="Update Faculty"
                    android:textColor="@color/textColor"
                    android:textStyle="bold"/>


            </LinearLayout>
        </com.google.android.material.card.MaterialCardView>



    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:gravity="center"
        android:orientation="horizontal">
        <com.google.android.material.card.MaterialCardView
            android:id="@+id/deleteNotice"
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            app:cardElevation="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:background="@drawable/cicle_red"
                    android:padding="15dp"
                    android:src="@drawable/ic_delete"/>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="10dp"
                    android:background="@color/lightGray"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:padding="5dp"
                    android:text="Delete Notice"
                    android:textColor="@color/textColor"
                    android:textStyle="bold"/>


            </LinearLayout>
        </com.google.android.material.card.MaterialCardView>





    </LinearLayout>



</LinearLayout>

这里还有AndroidManifest。xml以防万一:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.adminuniversityapp">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

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

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

</manifest>

下面是第二个活动上传otice.java:

package com.example.adminuniversityapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;

import java.io.IOException;

public class UploadNotice extends AppCompatActivity {


    private CardView addImage;

    private ImageView noticeImageView;

    private final int REQ = 1;

    private Bitmap bitmap;


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


        addImage = findViewById(R.id.addImage);

        noticeImageView = findViewById(R.id.noticeImageView);

        addImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openGallery();
            }
        });
    }

    private void openGallery() {
        Intent pickImage = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(pickImage, REQ);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQ && resultCode == RESULT_OK){
            Uri uri = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),uri);
            } catch (IOException e) {
                e.printStackTrace();
            }
            noticeImageView.setImageBitmap(bitmap);
        }
    }
}

下面是第二个活动Xml代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".UploadNotice"
    android:padding="16dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <com.google.android.material.card.MaterialCardView
            android:id="@+id/addImage"
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            app:cardElevation="5dp"
            android:layout_gravity="center">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circle_purple"
                    android:padding="15dp"
                    android:src="@drawable/ic_image"/>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="10dp"
                    android:background="@color/lightGray"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:padding="5dp"
                    android:text="Select Image"
                    android:textColor="@color/textColor"
                    android:textStyle="bold"/>


            </LinearLayout>
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="16dp">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/noticeTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Notice title" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.button.MaterialButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Upload Notice"
            android:layout_marginTop="16dp"
            android:textAllCaps="false"
            android:id="@+id/uploadNoticeBtn"/>
        <com.google.android.material.card.MaterialCardView
            android:layout_marginTop="16dp"
            android:layout_width="match_parent"
            android:layout_height="400dp">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                android:id="@+id/noticeImageView"/>
        </com.google.android.material.card.MaterialCardView>

    </LinearLayout>

</ScrollView>

这是我点击Cardview按钮时的Logcat

2022-02-26 16:09:00.615 26302-26302/com.example.adminuniversityapp I/Timeline: Timeline: Activity_launch_request time:449776768
2022-02-26 16:09:00.633 26302-26357/com.example.adminuniversityapp V/FA: Recording user engagement, ms: 60057
2022-02-26 16:09:00.636 26302-26357/com.example.adminuniversityapp V/FA: Using local app measurement service
2022-02-26 16:09:00.641 26302-26357/com.example.adminuniversityapp V/FA: Activity paused, time: 598904714
2022-02-26 16:09:00.651 26302-26302/com.example.adminuniversityapp V/FA: onActivityCreated
2022-02-26 16:09:00.656 26302-26302/com.example.adminuniversityapp D/DecorView[]: getWindowModeFromSystem  windowmode is 1
2022-02-26 16:09:00.667 26302-26357/com.example.adminuniversityapp V/FA: Connection attempt already in progress
2022-02-26 16:09:00.681 26302-26302/com.example.adminuniversityapp E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
2022-02-26 16:09:00.687 26302-26302/com.example.adminuniversityapp E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
2022-02-26 16:09:00.693 26302-26302/com.example.adminuniversityapp E/libc: Access denied finding property "ro.vendor.pref_scale_resolution"
2022-02-26 16:09:00.722 26302-26302/com.example.adminuniversityapp D/AndroidRuntime: Shutting down VM
2022-02-26 16:09:00.724 26302-26302/com.example.adminuniversityapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.adminuniversityapp, PID: 26302
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adminuniversityapp/com.example.adminuniversityapp.UploadNotice}: android.view.InflateException: Binary XML file line #55 in com.example.adminuniversityapp:layout/activity_upload_notice: Binary XML file line #55 in com.example.adminuniversityapp:layout/activity_upload_notice: Error inflating class com.google.android.material.textfield.TextInputLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3580)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3752)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2191)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:8049)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
     Caused by: android.view.InflateException: Binary XML file line #55 in com.example.adminuniversityapp:layout/activity_upload_notice: Binary XML file line #55 in com.example.adminuniversityapp:layout/activity_upload_notice: Error inflating class com.google.android.material.textfield.TextInputLayout
     Caused by: android.view.InflateException: Binary XML file line #55 in com.example.adminuniversityapp:layout/activity_upload_notice: Error inflating class com.google.android.material.textfield.TextInputLayout
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:856)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1012)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:963)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1142)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1103)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1145)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1103)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:684)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:536)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
        at com.example.adminuniversityapp.UploadNotice.onCreate(UploadNotice.java:32)
        at android.app.Activity.performCreate(Activity.java:8142)
        at android.app.Activity.performCreate(Activity.java:8114)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1308)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3553)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3752)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2191)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:8049)
        at java.lang.reflect.Method.invoke(Native Method)
2022-02-26 16:09:00.725 26302-26302/com.example.adminuniversityapp E/AndroidRuntime:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
     Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
        at com.google.android.material.internal.ThemeEnforcement.checkTextAppearance(ThemeEnforcement.java:185)
        at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:116)
        at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:474)
        at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:433)
            ... 30 more
2022-02-26 16:09:00.749 26302-26302/com.example.adminuniversityapp I/Process: Sending signal. PID: 26302 SIG: 9

有两个主题。xml:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.AdminUniversityApp" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.AdminUniversityApp" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_200</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_200</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

共有1个答案

皇甫福
2023-03-14

在styles.xml文件中,将主题更改为这样的内容:

<style name="Theme.AdminUniversityApp" parent="Theme.Material3.DayNight">
    ...
</style>
 类似资料:
  • 当我在设备上运行应用程序时单击增量按钮时,我的应用程序意外地强制执行这是我的activity_main.xml代码 这是我的mainactivity.java文件 这是调试报告05-23 22:14:45.695 297 36-29736/com.orton.birthdayCard E/AndroidRuntime:致命异常:主进程:com.orton.birthdayCard,PID:2973

  • 一切正常,应用程序出现了。但当我点击任何切换按钮时,应用程序就会崩溃。 我试过了,但找不到问题。其实我的知识还不够,我是这个领域的新手。所以请帮帮我。 这是查看活动。JAVA 还有这只logcat 2020-03-18 03:16:50.407 31609-31609/? E/lpaper。wallper:运行时设置的未知位_标志:0x8000 2020-03-18 03:17:04.862 31

  • 每当我单击登录页面中的“注册”按钮时,应用程序就会崩溃,无法移动到下一个活动。我已经在这个问题上纠缠了一天,似乎在任何地方都找不到解决办法。 下面是我为注册按钮方法编写的代码。(和是文本输入布局) Logcat在第7行显示错误,即ref.child(…行)。我尝试了第7行的替代方法,如:

  • 这是崩溃错误logcat,每次我时钟在我的寄存器btn应用程序崩溃。 我的模拟器是在Android模拟器设备上的2gb ram。 2019-09-22 16:36:39.307 6454-6454/com。康奈克斯。connexsocial E/AndroidRuntime:致命异常:主进程:com。康奈克斯。connexsocial,PID:6454Android系统。所容纳之物Activity

  • 请建议与android画中画模式相关的问题,如果我的应用程序有堆栈像家庭活动- 或 我想在应用程序图标启动时关闭PIP活动(PlayerActivity)。由于PIP活动在单独的任务中运行,如何关闭应用程序运行的所有其他任务?

  • 我是android新手,我正在学习一些教程,在这一点上,当我点击登录按钮时,应用程序不断崩溃。当我运行应用程序并单击登录按钮时,应用程序崩溃,在logcat中,它显示java RuntimeException的这个错误。这是我的logcat,loginactivity。xml和loginactivity。JAVA JAVAlang.RuntimeException:无法启动活动组件信息{com.e