skip to Main Content

Errors:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 8501
android.content.res.Resources$NotFoundException: Resource ID #0x7f070058
at android.content.res.Resources.getValue(Resources.java:1266)
at androidx.appcompat.widget.ResourceManagerInternal.loadDrawableFromDelegates(ResourceManagerInternal.java:255)
at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:142)
at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:135)
at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:104)
at androidx.appcompat.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:505)
at androidx.appcompat.view.menu.ListMenuItemView.initialize(ListMenuItemView.java:131)
at androidx.appcompat.view.menu.MenuAdapter.getView(MenuAdapter.java:109)
at androidx.appcompat.view.menu.MenuPopup.measureIndividualMenuWidth(MenuPopup.java:161)
at androidx.appcompat.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:174)
at androidx.appcompat.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:208)
at androidx.appcompat.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:296)
at androidx.appcompat.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:182)
at androidx.appcompat.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:792)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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)

FirstScreenActivity:

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

public class FirstScreenActivity extends AppCompatActivity {
    public static final String EXTRA_OPTION="category";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_screen);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        {
            getMenuInflater().inflate(R.menu.mymenu,menu);
            return true;
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //get the item id selected
        String option = "default";

        //update option according to the selected item
        switch(item.getItemId())
        {
            case R.id.Animals:
            {
                option = "Animals";
                break;
            }
            case R.id.Sport:
            {
                option = "Sport";
                break;
            }
        }
        //send intent to secondScreenActivity with parameter options
        Intent intent = new Intent(this, SecondScreenActivity.class);
        intent.putExtra(EXTRA_OPTION,option);
        startActivity(intent);
        finish();
        return true;
    }
}

SecondScreenActivity:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class SecondScreenActivity extends AppCompatActivity {

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

        TextView tvCat = findViewById(R.id.tvCat);
        ImageView ivPic = findViewById(R.id.ivPic);

        Intent intent = getIntent();
        String option = intent.getStringExtra(FirstScreenActivity.EXTRA_OPTION);

        switch(option)
        {
            case "Animals":
            {
                tvCat.setText("Animals");
                tvCat.setTextSize(30);
                ivPic.setImageResource(R.drawable.butterfly);
            }
            case "Sport":
            {
                tvCat.setText("Sport");
                tvCat.setTextSize(30);
                ivPic.setImageResource(R.drawable.basketball);
            }
        }
    }
}

Manifest:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="MyApplication"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity android:name=".SecondScreenActivity"></activity>
        <activity android:name=".FirstScreenActivity"
            android:exported="true">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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



    </application>

</manifest>

SecondActivity 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=".SecondScreenActivity"
    android:weightSum="10">
    <TextView
        android:id="@+id/tvCat"
        android:layout_weight="5"
        android:layout_width="match_parent"
        android:layout_height="0dp" />

    <ImageView
        android:id="@+id/ivPic"
        android:layout_weight="5"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

</LinearLayout>

mymenu XML:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/Sport"
        android:icon="@drawable/basketball"
        android:orderInCategory="100"
        android:title="Sport"
        app:showAsAction="never"/>
    <item
        android:id="@+id/Animals"
        android:icon="@drawable/butterfly"
        android:orderInCategory="100"
        android:title="Animals"
        app:showAsAction="never"
        />
</menu>

That’s it, I’m trying to create an options menu with pictures (1 of basketball and 1 of butterfly) when you click on the item.

If you need the whole ZIP, here it is: Download the ZIP file

Thanks in advance!:)

2

Answers


  1. The problem is in your butterfly.png & basketball.png placed wrong directory.

    Just move those image drawable-v24 to drawable

    Login or Signup to reply.
  2. The answer mentioned by Ganesh MB is correct.But I noticed one more issue.It is in your manifest.I see that the secondActivity does no have exported line in it.

    So,replace

    <activity android:name=".SecondScreenActivity"></activity>
    

    in your manifest to

    <activity android:name=".SecondScreenActivity"
        android:exported="true"/>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search