Search code examples
javaandroidonclicklistenersharing

Why is this Google + basic sharing not working?


import android.content.Intent;
import android.net.Uri;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button shareButton = (Button) findViewById(R.id.share_button);
    shareButton.setOnClickListener( 

        new OnClickListener() {

            public void onClick (View v) {
                // Launch the Google+ share dialog with attribution to your app.
                Intent shareIntent = new PlusShare.Builder(this)
                    .setType("text/plain")
                    .setText("Welcome to the Google+ platform.")
                    .setContentUrl(Uri.parse("https://developers.google.com/+/"))
                    .getIntent();

                startActivityForResult(shareIntent, 0);
            }
        }
    );
}

I'm getting an Error on the "OnClickListener" and the "(View V)", also I am near a complete beginnner to Java and Android trying to add some basic Google plus sharing so please help me out


Solution

  • You can't call methods inside the class block, you have to do it in a method.

    public class MainActivity extends AppCompatActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState){
            Button shareButton = (Button) findViewById(R.id.share_button);
            shareButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick (View v){
                    //your code
                }
            });
        }
    }