Stefano
Stefano

Reputation: 199

android.content.res.Resources$NotFoundException: Resource ID #0x7f07006e

Actually, I have no idea why I have this error.

Follows the code hoping that there is everything useful for a diagnosis.

The structure of the app is a standard Navigation Drawer Activity with a Fragment inside. The Fragment has a RecyclerView.

The device used is Motorola XT1068 (Android 6.0, API 23).

Logcat

03-26 16:07:54.340 20272-20272/com.example.ves.gennaio3 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ves.gennaio3, PID: 20272
    android.content.res.Resources$NotFoundException: Resource ID #0x7f07006e
        at android.content.res.Resources.getValue(Resources.java:1351)
        at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:330)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:195)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
        at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
        at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:85)
        at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:94)
        at com.example.ves.gennaio3.models.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:38)
        at com.example.ves.gennaio3.models.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:17)
        at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
        at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
        at android.view.View.layout(View.java:16646)
        at android.view.ViewGroup.layout(ViewGroup.java:5440)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
        at android.view.View.layout(View.java:16646)
        at android.view.ViewGroup.layout(ViewGroup.java:5440)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at android.view.View.layout(View.java:16646)
        at android.view.ViewGroup.layout(ViewGroup.java:5440)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
        at android.view.View.layout(View.java:16646)
        at android.view.ViewGroup.layout(ViewGroup.java:5440)
        at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1166)
        at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:851)
        at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:870)
        at android.view.View.layout(View.java:16646)
        at android.view.ViewGroup.layout(ViewGroup.java:5440)
        at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1172)
        at android.view.View.layout(View.java:16646)
        at android.view.ViewGroup.layout(ViewGroup.java:5440)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at android.view.View.layout(View.java:16646)
        at android.view.ViewGroup.layout(ViewGroup.java:5440)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
        at android.view.View.layout(View.java:16646)
        at android.view.ViewGroup.layout(ViewGroup.java:5440)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
        a
        03-26 16:07:55.215 15332-18804/? E/AudioRecord-JNI: Error -4 during AudioRecord native read

Dependencies

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:26.1.0'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

MainActivity.java

package com.example.ves.gennaio3;

import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements CardFragment.OnFragmentInteractionListener,
        NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        if(findViewById(R.id.fragment_container)!=null) {
            if (savedInstanceState!=null) {return;}
            CardFragment cardFragment = new CardFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, cardFragment).commit();

        }
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.nav_camera) {


        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }




        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

CardFragment.java

package com.example.ves.gennaio3;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.ves.gennaio3.models.ItemObject;
import com.example.ves.gennaio3.models.RecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link CardFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link CardFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CardFragment extends Fragment {
    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public CardFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CardFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CardFragment newInstance(String param1, String param2) {
        CardFragment fragment = new CardFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_card, container, false);
        rootView.setTag("CardFragment");

        List<ItemObject> rowListItem = getAllItemList();
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);

        RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(getActivity(), rowListItem);
        mRecyclerView.setAdapter(mAdapter);

        return rootView;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

    private List<ItemObject> getAllItemList(){

        List<ItemObject> allItems = new ArrayList<ItemObject>();
        allItems.add(new ItemObject("United States", R.drawable.newyork));
        allItems.add(new ItemObject("Canada", R.drawable.canada));
        allItems.add(new ItemObject("United Kingdom", R.drawable.uk));
        allItems.add(new ItemObject("Germany", R.drawable.germany));
        allItems.add(new ItemObject("Sweden", R.drawable.sweden));

        return allItems;
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.example.ves.gennaio3.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_container">
    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

fragment_card.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ves.gennaio3.CardFragment">

    <RelativeLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <android.support.v7.widget.RecyclerView
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/recycler_view"
            android:scrollbars="vertical"/>

    </RelativeLayout>

</FrameLayout>

UPDATE

card_view_list.xml (my RecycleView item layout)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginBottom="16dp"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/card_view">


<RelativeLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/country_photo"
        android:scaleType="centerCrop"
        android:layout_centerInParent="true"
        android:contentDescription="@string/action_settings"/>

    <TextView
        android:id="@+id/country_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="4dp"
        android:gravity="center"
        android:text="Nigeria"
        android:textSize="30sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="13dp"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"
        android:text="Get" />

</RelativeLayout>

</android.support.v7.widget.CardView>

RecyclerViewAdapter.java

package com.example.ves.gennaio3.models;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.ves.gennaio3.R;

import java.util.List;

/**
 * Created by VES on 03/02/2018.
 */

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders>{

    private List<ItemObject> itemList;
    private Context context;

    public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
        RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
        return rcv;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolders holder, int position) {
        holder.countryName.setText(itemList.get(position).getName());
        holder.countryPhoto.setImageResource(itemList.get(position).getPhoto());
    }

    @Override
    public int getItemCount() {
        return this.itemList.size();
    }

}

RecyclerViewHolders.java

package com.example.ves.gennaio3.models;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ves.gennaio3.R;

/**
 * Created by VES on 03/02/2018.
 */

public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{

    public TextView countryName;
    public ImageView countryPhoto;
    public Button button;

    public RecyclerViewHolders(final View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        countryName = (TextView)itemView.findViewById(R.id.country_name);
        countryPhoto = (ImageView)itemView.findViewById(R.id.country_photo);
        button = (Button)itemView.findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                Toast.makeText(itemView.getContext(), "questa è prova", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(view.getContext(), "Clicked Country Position = " + getPosition(), Toast.LENGTH_SHORT).show();
    }


}

UPDATE

One of the answers suggests that the problem could be related to getAllItems(). But resources invoked seem to be correctly loaded.

enter image description here

Upvotes: 28

Views: 20689

Answers (3)

Sana Ebadi
Sana Ebadi

Reputation: 7220

maybe you have a resource in drawable-v24 . just move them to drawable.

Upvotes: 5

serg3z
serg3z

Reputation: 1862

Your problem is the wrong placement of resources(images). You use (API 23) but resources for API 23 is null, because they only API 24 and up.

enter image description here

Now all images marked (v24) this means that they are in drawables-v24 just move in drawable

enter image description here

that'll solve your problem.

Upvotes: 53

Cheticamp
Cheticamp

Reputation: 62831

It looks like your issue is with this line:

holder.countryPhoto.setImageResource(itemList.get(position).getPhoto());

itemList.get(position).getPhoto() must return a valid ResId. (Refer to the ImageView documentation.

As you mention, itemList looks to be set up correctly. I suggest that you set a breakpoint at the above line and see what is being returned from getPhoto(). You should also be able to determine if this is the line causing your exception.

If that doesn't help, post ItemObject here so we can get a look at getPhoto() and maybe a couple of other things.


I just noticed the V24 by the drawables in the image you posted. I agree with Dennis K's comment that your phone is API23 and you have drawables for API 24+. Android will not select these drawables if the API level is less than API 24. If these drawables are suitable for API 23, move them to a folder that supports API 23.

See Providing Resources.

Platform Version (API level) Examples: v3 v4 v7 etc.

The API level supported by the device. For example, v1 for API level 1 (devices with Android 1.0 or higher) and v4 for API level 4 (devices with Android 1.6 or higher). See the Android API levels document for more information about these values.

Upvotes: 3

Related Questions