Home > database >  When I click the button the second Activity doesn't show up and my Android Studio App closes
When I click the button the second Activity doesn't show up and my Android Studio App closes

Time:02-27

When I click the cardView with the id addNotice in the activity_main.xml the second Activity UploadNotice is supposed to show up, but my Android studio app closes and the second Activity that I've created never shows up. I'm new to Android Studio and Java so I would appreciate it a lot if anyone can help me!

Here is my MainActivity.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;
        }


    }
}

and Here is my 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"
    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>

Also here is the AndroidManifest.xml just in case :

<?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>

Here is the second Activity UploadNotice.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);
        }
    }
}

and Here is the second Activity Xml code :

<?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>

And this is the Logcat when I click the Cardview button

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

there are two themes.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>

CodePudding user response:

In the styles.xml file, change the theme to something like this:

<style name="Theme.AdminUniversityApp" parent="Theme.Material3.DayNight">
    ...
</style>
  • Related