为什么Google +基本分享无效?

时间:2016-04-01 16:25:48

标签: java android onclicklistener sharing

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

我在" OnClickListener"上收到错误和#34;(View V)",我也接近Java和Android的初学者,试图添加一些基本的Google plus分享,所以请帮帮我

1 个答案:

答案 0 :(得分:0)

你不能在类块中调用方法,你必须在方法中执行它。

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