Reputation: 155
I have the following error:
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)
In this code where I would like to get Resources:
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;
}
}
Upvotes: 1
Views: 354
Reputation: 138
You dont pass HomeActivity.INTENT_KEY key to fragment in newInstance method
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
Upvotes: -1
Reputation: 51411
Your app crashes because this line:
getActivity().getIntent().getIntExtra(HomeActivity.INTENT_KEY, 1)
returns an Integer
that does not correspond to a resource.
I suggest you check whatever Integer
you are handing over to your Intent
.
Also, it is highly unlikely that your default value of 1
represents a resource.
Upvotes: 3