Intent和StartActivity从对话框按钮进入市场

时间:2012-04-19 07:59:01

标签: android android-activity android-intent nullpointerexception

即使阅读了Lars Vogels Tutorial(Tutorial on Intents

,我也无法理解活动,意图和开始......

我试图让问题尽可能简洁明了。

2个类(KKOTestActivity,VersionChangeInfo)和一个AndroidManifest。

目标:类KKOTestActivity启动,触发VersionChangeInfo。该类显示带有三个按钮的对话框。其中之一就是进入市场。这就是问题所在。当用户按下该按钮时,我得到NPE错误(参见下面的错误日志)。我真的不明白我在这里做什么,所以一个链接到Intents-for-dummies或者其他东西也会受到高度赞赏:)。谢谢!

KKOTestActivity:

package happyworx.nl.KKOTest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;


public class KKOTestActivity extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new VersionChangeInfo(this).show();
    }
    public void onClick(View v) {
        // TODO Auto-generated method stub  
    }
}

VersionChangeInfo.java:

package happyworx.nl.KKOTest;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.preference.PreferenceManager;


public class VersionChangeInfo extends Activity {

    private String VCI_PREFIX = "vci_";
    private Activity mActivity;

    public VersionChangeInfo(final Activity context) {
        mActivity = context;
    }

    private PackageInfo getPackageInfo() {
        PackageInfo pi = null;
        try {
             pi = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return pi;
    }

     public void show() {
        PackageInfo versionInfo = getPackageInfo();

        // the eulaKey changes every time you increment the version number in the AndroidManifest.xml
        final String eulaKey = VCI_PREFIX + versionInfo.versionCode;
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
        boolean hasBeenShown = prefs.getBoolean(eulaKey, false);
        if(hasBeenShown == false){

            // Show the Eula
            String title = mActivity.getString(R.string.app_name) + " v" + versionInfo.versionName;

            //Includes the updates as well so users know what changed.
            String message = mActivity.getString(R.string.updates) + "\n\n" + mActivity.getString(R.string.eula);

            AlertDialog.Builder builder = new AlertDialog.Builder(mActivity)
                    .setTitle(title)
                    .setMessage(message)
                    .setPositiveButton("Ga naar Market", new Dialog.OnClickListener() {

                        public void onClick(DialogInterface dialogInterface, int i) {                           
                            // Mark this version as read.
                            SharedPreferences.Editor editor = prefs.edit();
                         //   editor.putBoolean(eulaKey, true);
                         //   editor.commit();
                            dialogInterface.dismiss();

                            final Intent MyIntent = new Intent(Intent.ACTION_VIEW, 
                            Uri.parse("market://details?id=happyworx.nl.Flashwords"));
                            startActivity(MyIntent);
                        }
                    })
                    .setNegativeButton("Later", new Dialog.OnClickListener() {

                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }



                    })

                    .setNeutralButton("Niet meer tonen", new Dialog.OnClickListener(){
                        public void onClick(DialogInterface dialogInterface, int i) {
                            SharedPreferences.Editor editor = prefs.edit();
                            editor.putBoolean(eulaKey, true);
                            editor.commit();
                            dialogInterface.dismiss();
                        }
                    })
                    ;
            builder.create().show();
        }
    }

}

AndroidManifest.xls

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="happyworx.nl.KKOTest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".KKOTestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

                <activity
            android:name=".VersionChangeInfo"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

错误日志:

04-19 08:06:45.711: E/AndroidRuntime(15186): FATAL EXCEPTION: main
04-19 08:06:45.711: E/AndroidRuntime(15186): java.lang.NullPointerException
04-19 08:06:45.711: E/AndroidRuntime(15186):    at android.app.Activity.startActivityForResult(Activity.java:2827)
04-19 08:06:45.711: E/AndroidRuntime(15186):    at android.app.Activity.startActivity(Activity.java:2933)
04-19 08:06:45.711: E/AndroidRuntime(15186):    at happyworx.nl.KKOTest.VersionChangeInfo$1.onClick(VersionChangeInfo.java:63)
04-19 08:06:45.711: E/AndroidRuntime(15186):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
04-19 08:06:45.711: E/AndroidRuntime(15186):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 08:06:45.711: E/AndroidRuntime(15186):    at android.os.Looper.loop(Looper.java:123)
04-19 08:06:45.711: E/AndroidRuntime(15186):    at android.app.ActivityThread.main(ActivityThread.java:3683)
04-19 08:06:45.711: E/AndroidRuntime(15186):    at java.lang.reflect.Method.invokeNative(Native Method)
04-19 08:06:45.711: E/AndroidRuntime(15186):    at java.lang.reflect.Method.invoke(Method.java:507)
04-19 08:06:45.711: E/AndroidRuntime(15186):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-19 08:06:45.711: E/AndroidRuntime(15186):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-19 08:06:45.711: E/AndroidRuntime(15186):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:2)

你没有做错;您获得ActivityNotFoundException的原因是因为您的设备上没有安装可以处理Intent.ACTION_VIEW的应用程序。这样做的原因是您选择查看market:// - URI而不是http(s)://。您可能正在使用不包含市场的模拟器。

您有三种选择:

  1. 在您的模拟器上安装市场(不知道如何)。
  2. 保持原样;它适用于安装了Market的设备。在startActivity()
  3. 中打电话给try { .. } catch (ActivityNotFoundException e) { ... }
  4. 将您的URI更改为http://https://
  5. 即使没有安装Market,第三个选项也可以在模拟器中使用。在您的设备上,它会提示您是要在市场中还是在浏览器中打开URI。

    我会试着解释一下:

    // First, you're basically telling Android you want some Activity
    // that is not your current Activity to handle an action:
    // you wan't `some application` to view `something`.
    Intent i = new Intent(Intent.ACTION_VIEW);
    
    // Next, you're making it clear you want an application to view a specific URI.
    // In this case, you're asking for something that can handle a "market://"-URI.
    i.setData(Uri.parse("market://details?id=happyworx.nl.Flashwords"));
    
    // Finally, you're asking Android to actually broadcast the Intent.
    startActivity(i);
    

答案 1 :(得分:1)

首先,您在<action android:name="android.intent.action.VIEW" />内的AndroidManifest.xml中遗漏了<intent-filter>

但是当这个完成时,它会崩溃,因为市场没有安装在模拟器上。它应该可以在设备上正常工作。

答案 2 :(得分:0)

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=happyworx.nl.KKOTest"));
startActivity(intent);

并检查您的包裹名称。在您的班级中,您使用清单 happyworx.nl.KKOTest 中的 happyworx.nl.Flashwords ,如果您构建了应用程序的链接,则此包名称必须等于。您也可以使用getPackageName()方法代替&#34; your.package.name&#34;