2015-10-16 92 views
1

当我在本地运行我的应用程序时,它会运行,但是当我想广播它时,出现以下错误,我不知道为什么?无法调度事件

10-16 09:53:46.114 5990-5990 /? E /事件:无法分派事件:类com.bakerframework.baker.events.DownloadManifestCompleteEvent订阅类的类com.bakerframework.baker.model.RemoteIssueCollection 10-16 09:53:46.114 5990-5990 /? E/Event:java.lang.NullPointerException 10-16 09:53:46.114 5990-5990 /? E /事件:在com.bakerframework.baker.model.RemoteIssueCollection.processJson(RemoteIssueCollection.java:197)

package com.bakerframework.baker.events; 

public class DownloadManifestCompleteEvent { 

    public DownloadManifestCompleteEvent() { 
    } 
}  

public class RemoteIssueCollection implements IssueCollection { 

private final HashMap<String, Issue> issueMap; 
private List<String> categories; 

private List<Sku> subscriptionSkus; 

// Tasks management 
private DownloadManifestJob downloadManifestJob; 
private FetchPurchasesJob fetchPurchasesJob; 

// Data Processing 
final String JSON_ENCODING = "utf-8"; 
final SimpleDateFormat SDF_INPUT = new SimpleDateFormat(BakerApplication.getInstance().getString(R.string.format_input_date), Locale.US); 
final SimpleDateFormat SDF_OUTPUT = new SimpleDateFormat(BakerApplication.getInstance().getString(R.string.format_output_date), Locale.US); 

// Categories 
public static final String ALL_CATEGORIES_STRING = "All Categories"; 

// Billing 
@NonNull 
private Inventory inventory; 

public RemoteIssueCollection() { 
    // Initialize issue map 
    issueMap = new HashMap<>(); 
    EventBus.getDefault().register(this); 
} 

public List<String> getCategories() { 
    return categories; 
} 

public List<Sku> getSubscriptionSkus() { 
    return subscriptionSkus; 
} 

public List<String> getIssueProductIds() { 
    List<String> issueProductIdList = new ArrayList<>(); 
    for(Issue issue : getIssues()) { 
     if(issue.getProductId() != null && !issue.getProductId().equals("")) { 
      issueProductIdList.add(issue.getProductId()); 
     } 
    } 
    return issueProductIdList; 
} 

public List<Issue> getIssues() { 
    if(isLoading() || issueMap == null) { 
     return new ArrayList<>(); 
    }else{ 
     return new ArrayList<>(issueMap.values()); 
    } 
} 

public Issue getIssueBySku(Sku sku) { 
    return getIssueByProductId(sku.id); 
} 

public Issue getIssueByProductId(String productId) { 
    for(Issue issue : getIssues()) { 
     if(issue.getProductId() != null && issue.getProductId().equals(productId)) { 
      return issue; 
     } 
    } 
    return null; 
} 

public boolean isLoading() { 
    return (downloadManifestJob != null && !downloadManifestJob.isCompleted()) || (fetchPurchasesJob != null && !fetchPurchasesJob.isCompleted()); 
} 

// Reload data from backend 
public void load() { 
    if (!isLoading()) { 
     if (BakerApplication.getInstance().isNetworkConnected()) { 
      // Online Mode: Reload issue collection 
      downloadManifestJob = new DownloadManifestJob(Configuration.getManifestUrl(), getCachedFile()); 
      BakerApplication.getInstance().getJobManager().addJobInBackground(downloadManifestJob); 
     }else if(isCacheAvailable()) { 
      processManifestFile(getCachedFile()); 
     }else{ 
      EventBus.getDefault().post(new IssueCollectionErrorEvent(new Exception("No cached file available"))); 
     } 
    } 
} 

public void processManifestFileFromCache() { 

} 

private void processManifestFile(File file) { 

    try { 

     // Create issues 
     processJson(FileHelper.getJsonArrayFromFile(file)); 

     // Process categories 
     categories = extractAllCategories(); 

     // you only need this if this activity needs information about purchases/SKUs 
     if(BakerApplication.getInstance().isNetworkConnected()) { 
      inventory = BakerApplication.getInstance().getCheckout().loadInventory(); 
      inventory.whenLoaded(new InventoryLoadedListener()); 
      inventory.load(); 
     } 

     // Instantly trigger load event 
     EventBus.getDefault().post(new IssueCollectionLoadedEvent()); 

    } catch (JSONException e) { 
     Log.e(this.getClass().getName(), "processing error (invalid json): " + e); 
    } catch (IOException e) { 
     Log.e(this.getClass().getName(), "processing error (buffer error): " + e); 
    } catch (ParseException e) { 
     Log.e(this.getClass().getName(), "processing error (parse error): " + e); 
    } 

} 

private void processJson(final JSONArray jsonArray) throws JSONException, ParseException, UnsupportedEncodingException { 
    JSONObject json; 
    JSONArray jsonCategories; 
    List<String> categories; 
    List<String> issueNameList = new ArrayList<>(); 

    // Loop through issues 
    int length = jsonArray.length(); 
    for (int i = 0; i < length; i++) { 
     json = new JSONObject(jsonArray.getString(i)); 

     // Get issue data from json 
     String issueName = jsonString(json.getString("name")); 
     String issueProductId = json.isNull("product_id") ? null : jsonString(json.getString("product_id")); 
     String issueTitle = jsonString(json.getString("title")); 
     String issueInfo = jsonString(json.getString("info")); 
     String issueDate = jsonDate(json.getString("date")); 
     Date issueObjDate = jsonObjDate(json.getString("date")); 
     String issueCover = jsonString(json.getString("cover")); 
     String issueUrl = jsonString(json.getString("url")); 
     int issueSize = json.has("size") ? json.getInt("size") : 0; 

     Issue issue; 
     if(issueMap.containsKey(issueName)) { 
      // Get issue from issue map 
      issue = issueMap.get(issueName); 
      // Flag fields for update 
      if(!issue.getCover().equals(issueCover)) { 
       issue.setCoverChanged(true); 
      } 
      if(!issue.getUrl().equals(issueUrl)) { 
       issue.setUrlChanged(true); 
      } 
     }else{ 
      // Create new issue and store in issue map 
      issue = new Issue(issueName); 
      issueMap.put(issueName, issue); 
     } 

     // Set issue data 
     issue.setTitle(issueTitle); 
     issue.setProductId(issueProductId); 
     issue.setInfo(issueInfo); 
     issue.setDate(issueDate); 
     issue.setObjDate(issueObjDate); 
     issue.setCover(issueCover); 
     issue.setUrl(issueUrl); 
     issue.setSize(issueSize); 

     // Set categories 
     if(json.has("categories")) { 
      jsonCategories = json.getJSONArray("categories"); 
      categories = new ArrayList<>(); 
      for (int j = 0; j < jsonCategories.length(); j++) { 
       categories.add(jsonCategories.get(j).toString()); 
      } 
      issue.setCategories(categories); 
     }else{ 
      issue.setCategories(new ArrayList<String>()); 
     } 

     // Add name to issue name list 
     issueNameList.add(issueName); 

    } 

    // Get rid of old issues that are no longer in the manifest 
    for(Issue issue : issueMap.values()) { 
     if(!issueNameList.contains(issue.getName())) { 
      issueMap.remove(issue); 
     } 
    } 

} 

// Helpers 

private String jsonDate(String value) throws ParseException { 
    return SDF_OUTPUT.format(SDF_INPUT.parse(value)); 
} 

private Date jsonObjDate(String value) throws ParseException { 
    return SDF_INPUT.parse(value); 
} 

private String jsonString(String value) throws UnsupportedEncodingException { 
    if(value != null) { 
     return new String(value.getBytes(JSON_ENCODING), JSON_ENCODING); 
    }else{ 
     return null; 
    } 
} 

private String getCachedPath() { 
    return Configuration.getCacheDirectory() + File.separator + BakerApplication.getInstance().getString(R.string.path_shelf); 
} 

private File getCachedFile() { 
    return new File(getCachedPath()); 
} 

public boolean isCacheAvailable() { 
    return getCachedFile().exists() && getCachedFile().isFile(); 
} 

public void updatePrices(Inventory.Products inventoryProducts, List<String> productIds) { 

    // Update google-play subscriptions 
    if(inventoryProducts != null) { 
     boolean hasSubscription = false; 
     subscriptionSkus = new ArrayList<>(); 
     final Inventory.Product subscriptionProductCollection = inventoryProducts.get(SUBSCRIPTION); 
     if (subscriptionProductCollection.supported) { 
      for (Sku sku : subscriptionProductCollection.getSkus()) { 
       subscriptionSkus.add(sku); 
      } 
     } 

     // Update google-play purchased issues 
     final Inventory.Product inAppProductCollection = inventoryProducts.get(IN_APP); 
     if (inAppProductCollection.supported) { 
      // Update issue prices 
      for (Sku sku : inAppProductCollection.getSkus()) { 
       Issue issue = getIssueBySku(sku); 
       if(issue != null) { 
        // Check for subscription 
        issue.setPurchased(inAppProductCollection.isPurchased(sku)); 
        issue.setSku(sku); 
       } 
      } 
     } else { 
      Log.e(getClass().getName(), "Error: " + R.string.err_purchase_not_possible); 
     } 
    } 

    // Update backend-purchased issues 
    if(productIds != null) { 
     for (String productId : productIds) { 
      Issue issue = getIssueByProductId(productId); 
      if(issue != null) { 
       issue.setPurchased(true); 
      } 
     } 
    } 
} 

public List<String> extractAllCategories() { 

    // Collect all categories from issues 
    List<String> allCategories = new ArrayList<>(); 

    for(Issue issue : issueMap.values()) { 
     for(String category : issue.getCategories()) { 
      if(allCategories.indexOf(category) == -1) { 
       allCategories.add(category); 
      } 
     } 
    } 

    // Sort categories 
    Collections.sort(allCategories); 

    // Append all categories item 
    allCategories.add(0, ALL_CATEGORIES_STRING); 

    return allCategories; 
} 


public List<Issue> getDownloadingIssues() { 
    List<Issue> downloadingIssues = new ArrayList<>(); 
    for (Issue issue : issueMap.values()) { 
     if(issue.isDownloading()) { 
      downloadingIssues.add(issue); 
     } 
    } 
    return downloadingIssues; 
} 

public void cancelDownloadingIssues(final List<Issue> downloadingIssues) { 
    for (Issue issue : downloadingIssues) { 
     if(issue.isDownloading()) { 
      issue.cancelDownloadJob(); 
     } 
    } 
} 

public Issue getIssueByName(String issueName) { 
    return issueMap.get(issueName); 
} 

private class InventoryLoadedListener implements Inventory.Listener { 
    @Override 
    public void onLoaded(@NonNull Inventory.Products inventoryProducts) { 
     // Load existing purchases from backend 
     fetchPurchasesJob = new FetchPurchasesJob(Configuration.getManifestUrl()); 
     BakerApplication.getInstance().getJobManager().addJobInBackground(fetchPurchasesJob); 
     } 
    } 

    // @SuppressWarnings("UnusedDeclaration") 
    public void onEventMainThread(DownloadManifestCompleteEvent event) { 
     processManifestFile(getCachedFile()); 
    } 

    // @SuppressWarnings("UnusedDeclaration") 
    public void onEventMainThread(DownloadManifestErrorEvent event) { 
     Log.i("IssueCollection", "DownloadManifestErrorEvent"); 
     if(isCacheAvailable()) { 
      processManifestFile(getCachedFile()); 
     }else{ 
      EventBus.getDefault().post(new IssueCollectionErrorEvent(new Exception("No cached file available"))); 
     } 
    } 

    // @SuppressWarnings("UnusedDeclaration") 
    public void onEventMainThread(FetchPurchasesCompleteEvent event) { 

     // Set purchased issues 
     updatePrices(inventory.getProducts(), event.getFetchPurchasesResponse().issues); 

     // Trigger issues loaded event 
     EventBus.getDefault().post(new IssueCollectionLoadedEvent()); 

    } 


    // @SuppressWarnings("UnusedDeclaration") 
    public void onEventMainThread(FetchPurchasesErrorEvent event) { 

     // Set purchased issues 
     updatePrices(inventory.getProducts(), null); 

     // Trigger issues loaded event 
     EventBus.getDefault().post(new IssueCollectionLoadedEvent()); 

    } 

    public Inventory getInventory() { 
     return inventory; 
    } 

} 

XML

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.bakerframework.baker" android:versionCode="10" android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="20" /> 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /> 
<uses-permission android:name="com.android.vending.BILLING" /> 
<uses-permission android:name="com.android.vending.CHECK_LICENSE" /> 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.VIBRATE" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

<!--^permission_c2d--><permission android:name="com.magloft.demo.permission.C2D_MESSAGE" android:protectionLevel="signature" /><!--$permission_c2d--> 
<!--^uses_permission_c2d--><uses-permission android:name="com.magloft.demo.permission.C2D_MESSAGE" /><!--$uses_permission_c2d--> 

<application 
    android:allowBackup="true" 
    android:hardwareAccelerated="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:windowSoftInputMode="adjustPan" 
    android:vmSafeMode="true" 
    android:allowClearUserData="true" 
    android:largeHeap="true" 
    android:name=".BakerApplication"> 
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 

    <activity 
     android:name=".activity.SplashActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@android:style/Theme.Black.NoTitleBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".activity.ShelfActivity" 
     android:configChanges="orientation|screenSize" 
     android:label="@string/app_name" 
     android:theme="@style/custom_actionbar"> 
    </activity> 
    <activity 
     android:name=".activity.IssueActivity" 
     android:configChanges="orientation|screenSize" 
     android:parentActivityName=".activity.ShelfActivity" 
     android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" > 
    </activity> 
    <activity 
     android:name=".activity.InfoActivity" 
     android:configChanges="orientation|screenSize" 
     android:parentActivityName=".activity.ShelfActivity" 
     android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" > 
    </activity> 
    <activity 
     android:name="com.bakerframework.baker.settings.SettingsActivity" 
     android:configChanges="orientation|screenSize" 
     android:parentActivityName=".activity.ShelfActivity" 
     android:theme="@style/custom_actionbar" > 
    </activity> 
    <activity 
     android:name=".activity.ModalActivity" 
     android:configChanges="orientation|screenSize" 
     android:parentActivityName=".activity.IssueActivity" 
     android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" > 
    </activity> 

    <service android:name="com.parse.PushService" /> 
    <receiver android:name="com.parse.ParseBroadcastReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <action android:name="android.intent.action.USER_PRESENT" /> 
     </intent-filter> 
    </receiver> 
    <receiver android:name="com.parse.ParsePushBroadcastReceiver" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="com.parse.push.intent.RECEIVE" /> 
      <action android:name="com.parse.push.intent.DELETE" /> 
      <action android:name="com.parse.push.intent.OPEN" /> 
     </intent-filter> 
    </receiver> 
    <receiver android:name="com.parse.GcmBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND"> 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
      <!--^gcm_category_name--><category android:name="com.magloft.demo" /><!--$gcm_category_name--> 
     </intent-filter> 
    </receiver> 
</application> 

你能帮助我请。如果你需要别的东西,我可以给你更多的信息。

+0

whai是线..RemoteIssueCollection.java没有197: –

+0

INT length = jsonArray.length(); ---- 197 for(int i = 0; i Zisk

+0

我想你会得到null jsonArray ...检查它。 –

回答

0

如果打开的ProGuard,你应该添加下面的规则在您proguard-rules.pro

-keep class your package name.template.bean.** {*;} 
-keep class your package name.wxapi.bean.** {*;} 
-keep class de.greenrobot.event.** {*;} 
-keepclassmembers,includedescriptorclasses class ** { 
    public void onEvent*(**);`enter code here` 
    void onEvent*(**); 
} 

#all the beans you post with eventbus.post 

希望能帮助你