2013-08-04 61 views
0

我一直在試圖實現應用內購買到我的應用程序,但由於某種原因,應用程序退出到主屏幕,每次我嘗試開始我的測試購買。線程退出時未捕獲的異常,沒有錯誤

08-04 18:01:00.659: W/dalvikvm(10597): threadid=17: thread exiting with uncaught exception (group=0x40fd32a0) 
08-04 18:01:00.659: D/AndroidRuntime(10597): Shutting down VM 
08-04 18:01:00.659: W/dalvikvm(10597): threadid=1: thread exiting with uncaught exception (group=0x40fd32a0) 

這是我的logcat,我沒有得到一個錯誤,所以我不知道我做錯了什麼在這裏..

下面是可能導致錯誤的的.java:

package com.mitonanetherlands.buttonchooser; 

import java.util.ArrayList; 

import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.IntentSender.SendIntentException; 
import android.content.ServiceConnection; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.os.RemoteException; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.Toast; 

import com.android.vending.billing.IInAppBillingService; 
import com.mopub.mobileads.MoPubView; 

public class BuyPremium extends Activity { 

    public static int calculateInSampleSize(BitmapFactory.Options options, 
      int reqWidth, int reqHeight) { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 

      // Calculate ratios of height and width to requested height and 
      // width 
      final int heightRatio = Math.round((float) height 
        /(float) reqHeight); 
      final int widthRatio = Math.round((float) width/(float) reqWidth); 

      // Choose the smallest ratio as inSampleSize value, this will 
      // guarantee 
      // a final image with both dimensions larger than or equal to the 
      // requested height and width. 
      inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
     } 

     return inSampleSize; 
    } 

    public static Bitmap decodeSampledBitmapFromResource(Resources res, 
      int resId, int reqWidth, int reqHeight) { 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeResource(res, resId, options); 
    } 

    MoPubView ad1; 
    SharedPreferences completedLevels; 
    Bundle querySku = new Bundle(); 
    Bundle skuDetails; 
    String sku; 
    String price; 
    Bundle buyIntentBundle; 
    IInAppBillingService mService; 
    ServiceConnection mServiceConn = new ServiceConnection() { 
     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      mService = null; 
     } 

     @Override 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      mService = IInAppBillingService.Stub.asInterface(service); 
     } 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.buypremium); 

     ImageView thankyou = (ImageView) findViewById(R.id.thankyou); 
     thankyou.setImageBitmap(decodeSampledBitmapFromResource(getResources(), 
       R.drawable.thank_you, 450, 600)); 
     ad1 = (MoPubView) findViewById(R.id.adviewpremium); 
     ad1.setAdUnitId("d95782d0c22011e295fa123138070049"); 
     ad1.loadAd(); 
     bindService(new Intent(
       "com.android.vending.bulling.InAppBillingService.BIND"), 
       mServiceConn, Context.BIND_AUTO_CREATE); 

     Button backtomain = (Button) findViewById(R.id.BackToMenu); 
     backtomain.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(
         "com.mitonanetherlands.buttonchooser.MAINMENU")); 
      } 
     }); 

     Button BuyPremium = (Button) findViewById(R.id.BuyButton); 
     BuyPremium.setOnClickListener(new View.OnClickListener() { 

      @SuppressWarnings("unused") 
      @Override 
      public void onClick(View arg0) { 
       ArrayList<String> skuList = new ArrayList<String>(); 
       skuList.add("pemiumUpgrade"); 

       querySku.putStringArrayList("premium_apartment_life", skuList); 
       Thread myThread = new Thread(new Runnable() { 
        @Override 
        public void run() { 
         try { 
          Bundle skuDetails = mService.getSkuDetails(3, 
            getPackageName(), "inapp", querySku); 
         } catch (RemoteException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 
       }); 
       myThread.start(); 
       int response = skuDetails.getInt("RESPONSE_CODE"); 
       if (response == 0) { 
        ArrayList<String> responseList 
         = skuDetails.getStringArrayList("DETAILS_LIST"); 

        for (String thisResponse : responseList) { 
         JSONObject object = null; 
        try { 
         object = new JSONObject(thisResponse); 
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
         try { 
         String sku = object.getString("productId"); 
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
         try { 
         String price = object.getString("price"); 
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        } 
       } 
       try { 
        Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), sku, "inapp","Premium is aangekocht!"); 
       } catch (RemoteException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT"); 
       try { 
        startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0),Integer.valueOf(0), Integer.valueOf(0)); 
       } catch (SendIntentException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

     }); 


    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     ad1.destroy(); 
     if (mServiceConn != null) { 
      unbindService(mServiceConn); 
     } 
    } 
    @SuppressWarnings("unused") 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == 1001) {  
     int responseCode = data.getIntExtra("RESPONSE_CODE", 0); 
      String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA"); 
      String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE"); 

      if (resultCode == RESULT_OK) { 
      try { 
       JSONObject jo = new JSONObject(purchaseData); 
       String sku = jo.getString("productId"); 
       Toast.makeText(this,"You have bought the " + sku + ". Excellent choice!", Toast.LENGTH_LONG).show(); 
       SharedPreferences.Editor editor = completedLevels.edit(); 
       editor.putBoolean("Paid", true).commit(); 

       } 
       catch (JSONException e) { 
        Toast.makeText(this,"Failed to Parse data", Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

任何想法?

+1

請發佈更多的logcat,比你展示的高出25行。 – Simon

+0

使用Google的'gSon'庫來解析和創建Java對象。只是說... –

+2

在你的線程'try {/ * your code * /} catch(Throwable t){Log.w(「error is」,t); }' – zapl

回答

0

代碼中有幾件事可能出錯了。

一件事是保證崩潰您的應用程序:

Bundle skuDetails = null; 

public void onClick(View arg0) { 
    int response = skuDetails.getInt("RESPONSE_CODE"); 
} 

這是一個保證NullPointerException只要你點擊。您打算線程分配的東西skuDetails已經但不會:

  • 它使用它自己的局部變量:Bundle skuDetails = mService.get..
  • 即使你使用了正確的變量從線程內不會結束賦值到skuDetails那個時候。它並行運行,完成後無法保證。

你想在這裏做的是既可以做所有的處理是後臺線程內(一切在的onclick完成啓動線程後),或使用一個AsyncTask讀取和背景(doInBackground)過程,然後使用在UI線程內處理的數據進行最後的步驟(onPostExecute(data returned from doInBackground))。

還有其他問題。例如,mService仍然可能是nullnull,因爲該服務未被綁定。

IInAppBillingService mService = null; 
bindService(...); -> causes assignment to `mService` at unknown time in future. 

無法保證mService何時可以安全使用。應用程序初始化後的一段時間,但仍然可以在用戶按下按鈕,並且由於NullPointerException,您的線程可能死亡。檢查你是否沒有訪問null

相關問題