【问题标题】:ActivityNotFoundException In Google PlusGoogle Plus 中的 ActivityNotFoundException
【发布时间】:2013-12-22 00:07:25
【问题描述】:

我的应用程序想要在 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

【问题讨论】:

  • 你在要使用的activity中设置了Action "android.intent.action.SEND"吗?
  • 你把它放在了下的manifestfile中了吗?
  • 是的,我也试过这个。
  • 您是否在设备或模拟器上安装了 Google+ 应用?
  • 我也将建议与@ZubairAhmadKhan 相同。确保您的设备上确实有正确的 Google 活动。

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


【解决方案1】:

Google+ 没有可供一般开发人员使用的独立写入 API - 通过应用发布到 Google+ 的唯一方法是打开 Google+ 应用(最好使用PlusShare.Builder 返回的 Intent)所以如果 Google+ 不存在然后它会给出 ActivityNotFoundException。

【讨论】:

    【解决方案2】:

    尝试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);
    

    【讨论】:

    • 谢谢,但我的要求是,如果没有 google+,我的应用程序也可以共享图像文件。
    • 我可以理解您想要做什么,但事实是,大多数情况下,当您想从您的应用程序中将某些内容分享到社交媒体(如 Facebook、Twitter 等)时,您必须先登录并然后进行共享并为此目的,我认为您应该在您的设备或模拟器中安装应用程序,以便您可以执行登录并共享您想要的任何内容。
    • 请检查stackoverflow.com/questions/20471741/…此代码正在尝试上传图像文件,但出现异常,您可以帮忙吗?
    • 好吧,让我试试,我会评论。
    • 转到此developers.google.com/+/mobile/android/getting-started 链接并详细了解“先决条件”,然后您就会从一开始就明白我在说什么......
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      是的,您可以使用 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 共享的一个工作示例。

      【讨论】:

        【解决方案5】:

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

                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 帐户是否可用。

        【讨论】:

        • 即使您获得了 SUCCESS,如果未安装 Google+,您仍然会获得 ActivityNotFound 异常。
        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-12-13
            • 1970-01-01
            • 1970-01-01
            • 2013-01-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多