Imageresource无法使其工作

时间:2015-01-19 17:45:36

标签: resources fragment

我正在片段中工作,但我无法使图像资源工作。

package hej.swipeview;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;

public class FörstängningFragment extends Fragment {

int checkOne = '0';
int checkTwo = '0';
int checkThree = '0';
ImageView bild = (ImageView)
        getView().findViewById(R.id.forstangningimage);


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_forstangning, container, false);
    int imageresource = getResources().getDrawable(R.drawable.forstangning);
    bild.setImageResource(imageresource);

}


public void onCheckboxClicked(View view) {
    boolean checked = ((CheckBox) view).isChecked();

    checkOne = '0';
    checkTwo = '0';
    checkThree = '0';

    switch (view.getId()) {

        case R.id.checkboxbak:

            if (checked) {
                checkOne = '1';
                changeImage(); }

            else {
                checkOne = '0';
                changeImage(); }
            break;


        case R.id.checkBox2:
            if (checked) {
                checkTwo = '1';
                changeImage(); }
            else {
                checkTwo = '0';
                changeImage(); }
            break;

        case R.id.checkBox3:
            if (checked) {
                checkThree = '1';
                changeImage(); }
            else {
                checkThree = '0';
                changeImage(); }
            break;
    }
    changeImage(); {

        if (checkOne != '1' && checkTwo != '1' && checkThree != '1') {

            bild.setImageResource(R.drawable.forstangning);
        } else {

            if (checkOne == '1') {
                bild.setImageResource(R.drawable.splash2);
            } else if (checkOne == '1' && checkTwo == '1') {
                bild.setImageResource(R.drawable.dimensioner);
            } else if (checkOne == '1' && checkTwo == '1' && checkThree == '1') {
                bild.setImageResource(R.drawable.lastmonster);
            } else if (checkOne == '1' && checkThree == '1') {
                bild.setImageResource(R.drawable.ic_launcher);
            } else if (checkTwo == '1') {
                bild.setImageResource(R.drawable.ic_launcher);
            } else if (checkTwo == '1' && checkThree == '1') {
                bild.setImageResource(R.drawable.ic_launcher);
            } else if (checkThree == '1') {
                bild.setImageResource(R.drawable.ic_launcher);
            }
        }

    }
}

private void changeImage() {

}
}

这是我的错误:

int imageresource = getResources().getDrawable(R.drawable.forstangning);
        bild.setImageResource(imageresource);

1 个答案:

答案 0 :(得分:2)

您有无法访问的代码,因为这些行位于return语句之后。

您应该从ImageView方法中引用onCreate,如下所示:

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_forstangning, container, false);
            bild = (ImageView)rootView.findViewById(R.id.your_imageview_id);
            bild.setImageResource(R.drawable.forstangning);

            return rootView;
        }