PDA

View Full Version : کرش برنامه هنگام چک کردن نصب بودن کافه بازار



omidgs
شنبه 15 خرداد 1395, 08:51 صبح
با سلام من پرداخت درون برنامه ای رو برای برنامه ام درست کردم و میخوام وقتی برنامه کافه بازار روی گوشی کاربر نصب نباشه پیامی نشون داده بشه که کافه بازار رو نصب کنه . طبق آموزش هایی که پیدا کرم این کد ها رو نوشتم ولی متاسفانه به محض ورود به صفحه پرداخت کرش میکنه و برنامه بسته میشه کد ها ی برنامه و ارور لاگ رو میزارم لطفا کمک کنید عیب کار کجات تشکر



package ir.omidghasemi;


public class Buy1 extends AppCompatActivity {

Button kharid;

Dialog wdialog;

TextView kharidtxt;


public static final String PREFS_NAME = "MyPrefsFile33";

// Debug tag, for logging
static final String TAG = "htag";

// SKUs for our products: the premium upgrade (non-consumable)
static final String SKU_PREMIUM = "sg1";

// Does the user have the premium upgrade?
boolean mIsPremium = false;

// (arbitrary) request code for the purchase flow
static final int RC_REQUEST = 10021;

// The helper object
IabHelper mHelper;
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener;
IabHelper.QueryInventoryFinishedListener mGotInventoryListener;







//public key

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buy1);
getWindow().setFlags(WindowManager.LayoutParams.FL AG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FL AG_KEEP_SCREEN_ON);


wdialog = new Dialog(Buy1.this);
wdialog.requestWindowFeature(Window.FEATURE_NO_TIT LE);
wdialog.setContentView(R.layout.wdialog);
wdialog.show();
wdialog.setCancelable(false);


final ProgressBar pbar = (ProgressBar) wdialog.findViewById(R.id.bar);

kharidtxt = (TextView) wdialog.findViewById(R.id.kharidtxt);
final Typeface Homa = Typeface.createFromAsset(getAssets(), "fonts/Homa.ttf");

kharidtxt.setTypeface(Homa);

pbar.setVisibility(View.VISIBLE);

// Create a Handler instance on the main thread
final Handler handler = new Handler();

// Create and start a new Thread
new Thread(new Runnable() {
public void run() {
try{
Thread.sleep(3000);
}
catch (Exception e) { } // Just catch the InterruptedException

// Now we use the Handler to post back to the main thread
handler.post(new Runnable() {
public void run() {
// Set the View's visibility back on the main UI Thread
pbar.setVisibility(View.INVISIBLE);
wdialog.hide();
}
});
}
}).start();












kharid = (Button) findViewById(R.id.kharid);


kharid.setText("خرید 500 سکه به قیمت پانصد تومان");
final Typeface Homa1 = Typeface.createFromAsset(getAssets(), "fonts/Homa.ttf");

kharid.setTypeface(Homa1);

//retrieve Coin
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
G.omid = settings.getInt("key_point", G.omid);
Level4.pointer = settings.getString("key_pointer", Level4.pointer);


kharid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (isPackageInstalled("com.farsitel.bazaar")) {
// Bazar is installed
try {
mHelper.launchPurchaseFlow(Buy1.this, SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener, "payload-string");
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(Buy1.this,
"Reopen App and Try Again", Toast.LENGTH_SHORT).show();
}
}
else {

// not installed
}
}
});

String base64EncodedPublicKey = "bkey";

mHelper = new IabHelper(Buy1.this, base64EncodedPublicKey);


mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
if (result.isFailure()) {
Log.d(TAG, "Failed to query inventory: " + result);
return;
}
else {
Log.d(TAG, "Query inventory was successful.");
// does the user have the premium upgrade?
mIsPremium = inventory.hasPurchase(SKU_PREMIUM);
if (mIsPremium){
MasrafSeke(inventory.getPurchase(SKU_PREMIUM));
}
// update UI accordingly

Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
}

Log.d(TAG, "Initial inventory query finished; enabling main UI.");
}
};

mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
if (result.isFailure()) {
Log.d(TAG, "Error purchasing: " + result);
Toast.makeText(Buy1.this, "خرید ناموفق بود", Toast.LENGTH_SHORT).show();
return;
}
else if (purchase.getSku().equals(SKU_PREMIUM)) {
// give user access to premium content and update the UI

Toast.makeText(Buy1.this,"خرید موفق",Toast.LENGTH_SHORT).show();


MasrafSeke(purchase);


}
}
};


Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d(TAG, "Setup finished.");

if (!result.isSuccess()) {
// Oh noes, there was a problem.
Log.d(TAG, "Problem setting up In-app Billing: " + result);
}
// Hooray, IAB is fully set up!
mHelper.queryInventoryAsync(mGotInventoryListener) ;
}
});



}// oncreate









@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);

// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
} else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}


@Override
public void onDestroy() {
super.onDestroy();
if (isPackageInstalled("com.farsitel.bazaar")) {
if (mHelper != null) mHelper.dispose();
mHelper = null;

}
else{
Toast.makeText(Buy1.this, "برای خرید کافه بازار را نصب کنید", Toast.LENGTH_LONG).show();
}
}

private void MasrafSeke(Purchase kala){
// برای اینکه کاربر فقط یکبار بتواند از کالای فروشی استفاده کند
// باید بعد از خرید آن کالا را مصرف کنیم
// در غیر اینصورت کاربر با یکبار خرید محصول می تواند چندبار از آن استفاده کند
mHelper.consumeAsync(kala, new IabHelper.OnConsumeFinishedListener() {
@Override
public void onConsumeFinished(Purchase purchase, IabResult result) {
if (result.isSuccess())

G.omid = G.omid + 500;
Level4.pointer = String.valueOf(G.omid);
Level4.point.setText(Level4.pointer);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("key_point", G.omid);
editor.putString("key_pointer", Level4.pointer);

// Commit the edits!

editor.commit();
Toast.makeText(Buy1.this, "خرید 500 سکه با موفقیت انجام شد", Toast.LENGTH_SHORT).show();
finish();

Log.d(TAG, "NATIJE masraf: " + result.getMessage() + result.getResponse());

}
});
}

public boolean isPackageInstalled(String PackageName) {
PackageManager manager = getPackageManager();
boolean isAppInstalled = false;
try {
manager.getPackageInfo(PackageName, PackageManager.GET_ACTIVITIES);
isAppInstalled = true;
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return isAppInstalled;
}



}








اینم لاگ



06-04 04:48:42.890 1703-1703/ir.omidghasemi.golvajeh E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.omidghasemi.golvajeh/ir.omidghasemi.golvajeh.Buy1}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThre ad.java:123)
at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99 )
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.jav a:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at ir.omidghasemi.golvajeh.Utils.IabHelper.startSetup (IabHelper.java:267)
at ir.omidghasemi.golvajeh.Buy1.onCreate(Buy1.java:20 5)
at android.app.Activity.performCreate(Activity.java:4 465)
at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThre ad.java:123)
at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99 )
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.jav a:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:551)
at dalvik.system.NativeStart.main(Native Method)

BOB
یک شنبه 16 خرداد 1395, 10:47 صبح
سلام
IabHelper قبل از استفاده نیاز به متدی برای initialize ندارد؟؟