当我将项目用作public class HomeActivity extends Activity implements OnClickListener并在其中使用getContentResolver时,它将显示错误

The method getContentResolver() is undefined for the type new View.OnClickListener(){}

但是当我使用public class HomeActivity extends ActionBarActivity时,它对我有用,但是我如何在public class HomeActivity extends Activity implements OnClickListener中处理它

代码:

public class HomeActivity extends Activity implements OnClickListener  {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        btnEdit = (Button) findViewById(R.id.btn_edit);
        btnEdit.setOnClickListener(this);

        btnGallery = (Button) findViewById(R.id.btn_gallery);
        btnGallery.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
        case R.id.btn_edit:
            intent = new Intent();
            intent.setType("image/*");

            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent, REQUEST_IMAGE);
    //      startActivityForResult(
    //              Intent.createChooser(intent, "Select Photo"),
    //              MyConstants.TAKE_PHOTO);
            break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode != Activity.RESULT_OK) {return;}

            if (requestCode == REQUEST_IMAGE) {
                final Uri uri = data.getData();
                    Button button= (Button) findViewById(R.id.btn_camera);
                    button.setOnClickListener(new OnClickListener() {
                          @Override
                          public void onClick(View v)  {
                              try
                                {
                                InputStream is = this.getContentResolver().openInputStream(uri);
                                final Bitmap bmInImg = BitmapFactory.decodeStream(is);

                                }
                                catch (IOException e) {
                                    e.printStackTrace();
                                }

                          }
                        });

                }
            }

最佳答案

推理:




  因为getContentResolver()是上下文类的方法,而不是View.OnClickListener Interface的方法。


当你写这行

InputStream is = this.getContentResolver().openInputStream(uri);


您正在使用this从View.OnClickListener接口(该接口不存在)中调用该方法,这就是Android给您错误的原因。

See Context class HereView.OnClickListener Here



回答:



声明一个全球性的Context变量。

       // Global Var
       Context context;

       // In onCreate
       context = this;


将此行更改为

       InputStream is = this.getContentResolver().openInputStream(uri);


这个:

       InputStream is = context.getContentResolver().openInputStream(uri);

关于java - 未定义类型为new View.OnClickListener(){},我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26204196/

10-13 04:18