我在类中初始化对象,如下面的
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.
答案 0 :(得分:1)
我认为成员popupView在匿名类OnClickListener中不可见。您应该尝试通过MyClass1.popupView访问它。
答案 1 :(得分:0)
试试这个我希望这会对你有所帮助..
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = layoutInflater.inflate(R.layout.activity_filter_form, R.id.layout_id,true);
答案 2 :(得分:0)
在popupView
中定义onCreate()
变量并将其设为final
。您可以在onClick()
方法中访问它。