user3293120
user3293120

Reputation: 63

Open an activity by click on a imageView

I'm trying to make an ImageView open another activity:

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);
            }
        }); 
    }
}

Where i'm doing wrong? It say "Unreachable code"

Upvotes: 2

Views: 1319

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

move return true as last statement of 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;
}

Upvotes: 2

Related Questions