Reputation: 11
I have a problem with my application: When it opens, it crashes due to the integration of license. But I do not know where the problem is. Please can you help me find it? I will be very grateful!
My main acitivity:
package com.example.testouille;
import com.google.android.vending.licensing.AESObfuscator;
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;
import com.google.android.vending.licensing.ServerManagedPolicy;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings.Secure;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
public class MainActivity extends Activity {
static final String BASE64_PUBLIC_KEY = "LICENSE";
private static final byte[] SALT = new byte[] {
-46, 65, 30, -128, -103, -57, 74, -64, 51, 88, -95, -45, 77, -117, -36, -113, -11, 32, -64,
89
};
private Handler mHandler;
private LicenseChecker mChecker;
private LicenseCheckerCallback mLicenseCheckerCallback;
boolean licensed;
boolean checkingLicense;
boolean didCheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void doCheck() {
didCheck = false;
checkingLicense = true;
setProgressBarIndeterminateVisibility(true);
mChecker.checkAccess(mLicenseCheckerCallback);
}
private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
@Override
public void allow(int reason) {
if (isFinishing()) {
return;
}
Log.i("License","Accepted!");
licensed = true;
checkingLicense = false;
didCheck = true;
}
@SuppressWarnings("deprecation")
@Override
public void dontAllow(int reason) {
if (isFinishing()) {
return;
}
Log.i("License","Denied!");
Log.i("License","Reason for denial: "+reason);
licensed = false;
checkingLicense = false;
didCheck = true;
showDialog(0);
}
@SuppressWarnings("deprecation")
@Override
public void applicationError(int reason) {
Log.i("License", "Error: " + reason);
if (isFinishing()) {
return;
}
licensed = true;
checkingLicense = false;
didCheck = false;
showDialog(0);
}
}
protected Dialog onCreateDialog(int id) {
return new AlertDialog.Builder(this)
.setTitle("UNLICENSED APPLICATION DIALOG TITLE")
.setMessage("This application is not licensed, please buy it from the play store.")
.setPositiveButton("Buy", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"http://market.android.com/details?id=" + getPackageName()));
startActivity(marketIntent);
finish();
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNeutralButton("Re-Check", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
doCheck();
}
})
.setCancelable(false)
.setOnKeyListener(new DialogInterface.OnKeyListener(){
public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
Log.i("License", "Key Listener");
finish();
return true;
}
})
.create();
}{
String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
Log.i("Device Id", deviceId);
mHandler = new Handler();
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
mChecker = new LicenseChecker(this, new ServerManagedPolicy(this, new AESObfuscator(SALT, getPackageName(), deviceId)), BASE64_PUBLIC_KEY);
}}
My logcat:
02-21 15:00:06.530: D/AndroidRuntime(1231): Shutting down VM
02-21 15:00:06.530: W/dalvikvm(1231): threadid=1: thread exiting with uncaught exception (group=0xb3ab8ba8)
02-21 15:00:06.550: E/AndroidRuntime(1231): FATAL EXCEPTION: main
02-21 15:00:06.550: E/AndroidRuntime(1231): Process: com.example.testouille, PID: 1231
02-21 15:00:06.550: E/AndroidRuntime(1231): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testouille/com.example.testouille.MainActivity}: java.lang.NullPointerException
02-21 15:00:06.550: E/AndroidRuntime(1231): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-21 15:00:06.550: E/AndroidRuntime(1231): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-21 15:00:06.550: E/AndroidRuntime(1231): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-21 15:00:06.550: E/AndroidRuntime(1231): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-21 15:00:06.550: E/AndroidRuntime(1231): at android.os.Handler.dispatchMessage(Handler.java:102)
02-21 15:00:06.550: E/AndroidRuntime(1231): at android.os.Looper.loop(Looper.java:136)
02-21 15:00:06.550: E/AndroidRuntime(1231): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-21 15:00:06.550: E/AndroidRuntime(1231): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 15:00:06.550: E/AndroidRuntime(1231): at java.lang.reflect.Method.invoke(Method.java:515)
02-21 15:00:06.550: E/AndroidRuntime(1231): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-21 15:00:06.550: E/AndroidRuntime(1231): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-21 15:00:06.550: E/AndroidRuntime(1231): at dalvik.system.NativeStart.main(Native Method)
02-21 15:00:06.550: E/AndroidRuntime(1231): Caused by: java.lang.NullPointerException
02-21 15:00:06.550: E/AndroidRuntime(1231): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
02-21 15:00:06.550: E/AndroidRuntime(1231): at com.example.testouille.MainActivity.<init>(MainActivity.java:152)
02-21 15:00:06.550: E/AndroidRuntime(1231): at java.lang.Class.newInstanceImpl(Native Method)
02-21 15:00:06.550: E/AndroidRuntime(1231): at java.lang.Class.newInstance(Class.java:1208)
02-21 15:00:06.550: E/AndroidRuntime(1231): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-21 15:00:06.550: E/AndroidRuntime(1231): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-21 15:00:06.550: E/AndroidRuntime(1231): ... 11 more
02-21 15:00:14.370: I/Process(1231): Sending signal. PID: 1231 SIG: 9
02-21 15:01:30.460: D/AndroidRuntime(1251): Shutting down VM
02-21 15:01:30.470: W/dalvikvm(1251): threadid=1: thread exiting with uncaught exception (group=0xb3ab8ba8)
02-21 15:01:30.530: E/AndroidRuntime(1251): FATAL EXCEPTION: main
02-21 15:01:30.530: E/AndroidRuntime(1251): Process: com.example.testouille, PID: 1251
02-21 15:01:30.530: E/AndroidRuntime(1251): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testouille/com.example.testouille.MainActivity}: java.lang.NullPointerException
02-21 15:01:30.530: E/AndroidRuntime(1251): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-21 15:01:30.530: E/AndroidRuntime(1251): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-21 15:01:30.530: E/AndroidRuntime(1251): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-21 15:01:30.530: E/AndroidRuntime(1251): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-21 15:01:30.530: E/AndroidRuntime(1251): at android.os.Handler.dispatchMessage(Handler.java:102)
02-21 15:01:30.530: E/AndroidRuntime(1251): at android.os.Looper.loop(Looper.java:136)
02-21 15:01:30.530: E/AndroidRuntime(1251): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-21 15:01:30.530: E/AndroidRuntime(1251): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 15:01:30.530: E/AndroidRuntime(1251): at java.lang.reflect.Method.invoke(Method.java:515)
02-21 15:01:30.530: E/AndroidRuntime(1251): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-21 15:01:30.530: E/AndroidRuntime(1251): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-21 15:01:30.530: E/AndroidRuntime(1251): at dalvik.system.NativeStart.main(Native Method)
02-21 15:01:30.530: E/AndroidRuntime(1251): Caused by: java.lang.NullPointerException
02-21 15:01:30.530: E/AndroidRuntime(1251): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
02-21 15:01:30.530: E/AndroidRuntime(1251): at com.example.testouille.MainActivity.<init>(MainActivity.java:152)
02-21 15:01:30.530: E/AndroidRuntime(1251): at java.lang.Class.newInstanceImpl(Native Method)
02-21 15:01:30.530: E/AndroidRuntime(1251): at java.lang.Class.newInstance(Class.java:1208)
02-21 15:01:30.530: E/AndroidRuntime(1251): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-21 15:01:30.530: E/AndroidRuntime(1251): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-21 15:01:30.530: E/AndroidRuntime(1251): ... 11 more
02-21 15:01:33.890: I/Process(1251): Sending signal. PID: 1251 SIG: 9
I am a "noob" in Java on Android ^^ . Thanks you so much , David
Upvotes: 0
Views: 341
Reputation: 500
I know it's been awhile, however if you or someone else is having a problem with the code this way I am fairly sure the error is down where you are getting ready to make you dialog to let the use it fail to get a good license response.
protected Dialog onCreateDialog(int id) {
return new AlertDialog.Builder(this)
Because it is in a class of its own and note in the class
class MainActivity extends Activity
You can't use "this" because the AlertDialog.Builder requires a Context, you would need something like:
protected Dialog onCreateDialog(int id) {
new AlertDialog.Builder(getApplicationContext())
Another option would be to make a public context variable in the MainActivity the you could simply use that.
class MainActivity extends Activity{
public Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
}
Then down where you create the Dialog you can use the public context from the MainActivity like this:
protected Dialog onCreateDialog(int id) {
new AlertDialog.Builder(context)
Upvotes: 1
Reputation: 347
The only thing I can think of is if you didnt give your application the permissions it needs to run, but I'm apparently not much better than you haha.
Upvotes: 0