question

ganspach avatar image
ganspach asked sam Deactivated answered

Getting Clover account in BroadcastReceiver

So I want to be able to modify the order when new line items are added. I have a BroadcastReceiver that is called whenever a new line item is added, but am having issues getting the clover account so as to create an OrderConnector. I'm creating an intent to get the clover account, but when i startActivityForResult(), I get a NullPointerException deep in the android code, and I don't know how to fix it.
Relevant Code:
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<receiver
android:name=".EventReceiver"
android:exported="true"
android:label="Event Receiver">
<intent-filter>
<action android:name="com.clover.intent.action.LINE_ITEM_ADDED"/>
</intent-filter>
</receiver>

public class EventReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, final Intent inIntent) {
Log.v("EventReceiver", "Received event");
String action = intent.getAction();
if (action.equals("com.clover.intent.action.LINE_ITEM_ADDED")) {
Intent outIntent = new Intent();
outIntent.setClassName("packageName", "packageName.RegisterActivity");
outIntent.putExtra(RegisterActivity.ACTION, RegisterActivity.LINE_ITEMS);
outIntent.putExtra(ORDER_ID, inIntent.getStringExtra(ORDER_ID));
outIntent.putExtra(LINE_ITEM_ID, inIntent.getStringExtra(LINE_ITEM_ID));
outIntent.putExtra(ITEM_ID, inIntent.getStringExtra(ITEM_ID));
outIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(outIntent);
} else Log.e("EventReceiver", "bad event received"); }
}
}

public class RegisterActivity extends Activity{
private static final String TAG = "RegisterActivity";
public Account account;
public OrderConnector orderConnector;
private static final int REQUEST_ACCOUNT = 487;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
Log.v(TAG, "started activity");
intent = getIntent();
if(orderConnector == null) connect();
final String orderId = intent.getStringExtra(ORDER_ID);
final String lintItemId = intent.getStringExtra(LINE_ITEM_ID);
final String itemId = intent.getStringExtra(ITEM_ID);
final Discount discount = new Discount();
discount.setPercentage(10L);
discount.setName("10% Discount");
new AsyncTask<Void, Void, Void>() {
@Override protected Void doInBackground(Void... params) {
try{
Log.v("addDiscount", orderId);
Log.v("addDiscount", discount.getName());
Log.v("addDiscount", orderConnector.toString());
orderConnector.addDiscount(orderId,discount);
} catch (Exception e){ Log.e("addDiscount", " " + e); }
return null; }
}.execute();
startRegister();
}

private void startRegister(){
Intent i = new Intent(Intents.ACTION_START_REGISTER);
i.putExtra(Intents.EXTRA_ORDER_ID, order_id); startActivity(i); }
private void connect() {
Log.v(TAG, "Connecting");
if (account != null) {
orderConnector = new OrderConnector(this, account, null);
orderConnector.connect();
} else startAccountChooser();
Log.v(TAG, "Connected");
}

private void startAccountChooser() {
Intent intent;
if (Build.VERSION.SDK_INT >= 23)
intent = AccountManager.newChooseAccountIntent(null, null, new String[]{CloverAccount.CLOVER_ACCOUNT_TYPE}, null, null, null, null);
else {
intent = AccountManager.newChooseAccountIntent(null, null, new String[]{CloverAccount.CLOVER_ACCOUNT_TYPE}, false, null, null, null, null); }
Log.v(TAG, intent.toString());
Log.v(TAG, this.toString());
startActivityForResult(intent, REQUEST_ACCOUNT);
}
}

Log:06-13 14:47:28.684 31063-31063/?
E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start receiver package.EventReceiver: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2384)
at android.app.ActivityThread.access$1500(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1311)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5051)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.startActivityForResult(Activity.java:3380)
at android.app.Activity.startActivityForResult(Activity.java:3341)
at package.AddDiscountActivity.startAccountChooser(AddDiscountActivity.java:135)
at package.AddDiscountActivity.connect(AddDiscountActivity.java:147)
at package.AddDiscountActivity.addDiscount(AddDiscountActivity.java:73)
at package.EventReceiver.onReceive(EventReceiver.java:31)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2377)
at android.app.ActivityThread.access$1500(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1311)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5051)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Clover Android SDK
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

sam avatar image
sam Deactivated answered
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community