“this”关键字不起作用

xoefb8l8  于 2021-07-13  发布在  Java
关注(0)|答案(4)|浏览(326)

我创造了一些 ImageView 他用的是 for 循环并将它们插入 ScrollView .
然后我需要再创造一个 ImageView 触摸屏幕时。这个 this 关键字不能用于创建 ImageView 在“####”标记位置。
我是个初学者。

public class MainActivity extends ActionBarActivity {

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

        final ScrollView scroll=new ScrollView(this);

        final LinearLayout layout =new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

        final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lparams.setMargins(0, 10, 0, 10);
        lparams.gravity=Gravity.CENTER;

        int i;

        for(i=0;i<15;i++) {
            //here 'this' is working ******************
            ImageView imView = new ImageView(this);
            imView.setLayoutParams(lparams);

            Drawable new_image = getResources().getDrawable(R.drawable.rakthasakshi);
            imView.setBackgroundDrawable(new_image);

            layout.addView(imView, lparams);

        }
        scroll.addView(layout);
        setContentView(scroll);

        scroll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if(event.getAction() == MotionEvent.ACTION_UP){

                    //but here says 'this' cannot be applied ###############
                    ImageView imView = new ImageView(this);  
                    imView.setLayoutParams(lparams);

                    Drawable new_image = getResources().getDrawable(R.drawable.rakthasakshi);
                    imView.setBackgroundDrawable(new_image);

                    layout.addView(imView, lparams);

                    scroll.addView(layout);
                    setContentView(scroll);

                    return true;
                }
                return false;
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
rn0zuynd

rn0zuynd1#

使用 ImageView imView = new ImageView(MainActivity.this); .
在第一种情况下, thisMainActivity 已经有了。
然而,在第二种情况下, this 实际上是指 View.OnTouchListener .

2nc8po8w

2nc8po8w2#

你知道什么吗 this 你的意思是? this 引用类型为的匿名类 View.OnTouchListener .
引用类的对象 MainActivity 你必须参考 MainActivity.this .

rjee0c15

rjee0c153#

添加最后一个包含此内容的上下文并使用它。
例子:

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

        final ScrollView scroll=new ScrollView(this);
        final Context ctx = this;

...

        //but here says 'this' cannot be applied ###############
        ImageView imView = new ImageView(ctx);
7vhp5slm

7vhp5slm4#

这个关键字在父类中工作得很好,但是在创建子类时,必须使用 ActivityName.this 而不是 this .

相关问题