【问题标题】:Android No Activity found to handle intentAndroid 未找到处理意图的 Activity
【发布时间】:2014-08-14 04:09:42
【问题描述】:

我的应用基于在 Foursquare oAuth sample 发布的foursquare-oAuth-sample 应用

已经对 MyActivity 进行了与示例代码非常相似的更改,但仍然得到这个,有人可以指出我需要更改什么,代码如下

public class MyActivity extends FragmentActivity {


private static final int REQUEST_CODE_FSQ_CONNECT = 200;
private static final int REQUEST_CODE_FSQ_TOKEN_EXCHANGE = 201;

/**
 * Obtain your client id and secret from:
 * https://foursquare.com/developers/apps
 */
private static final String CLIENT_ID = "";
private static final String CLIENT_SECRET = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    ensureUi();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * Update the UI. If we already fetched a token, we'll just show a success
 * message.
 */
private void ensureUi() {
    boolean isAuthorized = !TextUtils.isEmpty(ExampleTokenStore.get().getToken());

    TextView tvMessage = (TextView) findViewById(R.id.tvMessage);
    tvMessage.setVisibility(isAuthorized ? View.VISIBLE : View.GONE);

    Button btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLogin.setVisibility(isAuthorized ? View.GONE : View.VISIBLE);
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Start the native auth flow.
            Intent intent = FoursquareOAuth.getConnectIntent(MyActivity.this, CLIENT_ID);

            // If the device does not have the Foursquare app installed, we'd
            // get an intent back that would open the Play Store for download.
            // Otherwise we start the auth flow.
            if (FoursquareOAuth.isPlayStoreIntent(intent)) {
                toastMessage(MyActivity.this, getString(R.string.app_not_installed_message));
                startActivity(intent);
            } else {
                startActivityForResult(intent, REQUEST_CODE_FSQ_CONNECT);
            }
        }


    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CODE_FSQ_CONNECT:
            onCompleteConnect(resultCode, data);
            break;

        case REQUEST_CODE_FSQ_TOKEN_EXCHANGE:
            onCompleteTokenExchange(resultCode, data);
            break;

        default:
            super.onActivityResult(requestCode, resultCode, data);
    }

}

private void onCompleteConnect(int resultCode, Intent data) {
    AuthCodeResponse codeResponse = FoursquareOAuth.getAuthCodeFromResult(resultCode, data);
    Exception exception = codeResponse.getException();

    if (exception == null) {
        // Success.
        String code = codeResponse.getCode();
        performTokenExchange(code);

    } else {
        if (exception instanceof FoursquareCancelException) {
            // Cancel.
            toastMessage(this, "Canceled");

        } else if (exception instanceof FoursquareDenyException) {
            // Deny.
            toastMessage(this, "Denied");

        } else if (exception instanceof FoursquareOAuthException) {
            // OAuth error.
            String errorMessage = exception.getMessage();
            String errorCode = ((FoursquareOAuthException) exception).getErrorCode();
            toastMessage(this, errorMessage + " [" + errorCode + "]");

        } else if (exception instanceof FoursquareUnsupportedVersionException) {
            // Unsupported Fourquare app version on the device.
            toastError(this, exception);

        } else if (exception instanceof FoursquareInvalidRequestException) {
            // Invalid request.
            toastError(this, exception);

        } else {
            // Error.
            toastError(this, exception);
        }
    }
}

private void onCompleteTokenExchange(int resultCode, Intent data) {
    AccessTokenResponse tokenResponse = FoursquareOAuth.getTokenFromResult(resultCode, data);
    Exception exception = tokenResponse.getException();

    if (exception == null) {
        String accessToken = tokenResponse.getAccessToken();
        // Success.
        toastMessage(this, "Access token: " + accessToken);

        // Persist the token for later use. In this example, we save
        // it to shared prefs.
        ExampleTokenStore.get().setToken(accessToken);

        // Refresh UI.
        ensureUi();

    } else {
        if (exception instanceof FoursquareOAuthException) {
            // OAuth error.
            String errorMessage = ((FoursquareOAuthException) exception).getMessage();
            String errorCode = ((FoursquareOAuthException) exception).getErrorCode();
            toastMessage(this, errorMessage + " [" + errorCode + "]");

        } else {
            // Other exception type.
            toastError(this, exception);
        }
    }
}

/**
 * Exchange a code for an OAuth Token. Note that we do not recommend you
 * do this in your app, rather do the exchange on your server. Added here
 * for demo purposes.
 *
 * @param code
 *          The auth code returned from the native auth flow.
 */
private void performTokenExchange(String code) {
    Intent intent = FoursquareOAuth.getTokenExchangeIntent(this, CLIENT_ID, CLIENT_SECRET, code);
    startActivityForResult(intent, REQUEST_CODE_FSQ_TOKEN_EXCHANGE);
}

public static void toastMessage(Context context, String message) {
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}

public static void toastError(Context context, Throwable t) {
    Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
}

错误日志

这是我得到的异常,有人可以指出为什么它无法找到处理意图的活动吗?谢谢

08-13 23:15:23.137    2754-2754/com.example.panaceatechnologysolutions.farhansfoursquareapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.panaceatechnologysolutions.farhansfoursquareapp, PID: 2754
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://details?id=com.example.panaceatechnologysolutions.farhansfoursquareapp&referrer=utm_source=foursquare-android-oauth&utm_term=CLIENT_ID }
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1691)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1482)
        at android.app.Activity.startActivityForResult(Activity.java:3711)
        at android.app.Activity.startActivityForResult(Activity.java:3669)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:840)
        at android.app.Activity.startActivity(Activity.java:3914)
        at android.app.Activity.startActivity(Activity.java:3882)
        at com.example.panaceatechnologysolutions.farhansfoursquareapp.MyActivity$1.onClick(MyActivity.java:90)
        at android.view.View.performClick(View.java:4598)
        at android.view.View$PerformClick.run(View.java:19268)
        at android.os.Handler.handleCallback(Handler.java:738)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5070)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)

08-13 23:15:30.157 2754-2765/com.example.panaceatechnologysolutions.farhansfoursquareapp I/art: 堆转换到 ProcessStateJankImperceptible 耗时 7.253732ms 至少节省了 72KB

好的,所以根据我检查的 Rohans 回复,因为我是在模拟器上执行此操作的,所以我在项目中拥有的 Foursquare oAuth 库中的这个 sn-p 无法根据上下文和客户端 ID 创建意图。我不确定为什么它返回 null 并因此将我重定向到 Google Play 商店以在我的模拟器上安装foursquare。我已经用foursquare注册了我的应用程序,并且正在使用注册的客户端ID,这个函数使用的其余参数是Foursquare oAuth Java类中的参数。如果有人使用过这个库或者可以指出为什么它找不到意图,请告诉我,因为我已经坚持了几天。

这是 Rohan 指出的在 MyActivity 类中调用 Foursquare oAuth Java 类的代码行

    Intent intent = FoursquareOAuth.getConnectIntent(MyActivity.this, CLIENT_ID);

这是 Foursquare oAuth Java 类中的 getConnectIntent 方法

    public static Intent getConnectIntent(Context context, String clientId) {
    Uri.Builder builder = new Uri.Builder();
    builder.scheme(URI_SCHEME);
    builder.authority(URI_AUTHORITY);
    builder.appendQueryParameter(PARAM_CLIENT_ID, clientId);
    builder.appendQueryParameter(PARAM_VERSION, String.valueOf(LIB_VERSION));
    builder.appendQueryParameter(PARAM_SIGNATURE, getSignatureFingerprint(context));

    Intent intent = new Intent(Intent.ACTION_VIEW, builder.build());
    if (isIntentAvailable(context, intent)) {
        return intent;
    }

    return getPlayStoreIntent(clientId);
}

【问题讨论】:

  • 我认为问题出在 Intent intent = FoursquareOAuth.getConnectIntent(MyActivity.this, CLIENT_ID);这条线你没有从“getConnectIntent”获得意图
  • 嗨,Rohan,你是对的,我发布了 Foursquare 库中的代码,该代码没有返回意图,而是将我重定向到 google play 商店。你知道它为什么不能授权吗?

标签: android android-activity android-studio foursquare


【解决方案1】:

它会将您重定向到播放商店,因为“isIntentAvailable 为假”并调用“getPlayStoreIntent”将您重定向到播放商店。 在 isIntentAvailable 方法中

private static boolean isIntentAvailable(Context context, Intent intent) {
     PackageManager packageManager = context.getPackageManager();
     List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(
    intent, PackageManager.MATCH_DEFAULT_ONLY);
    return resolveInfo.size() > 0;
 }

如果找到合适的包,此方法返回 true。 还要检查您的客户 ID 是否为空且正确

【讨论】:

    【解决方案2】:

    是的,Rohan...你是对的,这是错误的,因为意图没有从 isIntentAvailable 返回任何内容,但没有返回意图的真正原因是因为我使用的是模拟器,包管理器显然正在寻找对于安装的foursquare.apk 包,它没有找到。我没有在任何地方表明 Foursquare 必须安装他们的 apk,该 apk 不包含在他们在上面示例项目的链接中提供的 oAuth 库中。我猜他们假设您使用的是 Android 设备进行测试,而不是模拟器。这些是在 Android Studio 的 Android 模拟器上使用 Foursquare 的 oAuth 的步骤,或者我猜测的 Eclipse。

    1) 下载 Foursquare APK http://www.apk4fun.com/apk/6395/

    2) 作为先决条件,在 Android Studio 中打开 Android SDK Manager 并确保已下载并安装 Google API,这些是 Foursquare 需要的

    3) 复制 /Applications/sdk/platform-tools 文件夹下的foursquare.apk文件

    4) 使用 adb 工具在此链接中显示的文件夹中安装 apk How to install an apk on the emulator in Android Studio?

    5) 现在您的应用程序可以使用模拟器联系foursquare,您将不会被重定向到模拟器上安装应用程序!

    -注意,当我第二天关闭 Android Studio 和模拟器时,我注意到我必须重新安装“foursquare.apk”。但是很容易,因为我知道该怎么做,希望这可以避免其他人的挫败感,因为我花了几天时间才弄清楚这一点:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      相关资源
      最近更新 更多