ActivityNotFoundException在Google Plus中

时间:2013-12-05 06:55:45

标签: android google-plus google-play-services

我的应用程序想要在Google Plus上共享图像我已阅读关于Google Plus并且API显示我们可以使用PlusShare.Builder发布媒体文件。我也搜索了stackoverflow但是没有得到任何正确的答案。 我试过这些代码是:

case R.id.Share:
         if (mPlusClient.isConnected()) {
         Intent shareIntent = new PlusShare.Builder(this,mPlusClient)
         .setType("image/*")
         .setText("Risk Score")
         .addStream(Uri.fromFile(new File(Path)))
         .getIntent();
         startActivityForResult(shareIntent, REQ_START_SHARE);
         }

        break;

并且

  case R.id.Share:

Intent shareIntent = ShareCompat.IntentBuilder.from(PlusActivity.this)
   .setText("This site has lots of great information about Android! http://www.android.com")
.setType("image/*").addStream(Uri.fromFile(new File(Path)))
.getIntent().setPackage("com.google.android.apps.plus");             
 startActivityForResult(shareIntent, REQ_START_SHARE);
        break;

 case R.id.Share:

         Intent shareIntent = new PlusShare.Builder(this,mPlusClient)
         .setType("image/*")
         .setText("Risk Score")
         .addStream(Uri.fromFile(new File(Path)))
         .getIntent();
         startActivityForResult(shareIntent, REQ_START_SHARE);

        break;

每次我遇到同样的问题。

12-05 12:19:57.316: E/AndroidRuntime(4688): FATAL EXCEPTION: main
12-05 12:19:57.316: E/AndroidRuntime(4688): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND typ=image/* flg=0x80000 pkg=com.google.android.apps.plus (has extras) }
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1518)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1390)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.app.Activity.startActivityForResult(Activity.java:3204)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at com.winit.android.riskfactor.PlusActivity.onClick(PlusActivity.java:133)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.view.View.performClick(View.java:3517)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.view.View$PerformClick.run(View.java:14155)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.os.Handler.handleCallback(Handler.java:605)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.os.Looper.loop(Looper.java:154)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.app.ActivityThread.main(ActivityThread.java:4624)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at java.lang.reflect.Method.invokeNative(Native Method)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at java.lang.reflect.Method.invoke(Method.java:511)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at dalvik.system.NativeStart.main(Native Method)

我也读过这些问题

sending-json-data-to-google-plus

android-google-plush-intregation-in-my-android-app-gives-android-content-activit

sending-json-data-to-google-plus

failing-to-share-on-google-with-android

no-activity-found-to-handle-intent-causes-fc

7 个答案:

答案 0 :(得分:6)

Google +没有针对一般开发者提供的独立编写API - 通过应用发布到Google+的唯一方法是打开Goog​​le+应用(最好使用PlusShare.Builder返回的意图)所以如果Google plus是不存在然后它会给ActivityNotFoundException。

答案 1 :(得分:3)

尝试this link并按照分步过程进行操作。我跟着他一样,为我工作。另一件事是清理您的项目或重新启动eclipse IDE或在您的设备或模拟器上安装Google+应用程序(apk)。
您还可以查看我的回答here一步一步的步骤在仿真器上安装Google+应用程序。
希望它能解决你的问题......

编辑:
在你的btn_share上试试这个代码

Intent shareIntent = ShareCompat.IntentBuilder.from(ExampleActivity.this)
          .setType("image/*")
          .setText("Welcome to the Google+ platform. https://developers.google.com/+")
          .getIntent()
          .setPackage("com.google.android.apps.plus");//this line is important you should set your package like in this code.

      startActivity(shareIntent);

答案 2 :(得分:2)

Google+要求在设备上安装Google+。添加此代码以捕获异常。

try {
    startActivityForResult(builder.getIntent(), 0);
} catch (ActivityNotFoundException e) {
    Toast.makeText(YourActivity.this, "You haven't installed google+ on your device", Toast.LENGTH_SHORT).show();
}

答案 3 :(得分:1)

是的,您可以使用PlusBuilder在Google Plus上分享图片。添加 Google Play服务库作为项目中的参考。此方法调用 com.google.android.gms.plus.PlusShare $ Builder 类进行共享。如果Google plus不存在,则会提供 ActivityNotFoundException

<强> MainActivity.java

 import android.app.Activity;
 import android.content.ContentResolver;
 import android.content.Intent;
 import android.content.IntentSender.SendIntentException;
 import android.net.Uri;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;

 import com.google.android.gms.common.ConnectionResult;
 import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
 import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
 import com.google.android.gms.plus.PlusClient;
 import com.google.android.gms.plus.PlusOneButton;
 import com.google.android.gms.plus.PlusShare;

 public class MainActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener{




private static final int PLUS_ONE_REQUEST_CODE = 0;
private static final int REQ_SELECT_PHOTO = 1;
private static final int REQ_START_SHARE = 2;
private PlusClient mPlusClient;
Button share;
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPlusClient = new PlusClient.Builder(MainActivity.this, MainActivity.this, MainActivity.this)
    .build();


    share=(Button)findViewById(R.id.share);
    share.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent photoPicker = new Intent(Intent.ACTION_PICK);
            photoPicker.setType("image/*");
            startActivityForResult(photoPicker, REQ_SELECT_PHOTO);
        }
    });
}



@Override
public void onConnectionFailed(ConnectionResult result) {
    // TODO Auto-generated method stub
    if (result.hasResolution()) {
        try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
        } catch (SendIntentException e) {
            mPlusClient.connect();
        }

    }
}


@Override
public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub

}


@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}

@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
        mPlusClient.connect();
        return;
    }
    if(requestCode == REQ_SELECT_PHOTO) {
        if(responseCode == RESULT_OK) {
            Uri selectedImage = intent.getData();
            ContentResolver cr = this.getContentResolver();
            String mime = cr.getType(selectedImage);

            PlusShare.Builder share = new PlusShare.Builder(this);
            share.setText("hello everyone!");
            share.addStream(selectedImage);
            share.setType(mime);
            startActivityForResult(share.getIntent(), REQ_START_SHARE);
        }
    }
}

@Override
protected void onResume() {
    super.onResume();        
    //  mPlusOneStandardButtonWithAnnotation.initialize( URL, PLUS_ONE_REQUEST_CODE);
}

@Override
protected void onStart() {
    super.onStart();
    mPlusClient.connect();
}
@Override
protected void onStop() {
    super.onStop();
    mPlusClient.disconnect();

}
 }

分享是一个按钮,厨房将在点击时打开。

在应用标记

下的 AndroidManifest.xml 中添加以下代码行
   <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

和互联网许可:

<uses-permission android:name="android.permission.INTERNET"/>

这是Google Plus Sharing的一个实例。

答案 4 :(得分:1)

只需检查谷歌服务是否可用

        int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (errorCode != ConnectionResult.SUCCESS) {
            GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
        } else {
            // Do somethings
        }

您需要导入此包:

import com.google.android.gms.common.GooglePlayServicesUtil;

注意:通常情况下,模拟器没有安装Google Play服务,除非您为模拟设备选择了Google API而不是Android API,但在这种情况下,您还必须检查Google帐户是否为可用。

答案 5 :(得分:1)

这与.settype()有某种关系。 我有.setType("text/html")并且遇到了这个错误。但在我将其更改为.setType(“text / plain”)之后,问题就解决了。我不知道为什么,但.settype()是造成这个问题的原因。

答案 6 :(得分:1)

对我来说,我忘记添加setType("image/*") 现在它工作正常