Note that there are some explanatory texts on larger screens.

plurals
  1. POIntent and StartActivity to go to Market from a Dialog button
    primarykey
    data
    text
    <p>I really can't get my head around Activity, Intent and start.. even after reading Lars Vogels Tutorial (<a href="http://www.vogella.com/articles/AndroidIntent/article.html" rel="nofollow">Tutorial on Intents</a>)</p> <p>I've tried to make the question as clean and simple as possible.</p> <p>2 classes (KKOTestActivity, VersionChangeInfo) and one AndroidManifest.</p> <p>Goal : Class KKOTestActivity starts, fires off VersionChangeInfo. That class shows a dialog with three buttons. One of them is the go-to-market. That's where the problem is. When the user presses that button, I get NPE Error (see error log below). I really don't understand what I'm doing here, so a link to Intents-for-dummies or something would also be highly appreciated :). Thanks!</p> <p>KKOTestActivity :</p> <pre><code>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 } } </code></pre> <p>VersionChangeInfo.java :</p> <pre><code>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(); } } } </code></pre> <p>AndroidManifest.xls</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="happyworx.nl.KKOTest" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="4" /&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" &gt; &lt;activity android:name=".KKOTestActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".VersionChangeInfo" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>Error Log :</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload