my app get crashing frequently due to NullPointerException and Native crash and thus I am losing my play store rank day by day. someone please help me on this.. is there any way if banner ad is not ready, can I show native ad (optional question)?
NullPointerException at: mAdView.loadAd(new AdRequest.Builder().build());
My app build version 26.0.1 and using Android Studio 3.0 Beta 2.
my code:
public class B1 extends Fragment {
public B1() {
// Required empty public constructor
}
private AdView mAdView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_b1, container, false);
//Banner
mAdView = rootView.findViewById(R.id.adView);
mAdView.loadAd(new AdRequest.Builder().build());
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
mAdView.setVisibility(View.VISIBLE);
}
#Override
public void onAdFailedToLoad(int error) {
mAdView.setVisibility(View.GONE);
}
});
return rootView;
}
}
The banner is ready. Your findViewById is returning null. Make sure rootView actually contains the AdView R.id.adView
Related
I use react-native to develop an app, and I need to connect to braintree (paypal value). The official provides 3 kinds of sdk, js, android, and ios. I try to connect to the native android library, but it doesn't seem to have any effect.After calling the native method in ReactNative, only "react-native-test" is output (No error is reported, and there is no change on the interface. It seems that it should jump to PayPal authorization to be normal). I'm not sure if it's my problem . I also tried to use js library in react-native, but after importing a certain method, my program doesn't start. Or can I only do it in webView? Has anyone connected with PayPal value? Can you give me some advice? Thanks.
Here is the documentation I refer to.
Below is my android codeļ¼
public class BraintreeValueModule extends ReactContextBaseJavaModule implements PayPalListener {
private static ReactApplicationContext reactContext;
private Callback successCallback;
private Callback errorCallback;
private BraintreeClient braintreeClient;
private PayPalClient payPalClient;
public BraintreeValueModule(ReactApplicationContext context) {
super(context);
reactContext = context;
}
#NonNull
#Override
public String getName() {
return "BraintreeValueModule";
}
#ReactMethod
public void test(){
System.out.println("react-native-test");
braintreeClient = new BraintreeClient(reactContext.getApplicationContext(), "sandbox_ykbznr4s_ctmssyj6wz2qcj2g");
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
activity.runOnUiThread(new MyRunnable(activity,braintreeClient));
//The following writing method will report an error: Method addObserver must be called on the main thread
//if(activity != null){
// payPalClient = new PayPalClient(activity, braintreeClient);
// payPalClient.setListener(this);
//}
}
#Override
public void onPayPalSuccess(#NonNull PayPalAccountNonce payPalAccountNonce) {
successCallback.invoke(payPalAccountNonce.toString());
}
#Override
public void onPayPalFailure(#NonNull Exception error) {
if (error instanceof UserCanceledException) {
// user canceled
errorCallback.invoke("use canceled");
} else {
// handle error
errorCallback.invoke("error");
}
}
}
public class MyRunnable implements Runnable, PayPalListener {
private BraintreeClient braintreeClient;
private PayPalClient payPalClient;
private FragmentActivity activity;
MyRunnable(FragmentActivity activity,BraintreeClient braintreeClient){
this.activity = activity;
this.braintreeClient = braintreeClient;
}
#Override
public void run() {
if(activity != null){
payPalClient = new PayPalClient(activity, braintreeClient);
payPalClient.setListener(this);
}
}
#Override
public void onPayPalSuccess(#NonNull PayPalAccountNonce payPalAccountNonce) {
System.out.println(payPalAccountNonce.getString());
}
#Override
public void onPayPalFailure(#NonNull Exception error) {
if (error instanceof UserCanceledException) {
// user canceled
System.out.println("use canceled");
} else {
// handle error
System.out.println("error");
}
}
}
I believe you might have missed out the following few lines:
PayPalVaultRequest request = new PayPalVaultRequest();
request.setBillingAgreementDescription("Your agreement description");
payPalClient.tokenizePayPalAccount(getCurrentActivity(), request);
However, I believe it might still not work as I had similar problem when I was trying to integrate the drop-in.
I'm afraid that you'll need to initialise your clients (in your case, BraintreeClient and PaypalClient) in the onCreate method of your MainActivity.
And then try to call a reference to the client (either by SharedPreference or static variable) in your module to launch the drop in.
Pretty sure it's similar issue to your case.
This only applies to the v4 library.
Read more on this thread:
https://github.com/braintree/braintree-android-drop-in/issues/374#issuecomment-1345929549
I need to open a view using the code with the help of HandlerUtil, how can I do it in Eclipse RCP ?
I've tried with PlatformUI.
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("MyView");
but I need to perform it with HandlerUtil
Use getActiveWorkbenchWindow in HandlerUtil:
public class MyHandler extends AbstractHandler {
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchPage workbenchPage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
workbenchPage.showView("MyView");
}
}
In Vaadin Flow, there exists no Component that is essentially a Scrollable Layout.
In Vaadin 8, this was done by the Panel.
Is there a way to achieve a scrollable Component in Vaadin Flow?
Edit: I have now published an add-on here that provides the class VerticalScrollLayout, and also the class HorizontalScrollLayout. If there are suggestions for improvements, feel free to contact me or comment here.
Yes it is possible, although there is no existing Component that does it automatically.
The way to go is placing a VerticalLayout (for a vertical scroll bar) inside another component, and setting the display property of that VerticalLayout from flex to block. (credits to Diego Sanz Villafruela in the vaadin forum)
I have made my own VerticalScrollLayout class that does it all for you, so that using it in a view is as easy as using a simple VerticalLayout
public class VerticalScrollLayout extends VerticalLayout {
private VerticalLayout content;
public VerticalScrollLayout(){
preparePanel();
}
public VerticalScrollLayout(Component... children){
preparePanel();
this.add(children);
}
private void preparePanel() {
setWidth("100%");
setHeight("100%");
getStyle().set("overflow", "auto");
content = new VerticalLayout();
content.getStyle().set("display", "block");
content.setWidth("100%");
content.setPadding(false);
super.add(content);
}
public VerticalLayout getContent(){
return content;
}
#Override
public void add(Component... components){
content.add(components);
}
#Override
public void remove(Component... components){
content.remove(components);
}
#Override
public void removeAll(){
content.removeAll();
}
#Override
public void addComponentAsFirst(Component component) {
content.addComponentAtIndex(0, component);
}
}
There is now an official component for scrolling:
https://vaadin.com/docs/latest/components/scroller
I have implemented glide very well but the problem is that it loads same image for different imageviews with different urls in recyclerview.
The main question is how can I load the required images and still keep local caching to save user data? Here is my code:
Glide.with(context).load(objects.get(position)).apply(new RequestOptions().fitCenter().error(R.drawable.logo).placeholder(R.drawable.logo).diskCacheStrategy(DiskCacheStrategy.ALL)).into(new SimpleTarget<Drawable>() {
#Override
public void onResourceReady(#NonNull Drawable resource, #Nullable Transition<? super Drawable> transition) {
imageView.setImageDrawable(resource);
progressBar.setVisibility(View.GONE);
}
});
Instead of doing something like this:
.into(new SimpleTarget<Drawable>() {
#Override
public void onResourceReady(#NonNull Drawable resource, #Nullable Transition<? super Drawable> transition) {
imageView.setImageDrawable(resource);
progressBar.setVisibility(View.GONE);
}
});
Just simply put into the into function your imageView. If you still want to be notified whenever loading has been done you can use listener function. Your code should look like this:
Glide
.with(context)
.load(objects.get(position))
.apply(new
RequestOptions()
.fitCenter()
.error(R.drawable.logo)
.placeholder(R.drawable.logo)
.diskCacheStrategy(DiskCacheStrategy.ALL)
)
.listener(new SimpleTarget<Drawable>() {
#Override
public void onResourceReady(#NonNull Drawable resource, #Nullable
Transition<? super Drawable> transition) {
progressBar.setVisibility(View.GONE);
}
})
.into(imageView);
Glide should handle the RecyclerView lifecycle by its own. The only thing that you need to do is to hide progress bar. You do not need to set the drawable into the ImageView by yourself.
UPDATE:
The mentioned approach is the best however it gives me some lint errors when using SimpleTarget<?>() inside .listener(), maybe be due to possible version difference and has forced me to use RequestListener<Drawable>() and it works:
Glide.with(holder.service_image.getContext()).load(imageArray.get(0)).apply(new RequestOptions().fitCenter().error(R.drawable.logo).placeholder(R.drawable.logo).diskCacheStrategy(DiskCacheStrategy.ALL)).listener(new RequestListener<Drawable>() {
#Override
public boolean onLoadFailed(#Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
holder.progressBar.setVisibility(View.GONE);
return false;
}
}).into( holder.service_image);
I've build simple service in android studio to run a service every second in console log,
and I want to implement my android studio code in react native
there's a way to do that?
let say I've a code :
myService.class
public class myService extends Service {
private Handler handler= new Handler();
private boolean run = true;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onStart(Intent i, int startId){
super.onStart(i, startId);
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (run){
Log.e("Second", "test");
}
handler.postDelayed(this,1000);
}
},1000);
}
public void onDestroy(){
super.onDestroy();
run=false;
Log.d("Test", "Screen on");
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onPause() {
super.onPause();
Log.d("Test", "Screen off");
startService(new Intent(this, myService.class));
}
#Override
protected void onResume() {
super.onResume();
startService(new Intent(this, myService.class));
}
}
You can use RN Native modules. For background tasks Headless JS is useful. And for listening events LifecycleEventListener is what you are looking for. getReactApplicationContext().startService(new Intent(getReactApplicationContext(), myService.class) will do the rest. I am ready for further help
Please refer to https://facebook.github.io/react-native/docs/native-modules-android
You can follow the docs: https://facebook.github.io/react-native/docs/native-modules-android
The other answer pretty much covers the way you implement RN Modules. A useful tip is how to send events to JavaScript, such as below:
private void sendEvent(ReactContext reactContext,
String eventName,
#Nullable WritableMap params) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
...
WritableMap params = Arguments.createMap();
...
sendEvent(reactContext, "keyboardWillShow", params);
Further Reading (for your intended feature) for background tasks [ANDROID]
Just to add, you seem like you want to create a background task in React Native. Now from experience, if you want to run something every second - this will work as expected, until the device goes into Doze mode. If you don't want the service to run in the background or Doze mode - that's fine. If so, you may want to start reading about Doze mode and how to test your service in a Doze mode environment.
The issue with background tasks, is that if the phone is idle or stationary - the phone will go into Doze mode. This impacts upon some functionality, such as network. It is expected that if you need to perform actions in Doze mode that you do within a Maintenance Window
Now, I've managed to overcome some issues - by using an Alarm Clock Manager and resetting it to stop Doze mode. However, this does not work in all cases. You'll need a combination of that and a service to keep it alive (but will act differently on a lot of phones). Sometimes the GC just ditches it and kills the process.
Useful links:
Testing your service in Doze mode:
https://developer.android.com/training/monitoring-device-state/doze-standby#testing_doze
Understanding Doze:
https://developer.android.com/training/monitoring-device-state/doze-standby#understand_doze