我有以下错误:
FATAL EXCEPTION: main
Process: com.vadimsleonovs.horoscope, PID: 7052
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x1
at android.content.res.Resources.getResourceEntryName(Resources.java:2083)
at com.vadimsleonovs.horoscope.FirstFragment.onCreateView(FirstFragment.java:62)
在此代码中,我想获取资源:
public class FirstFragment extends Fragment {
TextView mDateTextView;
TextView mHoroscopeTextView;
TextView mSignTextView;
String mSignName;
ImageView mSignImageView;
int mExtra;
Intent mIntent;
// Store instance variables
private String title;
private int page;
//Received stuff
String mDailyHoroscope = "";
String mZodiacSign = "";
String mHoroscopeDate = "";
// newInstance constructor for creating fragment with arguments
public static FirstFragment newInstance(int page, String title) {
FirstFragment fragmentFirst = new FirstFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
ViewPager pager = new ViewPager(getContext());
mIntent = getActivity().getIntent();
mExtra = mIntent.getIntExtra(HomeActivity.INTENT_KEY, 1);
}
// Inflate the view for the fragment based on layout XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel1);
mDateTextView = (TextView) view.findViewById(R.id.date_label_f1);
mHoroscopeTextView = (TextView) view.findViewById(R.id.horoscope_label_f1);
mSignTextView = (TextView) view.findViewById(R.id.zodiac_label_f1);
mSignImageView = (ImageView) view.findViewById(R.id.image_label_f1);
**//Problem is here**
mSignName = view.getResources().getResourceEntryName(mExtra);
int resID = view.getResources().getIdentifier(mSignName, "drawable", getActivity().getPackageName());
mSignImageView.setImageResource(resID);
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(view.getContext());
mHoroscopeDate = mSharedPreferences.getString("date:", "data hz");
mDailyHoroscope = mSharedPreferences.getString(mSignName, "hz goroskop tvoj");
mZodiacSign = mSignName;
mDateTextView.setText(mHoroscopeDate);
AutofitHelper.create(mDateTextView);
mHoroscopeTextView.setText(mDailyHoroscope);
AutofitHelper.create(mHoroscopeTextView);
String Caps = mZodiacSign.substring(0,1).toUpperCase() + mZodiacSign.substring(1);
mSignTextView.setText(Caps);
AutofitHelper.create(mSignTextView);
tvLabel.setText(page + " -- " + title);
return view;
}
}
最佳答案
您的应用程序崩溃,因为此行:
getActivity().getIntent().getIntExtra(HomeActivity.INTENT_KEY, 1)
返回与资源不对应的
Integer
。我建议您检查要移交给
Integer
的任何Intent
。另外,您的默认值
1
极不可能代表资源。关于android - 从Fragment访问时出现ResourceNotFoundException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33921615/