单击imageView打开活动

时间:2014-02-23 22:18:31

标签: java android

我正在尝试让ImageView打开另一项活动:

public class MainActivity extends Activity {

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

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

        ImageView Image1 = (ImageView) findViewById(R.id.Image1);
        Image1.setClickable(true);
        Image1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View V) {       
                Intent intent = new Intent(V.getContext(), Activity2.class);
                startActivityForResult(intent, 0);
            }
        }); 
    }
}

我做错了什么?它说“无法访问的代码”

1 个答案:

答案 0 :(得分:1)

移动return true作为onCreateOptionsMenu

的最后一句话
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.gtacheats, menu);


    ImageView Image1 = (ImageView) findViewById(R.id.Image1);
    Image1.setClickable(true);
    Image1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View V) {

        Intent intent = new Intent(V.getContext(), Activity2.class);
        startActivityForResult(intent, 0);
        }
  });
  return true;
}