实际上,我不知道为什么会出现这个错误。
遵循代码,希望有一切对诊断有用。
应用程序的结构是标准Navigation Drawer Activity
,里面有Fragment
。 Fragment
有一个RecyclerView
。
使用的设备是 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 {
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>
更新
card_view_list.xml (我的RecycleView
项目布局)
<?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();
}
}
更新
其中一个答案表明问题可能与getAllItems()有关。但是调用的资源似乎已正确加载。
答案 0 :(得分:20)
您的问题是资源(图片)放置错误。 您使用(API 23)但API 23的资源为空,因为它们只有API 24及更高版本。
现在所有标记的图像(v24)这意味着它们都在drawables中 - v24只是在drawable中移动
解决你的问题。
答案 1 :(得分:1)
看起来您的问题在于这一行:
holder.countryPhoto.setImageResource(itemList.get(position).getPhoto());
itemList.get(position).getPhoto()
必须返回有效的ResId。 (请参阅ImageView
documentation。
正如您所提到的,itemList
看起来要正确设置。我建议你在上面一行设置一个断点,看看从getPhoto()
返回的是什么。您还应该能够确定这是否是导致您的异常的行。
如果这没有帮助,请在此处发布ItemObject
,以便我们查看getPhoto()
以及其他一些内容。
我刚注意到你发布的图片中的drawables的V24。我同意Dennis K的评论,即你的手机是API23,而且你有API 24+的绘图。如果API级别低于API 24,Android将不会选择这些drawable。如果这些drawable适用于API 23,请将它们移动到支持API 23的文件夹。
平台版本(API级别)示例: V3 V4 V7 等。
设备支持的API级别。例如,API级别1的v1(Android 1.0或更高版本的设备)和API级别4的v4(Android 1.6或更高版本的设备)。有关这些值的详细信息,请参阅Android API级别文档。
答案 2 :(得分:1)
也许您在 drawable-v24 中有资源。只需将主题移至可绘制。