1

如果 zxing 条码扫描器不可用,我希望提示我的应用用户安装它。这是我用来加载对话框并要求用户下载应用程序的代码,但它当前返回与“未找到活动”相关的错误:

显现:

<activity
    android:name=".FrontActivity"
    android:label="@string/title_activity_front"
    android:screenOrientation = "portrait" 
    android:theme="@style/NoTitleBar" >

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <intent-filter> 
        <action android:name="android.intent.action.VIEW" />  
        <category android:name="android.intent.category.DEFAULT" />  
    </intent-filter> 

</activity>

爪哇:

public class FrontActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.front_activity);


        isScannerInstalled();
    }

    private void isScannerInstalled()
    {
        try
        {
            ApplicationInfo info = 
                getPackageManager().
                getApplicationInfo("com.google.zxing.client.android",0);
        } 

        catch (PackageManager.NameNotFoundException e)
        {
            AlertDialog.Builder alertDialogBuilder = 
                new AlertDialog.Builder(FrontActivity.this);

            alertDialogBuilder.setTitle("Warning");
            alertDialogBuilder.setMessage("Please install the Barcode Scanner.");
            alertDialogBuilder.setCancelable(false);

            alertDialogBuilder.setNeutralButton
            (
                "OK", 
                new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int whichButton) 
                    {
                          Uri uri = Uri.parse(
                          "market://search?q=pname:com.google.zxing.client.android"
                          );

                          startActivity(new Intent(Intent.ACTION_VIEW, uri));
                    }
                }
            );

            alertDialogBuilder.show();
        }
    }
}

错误:

11-16 08:37:37.716: E/AndroidRuntime(12694): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://search?q=pname:com.google.zxing.client.android }
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.app.Activity.startActivityForResult(Activity.java:2817)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.app.Activity.startActivity(Activity.java:2923)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at test.app01.FrontActivity$1.onClick(FrontActivity.java:140)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.os.Looper.loop(Looper.java:123)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at android.app.ActivityThread.main(ActivityThread.java:4627)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at java.lang.reflect.Method.invokeNative(Native Method)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at java.lang.reflect.Method.invoke(Method.java:521)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-16 08:37:37.716: E/AndroidRuntime(12694):    at dalvik.system.NativeStart.main(Native Method)

谁能看到问题可能是什么?

4

1 回答 1

0

试试这个

public void onClick(DialogInterface dialog, int whichButton) 
                    {
                          try 
                          {
                               startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+packageName)));
                              }
                         catch (android.content.ActivityNotFoundException err) 
                         {
                             startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id="+packageName)));
                             }
                    }

并在设备上进行测试...因为模拟器不会有市场应用程序..

于 2012-11-16T09:31:50.533 回答