Android 通过对话框按钮进入市场的意图和StartActivity
我真的无法集中注意力在活动、意图和开始上。。即使阅读了Lars Vogels教程() 我已经尽力使这个问题尽可能简洁明了 2个类(KKOTestActivity,VersionChangeInfo)和一个AndroidManifest 目标:类KKOTestActivity启动,激发VersionChangeInfo。该类显示了一个包含三个按钮的对话框。其中之一就是上市。这就是问题所在。当用户按下该按钮时,我得到NPE错误(请参阅下面的错误日志)。我真的不明白我在这里做什么,所以如果能链接到假人的意图或其他东西,我将不胜感激:)。谢谢 持续性:Android 通过对话框按钮进入市场的意图和StartActivity,android,android-activity,android-intent,nullpointerexception,Android,Android Activity,Android Intent,Nullpointerexception,我真的无法集中注意力在活动、意图和开始上。。即使阅读了Lars Vogels教程() 我已经尽力使这个问题尽可能简洁明了 2个类(KKOTestActivity,VersionChangeInfo)和一个AndroidManifest 目标:类KKOTestActivity启动,激发VersionChangeInfo。该类显示了一个包含三个按钮的对话框。其中之一就是上市。这就是问题所在。当用户按下该按钮时,我得到NPE错误(请参阅下面的错误日志)。我真的不明白我在这里做什么,所以如果能链接到假人
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>
首先,在您的AndroidManifest.xml中,
中缺少一个
但当这项工作完成时,无论如何都会崩溃,因为模拟器上没有安装市场。不过,它在设备上应该可以正常工作。首先,您在AndroidManifest.xml的
中缺少一个
但当这项工作完成时,无论如何都会崩溃,因为模拟器上没有安装市场。不过,它在设备上应该可以正常工作。你没有做错;您获得
ActivityNotFoundException
的原因是,您的设备上没有安装能够处理Intent.ACTION\u视图的应用程序。原因是您选择查看市场://
-URI,而不是http://
。您可能正在使用emulator,它不包含市场
您有三种选择:
在模拟器上安装Market(不知道如何安装)
保持原样;它将在安装了市场的设备上工作。将对startActivity()
的调用包装在中,尝试{..}catch(ActivityNotFoundException e){..}
将URI更改为http://
或https://
第三个选项将在模拟器中工作,即使没有安装市场。在您的设备上,它将提示您是要在市场上还是在浏览器中打开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);
你没有做错;您获得ActivityNotFoundException
的原因是,您的设备上没有安装能够处理Intent.ACTION\u视图的应用程序。原因是您选择查看市场://
-URI,而不是http://
。您可能正在使用emulator,它不包含市场
您有三种选择:
在模拟器上安装Market(不知道如何安装)
保持原样;它将在安装了市场的设备上工作。将对startActivity()
的调用包装在中,尝试{..}catch(ActivityNotFoundException e){..}
将URI更改为http://
或https://
第三个选项将在模拟器中工作,即使没有安装市场。在您的设备上,它将提示您是要在市场上还是在浏览器中打开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);
试一试
并检查您的包名。在您的类中,您在manifesthappyworx.nl.KKOTest中使用了happyworx.nl.Flashwords,如果您构建了到应用程序的链接,则此包名称必须相等。您还可以使用getPackageName()方法而不是“your.package.name”试试看
并检查您的包名。在您的类中,您在manifesthappyworx.nl.KKOTest中使用了happyworx.nl.Flashwords,如果您构建了到应用程序的链接,则此包名称必须相等。您还可以使用getPackageName()方法而不是“your.package.name”YES!!感谢您对ActivityNotFound错误的清晰解释。我现在明白了+1:)是的!!感谢您对ActivityNotFound错误的清晰解释。我现在明白了+1:)是的!现在可以了。据我所知,这是我问题的完整答案。非常感谢你。如果你有时间,我想让你看看我的另一个意向问题:谢谢!不完全是,;通过将
-标记添加到清单中,您声明您的应用程序可以处理意图。查看-情况并非如此,除非您希望您的应用程序本身处理市场://
-链接(而不是指市场)。在这种情况下,不需要添加。是!现在可以了。据我所知,这是我问题的完整答案。非常感谢你。如果你有时间,我想让你看看我的另一个意向问题:谢谢!不完全是,;通过将
-标记添加到清单中,您声明您的应用程序可以处理意图。查看-情况并非如此,除非您希望您的应用程序本身处理市场://
-链接(而不是指市场)。在这种情况下,不需要添加。
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=happyworx.nl.KKOTest"));
startActivity(intent);