从Fragment访问时的ResourceNotFoundException

时间:2015-11-25 16:27:11

标签: android android-fragments

我有以下错误:

 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;
    }
}

2 个答案:

答案 0 :(得分:3)

您的应用崩溃了,因为这一行:

getActivity().getIntent().getIntExtra(HomeActivity.INTENT_KEY, 1)

返回与资源不对应的Integer 。 我建议您检查一下Integer您要移交给Intent的任何内容。

此外,您的默认值1极不可能代表资源。

答案 1 :(得分:-1)

你没有将HomeActivity.INTENT_KEY键传递给newInstance方法中的片段

   args.putInt("someInt", page);
   args.putString("someTitle", title);
   fragmentFirst.setArguments(args);