无法解析符号上下文

时间:2019-09-15 12:45:40

标签: android

我对Android开发还很陌生,我刚刚看了一段视频,展示了如何向Android添加滑块。我面临的这个问题是context (Cannot Resolve Symbol Context)。在所有三个点上,它都显示为红色。

public class MainActivity extends AppCompatActivity {

    ViewFlipper v_flipper;

    int[] imageos={

            R.drawable.slider,
            R.drawable.slider,
            R.drawable.slider,
            R.drawable.slider,
            R.drawable.slider,

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        v_flipper=findViewById(R.id.v_flipper);

        for (int i=0;i<imageos.length;i++){
            flip_image(imageos[i]);
        }

    }

    private void flip_image(int i) {

        ImageView view=new ImageView(context:this);
        view.setBackgroundResource(i);
        v_flipper.addView(view);
        v_flipper.setFlipInterval(4000);
        v_flipper.setAutoStart(true);

        v_flipper.setInAnimation(context: this,android.R.anim.slide_in_left);
        v_flipper.setOutAnimation(context: this,android.R.anim.slide_out_right);
    }

}

2 个答案:

答案 0 :(得分:0)

将行更改为:

ImageView view=new ImageView(getApplicationContext());
v_flipper.setInAnimation(getApplicationContext(),android.R.anim.slide_in_left);
v_flipper.setOutAnimation(getApplicationContext(),android.R.anim.slide_out_right);

如果这是一项活动,则可以使用this代替getApplicationContext()

答案 1 :(得分:0)

在您的课程中首先定义上下文

ViewFlipper v_flipper;
    Context context;

int[] imageos={

        R.drawable.slider,
        R.drawable.slider,
        R.drawable.slider,
        R.drawable.slider,
        R.drawable.slider,

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    v_flipper=findViewById(R.id.v_flipper);
context=this;
    for (int i=0;i<imageos.length;i++){
        flip_image(imageos[i]);
    }

}

private void flip_image(int i) {

    ImageView view=new ImageView(context);
    view.setBackgroundResource(i);
    v_flipper.addView(view);
    v_flipper.setFlipInterval(4000);
    v_flipper.setAutoStart(true);

    v_flipper.setInAnimation(context,android.R.anim.slide_in_left);
    v_flipper.setOutAnimation(context,android.R.anim.slide_out_right);
}
}