Eclipse中Activity代码中的错误

时间:2014-03-23 08:51:31

标签: android eclipse android-activity

我是编程新手,我无法理解为什么每个public void onClick(View v) { " and warning on "(new OnClickListener);

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

    //mana lapu parslegshnas
    Button matematikaslapa = (Button) findViewById(R.id.poga1);
    matematikaslapa.setOnClickListener(new OnClickListener);

    @Override
    public void onClick(View v) {
        Intent myIntent = new Intent(v.getContext(), matematikaslapa.class);
        v.getContext().startActivity(myIntent);
    }

    Button fizikaslapa = (Button) findViewById(R.id.poga2);
    fizikaslapa.setOnClickListener(new OnClickListener);

    @Override
    public void onClick(View v) {
        Intent myIntent = new Intent(v.getContext(), fizikaslapa.class);
        v.getContext().startActivity(myIntent);
    }

    Button kimijaslapa = (Button) findViewById(R.id.poga3);
    kimijaslapa.setOnClickListener(new OnClickListener);

    @Override
    public void onClick(View v) {
        Intent myIntent = new Intent(v.getContext(), kimijaslapa.class);
        v.getContext().startActivity(myIntent);
    }
    //beigas

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

2 个答案:

答案 0 :(得分:0)

你不能在方法中声明方法。

像这样改变结构

fizikaslapa.setOnClickListener(new OnClickListener);

@Override
public void onClick(View v) {
    Intent myIntent = new Intent(v.getContext(), fizikaslapa.class);
    v.getContext().startActivity(myIntent);
}

这样的事情

fizikaslapa.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent myIntent = new Intent(v.getContext(), fizikaslapa.class);
        v.getContext().startActivity(myIntent);
    }
});

现在,您正在创建一个OnClickListener匿名内部类实例,并在该类中声明onClick()方法。

答案 1 :(得分:0)

更改此

 matematikaslapa.setOnClickListener(new OnClickListener);
 @Override
 public void onClick(View v) {
    Intent myIntent = new Intent(v.getContext(), matematikaslapa.class);
    v.getContext().startActivity(myIntent);
 }

matematikaslapa.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v) {
    Intent myIntent = new Intent(v.getContext(), matematikaslapa.class);
    v.getContext().startActivity(myIntent);
}
});

与其他OnClickListener's类似。您正在使用匿名内部类,并且您正在实现接口OnClickListener

让您的Activity类实现OnClickListener

 matematikaslapa.setOnClickListener(this);
 fizikaslapa.setOnClickListener(this);
 ..// similarly for others 

然后

public void onClick(View v)
{
    switch(v.getId())
    {
      case R.id.poga1 :
                Intent myIntent = new Intent(v.getContext(), matematikaslapa.class);
                v.getContext().startActivity(myIntent);
      break; 

      case R.id.poga2 :
                Intent myIntent = new Intent(v.getContext(), fizikaslapa.class);
                v.getContext().startActivity(myIntent);
      break; 
    }
}