MRAK
MRAK

Reputation: 25

event can't run in android ( get errors)

I recently use android studio instead of eclipse. but somthing is wrong! these are main java code that run currectly in eclipse but now get errors!

    package com.example.amir.myapplicationeeee;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.Button;
    import android.view.View;
    import android.view.View.OnClickListener;

    public class MainActivity extends Activity  {


        Button button=(Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                startActivity(new Intent(getApplicationContext(),Main2Activity.class));
                finish();
            }
        });

setOnClickListener is hilighed red and error ";" excepted showed when want to runnin app in virtual device

Upvotes: 0

Views: 38

Answers (1)

K.Os
K.Os

Reputation: 5496

If the code you provide here is exactly the same as you have in your project, then you should just add '}' on the end of your code - it closes the MainActivity class.

You should also have evertyhing implemented inside onCreate() method.

So it should look like:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button=(Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            startActivity(new 
            Intent(this,Main2Activity.class));
            finish();
        }
    });
  }
}

Upvotes: 1

Related Questions