0

我已经在类中初始化对象,如下所示

View popupView; //This is global

我在 onCreate() 方法中创建了 View 对象,如下所示

LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = layoutInflater.inflate(R.layout.activity_filter_form, null);

我想访问 setOnClickListener 中的视图对象,如下所示

popupView.setVisibility(View.VISIBLE);

但上面的行给出了一个错误“popupView 无法解析为一个类型”。请帮忙。

完整代码如下

public class Myclass1 extends Activity 
{
TextView tv_addiction_name, tv_today, tv_yesterday, tv_this_week, tv_this_month, tv_total;
ImageView iv_back, iv_filter;
int year, month, day;
int placeData;
Boolean state = false;
DatePickerDialog.OnDateSetListener dateListener;
SQLiteDatabase database;
int cat_id = 0;
View popupView;

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

    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    popupView = layoutInflater.inflate(R.layout.activity_filter_addiction_form, null);
    popupView.setVisibility(View.INVISIBLE);

    Intent intent = getIntent();
    final String cat_name = intent.getStringExtra("cat_name");
    tv_addiction_name = (TextView) findViewById(R.id.textViewAddictionDetails);
    tv_addiction_name.setText(cat_name);

    tv_total = (TextView) findViewById(R.id.textview_total);
    tv_today = (TextView) findViewById(R.id.textview_today);
    tv_yesterday = (TextView) findViewById(R.id.textview_yesterday);
    tv_this_week = (TextView) findViewById(R.id.textview_this_week);
    tv_this_month = (TextView) findViewById(R.id.textview_this_month);

    iv_back = (ImageView) findViewById(R.id.imageViewAddictionDetailsBack);
    iv_back.setClickable(true);
    iv_back.setOnClickListener(new OnClickListener()
                                    {
                                        @Override
                                        public void onClick(View v)
                                        {
                                            Intent intent = new Intent(AddictionDetails.this, StartActivity.class);
                                            startActivity(intent);
                                        }
                                    }
                                );

    iv_filter = (ImageView) findViewById(R.id.imageViewAddictionDetailsFilter);
    iv_filter.setClickable(true);
    iv_filter.setOnClickListener(new OnClickListener()
                                    {
                                        @Override
                                        public void onClick(View v)
                                        {
                                            if(state == false)
                                            {
                                                state = true;
                                                popupView.setVisibility(View.VISIBLE); // Getting error on this line.
4

3 回答 3

1

我认为成员 popupView 在匿名类 OnClickListener 中不可见。您应该尝试通过 MyClass1.popupView 访问它。

于 2013-02-11T08:35:33.403 回答
0

试试这个我希望这对你有帮助..

 LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
 popupView = layoutInflater.inflate(R.layout.activity_filter_form, R.id.layout_id,true);
于 2013-02-11T08:24:20.707 回答
0

在里面定义popupView 变量onCreate()并制作它final。然后,您将能够在onClick()方法中访问它。

于 2013-02-11T08:29:57.017 回答