AsyncLoader - wont accept 'this' - java

This is a follow up to a question I asked here.
I have copied and pasted this code from this tutorial. When I paste it into Android Studio, the 'this' parameter of of content.getLoadManager.initLoader() is highlighted in red and shows the following error:
Wrong 3rd Argument Type. Found 'com.example.carl.loaderDemo.FooLoaderClient', requried: 'android.app.LoaderManager.LoaderCallBacks
I've ran into this previously (see first link). I was hoping this tutorial would help but I just seem to be going in endless circles!
Can anyone point me in the right direction?!
package com.example.carl.loaderdemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
public class FooLoader extends AsyncTaskLoader {
public FooLoader(Context context, Bundle args) {
super(context);
// do some initializations here
}
public String loadInBackground() {
String result = "";
// ...
// do long running tasks here
// ...
return result;
}
}
class FooLoaderClient implements LoaderManager.LoaderCallbacks {
Activity context;
// to be used for support library:
// FragmentActivity context2;
public Loader onCreateLoader(int id, Bundle args) {
// init loader depending on id
return new FooLoader(context, args);
}
#Override
public void onLoadFinished(Loader loader, Object data) {
}
public void onLoaderReset(Loader loader) {
// ...
}
public void useLoader() {
Bundle args = new Bundle();
// ...
// fill in args
// ...
Loader loader =
context.getLoaderManager().initLoader(0, args, this);
// with support library:
// Loader loader =
// context2.getSupportLoaderManager().initLoader(0, args, this);
// call forceLoad() to start processing
loader.forceLoad();
}
}
Screenshot of error message:

There is a mismatch in your imports:
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
But you need
import android.app.LoaderManager;
import android.content.AsyncTaskLoader;
import android.content.Loader;
You cannot mix the support library with the android framework. Alternatively you can subclass FragmentActivity and call getSupportLoaderManager() instead.

You're implementing android.support.v4.app.LoaderManager.LoaderCallbacks but the client is expecting android.app.LoaderManager.LoaderCallbacks. You need to be consistent in which loader API you're using.

Related

codename native Android application subclassing issues

My codenameone application crashes anything I use this native code
package com.mycompany.interfaces;
import android.app.Application;
import android.content.Context;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
public class InitialiseApp extends Application{
private static Context context;
public static Context getContext() {
return context;
}
#Override
public void onCreate()
{
super.onCreate();
context = getApplicationContext();
try
{
FirebaseApp.initializeApp(this, new FirebaseOptions.Builder().
setApiKey("XXXXXXXXXXXXXX").
setApplicationId("XXXXXXXX").
setGcmSenderId("XXXXXXXXXX")
.build());
FirebaseInstanceId.getInstance().deleteInstanceId();
FirebaseInstanceId.getInstance().getToken("XXXXXXXXXX",FirebaseMessaging.INSTANCE_ID_SCOPE);
FirebaseMessaging.getInstance().subscribeToTopic("test");
}
catch(Exception c)
{
c.printStackTrace();
}
}
}
I declare the class in the android.xapplication_attr android:name="com.mycompany.interfaces.InitialiseApp"
Need a assistance
Are you putting this in the native interface stub or in the CN1 part of the code?
Also, I don’t think that’s how you get a context in CN1. Look in the developer guide and video tutorials for Native Interfaces. I also recall a series of blog posts about native interfaces that dive into writing the Android code. You’ll need to use something from the AndroidNativeUtil class like: AndroidNativeUtil.getActivity().

Item textures are pink/black

I've tried to modify minecraft by adding a new item called "uranium". Therefore I created the class "Trauma.java" in the main package and a few other classes listed below.
All packages and classes:
Package Explorer
Trauma.java
package main;
import items.ItemUranium;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import proxy.ServerProxy;
#Mod(modid = Trauma.MODID)
public class Trauma {
public static final String MODID = "Trauma";
#SidedProxy(clientSide = "proxy.ClientProxy", serverSide = "proxy.ServerProxy")
public static ServerProxy proxy;
public static ItemUranium uranium = new ItemUranium();
#EventHandler
public void preInit(FMLPreInitializationEvent event) {
GameRegistry.register(uranium);
}
#EventHandler
public void init(FMLInitializationEvent event) {
proxy.registerClientStuff();
}
#EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
}
BasicItem.java
package items;
import net.minecraft.item.Item;
public class BasicItem extends Item {
public BasicItem(String name) {
setUnlocalizedName(name);
setRegistryName(name);
}
}
ItemUranium.java
package items;
public class ItemUranium extends BasicItem {
public ItemUranium() {
super("uranium");
}
}
ClientProxy.java
package proxy;
import items.BasicItem;
import main.Trauma;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
public class ClientProxy extends ServerProxy {
#Override
public void registerClientStuff () {
registerItemModel(Trauma.uranium);
}
public static void registerItemModel(BasicItem item) {
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Trauma.MODID + ":" + item.getRegistryName(), "inventory"));
}
}
ServerProxy.java
package proxy;
public class ServerProxy {
public void registerClientStuff() {}
}
uranium.json
{
"parent": "item/generated",
"textures": {
"layer0": "Trauma:items/uranium"
}
}
uranium.png
ingame
Also I don't know why the item in inventory isn't called uranium...
I spent two hours on fixing the problem and it didn't help so it would be really nice if somebody of you may help me.
Thanks :)
Don't use the Model Mesher:
The model mesher is Vanilla (Mojang) code and using it correctly has always been finicky and unreliable, failing if you called it too early and failing if you called it too late. So Forge added the ModelLoader class to resolve that problem.
Replace this line:
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(...)
With this line:
ModelLoader.setCustomModelResourceLocation(...)
The ... contents are identical.
Second, depending on what version of Minecraft you're using, you should...:
Stop using GameRegistry.Register
Instead use the RegistryEvent.Register<T> events (where <T> will be <Block> to register blocks, <Item> to register items, etc)
Register your models in the ModelRegistryEvent and no where else.
This event is #SideOnly(CLIENT) and can be subscribed to in your client proxy, avoiding the need to forward references through your proxy class. Eg. I do it like this, where lines 197-199 is the most common scenario needed, where the array is populated during the item registration event. The rest of that method handles the custom state mappers and custom mesh definitions that are used by only a handful of items/blocks and not relevant here.
Include your Mod ID in your unlocalized name. The best way to do this would be setUnlocalizedName(getRegistryName().toString());
See also the Forge documentation on events.

React Native: Sending ReactApplicationContext to non-React classes in android

My junior and I are working on a React Native application which responds to calls made by the Android system. We have two classes which need to communicate between the two classes shown below: MyModule (the first code block), which should be notified by OtherClass (the second code block) when an event happens in the Android system.
What we're seeing that is:
We create an instance of MyModule, which extends ReactContextBaseJavaModule, where we pass the ReactApplicationContext in via its Constructor.
This creates an instance of OtherClass and passes the ReactApplicationContext into OtherClass, again via its constructor.
In OtherClass, the method onEvent is called when a certain system event is fired. When this happens, OtherClass' private variable reactContext becomes null.
So what we're trying to do is allow our ReactNative application to receive messages from an Android class when a given system event happens. We've attempted to use the "tutorial" at http://facebook.github.io/react-native/docs/native-modules-android.html but, because this is provided without full source code, we're getting a bit lost.
Any suggestions please?
Thanks!
OtherClass.java:
package com.ats;
import com.ats.SuperClass;
import com.ats.MyModule;
import android.content.Context;
import com.ats.MyModule;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import java.util.Date;
import javax.annotation.Nullable;
public class OtherClass extends SuperClass {
private ReactApplicationContext reactContext;
public OtherClass(ReactApplicationContext reactContext) {
super();
this.reactContext = reactContext;
}
public OtherClass() {
super();
}
#Override
protected void onEventTriggered(Context ctx, String number, Date start) {
WritableMap params = Arguments.createMap();
params.putInt("test", 0);
sendEvent("sendCall", params);
}
private void sendEvent(String eventName, #Nullable WritableMap params) {
System.out.println("OtherClass ReactAppContext: " + this.reactContext); //ReactContext is being lost at this point(Nullpointexeception)
this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
}
}
...
MyModule.java:
package com.ats;
import com.ats.OtherClass;
import android.telecom.Call;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReadableMap;
import javax.annotation.Nullable;
/**
* Provides a bridge between JavaScript and Java
*/
public class MyModule extends ReactContextBaseJavaModule {
private OtherClass OR;
/**
* Our local copy of the React Application Context
*/
/**
* Construct an instance of this class and the OtherClass Java class
*
* #param reactContext
*/
public MyModule(ReactApplicationContext reactContext) {
super(reactContext);
this.OR = new OtherClass(reactContext);
}
/**
* Define the name of this ReactNative [Native] Module
*
* #return String
*/
#Override
public String getName() {
return "Module";
}
/**
* Perform a simple println to test whether this code is reachable
*/
#ReactMethod
public void print() {
System.out.println("React Method Print");
}
}

NFC on Android using JavaFX Ports

I have been recently experimenting with JavaFXPorts and I have been trying to use it to build a native Android app. For the application I'm building I am trying to bake in NFC support but there doesn't seem to be much information out there about this. The only useful guide I found so far was some sample code written by johanvos on his BitBucket Repo here.
The problem I am facing now is using this code snippet, Netbeans keeps on reporting that the android libraries I'm trying to reference don't exist.
This is a some of my code:
package com.afropolymath.waitress;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import android.content.Context;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.ReaderCallback;
import android.nfc.Tag;
import android.os.Bundle;
import javafxports.android.FXActivity;
public class Waitress extends Application implements ReaderCallback {
private Stage stage;
private StackPane rootLayout;
#Override
public void start(Stage stage) {
this.stage = stage;
this.initLayout();
Context ctx = FXActivity.getInstance();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(ctx);
nfcAdapter.enableReaderMode(FXActivity.getInstance(), this, NfcAdapter.FLAG_READER_NFC_A, Bundle.EMPTY);
}
public void initLayout() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/RootLayout.fxml"));
rootLayout = (StackPane) loader.load();
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(rootLayout, visualBounds.getWidth(), visualBounds.getHeight());
stage.getIcons().add(new Image(Waitress.class.getResourceAsStream("/assets/icon.png")));
stage.setScene(scene);
stage.show();
} catch (IOException e) {
}
}
#Override
public void onTagDiscovered(Tag tag) {
}
}
And these are the errors I'm getting:
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:12: error: package android.content does not exist
import android.content.Context;
^
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:13: error: package android.nfc does not exist
import android.nfc.NfcAdapter;
^
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:14: error: package android.nfc.NfcAdapter does not exist
import android.nfc.NfcAdapter.ReaderCallback;
^
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:15: error: package android.nfc does not exist
import android.nfc.Tag;
^
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:16: error: package android.os does not exist
import android.os.Bundle;
^
Any ideas on what might have been the problem?
While Johan Vos' NFC project works, it is based on the use of android and dalvik dependencies on the main package:
dependencies {
compile files("${ANDROID_HOME}/platforms/android-21/android.jar")
compile "org.javafxports:jfxdvk:8u60-b3"
}
But JavaFXPorts it's intended to create projects that can be deployed on different platforms, so now if you create a project using the Gluon's Plugin for NetBeans, you'll find four different folders to place the code:
Source Packages [Java], for all the common code, shared with all the platforms
Desktop/Java Packages, for Java code, only available running on Desktop
Android/Java Packages, for Java or Android code, only available running on Android
Ios/Java Packages, for Java code, only available running on iOS.
Also, if you check the project dependencies, you'll notice that the android.jar is available only for Android.
This means that you should create your regular project on the main package, and add the android part only in the Android package. In order to call the android class, you'll need to provide some mechanism on the main package to create an instance of it.
If you check the HelloPlatform sample, you'll notice that a PlatformService is used to load the classes depending on the platform, while PlatformProvider is an interface with the method/s that can be called from the main package, but will have the implementation given to each platform.
With this idea, but using Class.forName() instead of the service, Gluon Charm-Down library implements different native services.
You can have a look also at the GoNative sample and post that explains it in detail.
In case you want to implement a new service, like the NFC for Android,
these are the possible classes required.
Main package
NFCPlatform
public abstract class NFCPlatform {
public abstract NFCService getNFCService();
}
NFCService
public interface NFCService {
boolean isAvailable();
boolean isEnabled();
StringProperty tagIdProperty();
// other methods
}
Android Package
AndroidNFCPlatform
public class AndroidNFCPlatform extends NFCPlatform {
private AndroidNFCService nfcService;
#Override
public NFCService getNFCService() {
if (nfcService == null) {
nfcService = new AndroidNFCService();
}
return nfcService;
}
}
AndroidNFCService
Your implementation of the NFC service. Here we can use Android API. NetBeans won't complain.
This is a minimal implementation:
public class AndroidNFCService implements NFCService, ReaderCallback {
private final NfcAdapter nfcAdapter;
private StringProperty tagId;
public AndroidNFCService() {
NfcManager manager = (NfcManager) FXActivity.getInstance().getSystemService(FXActivity.NFC_SERVICE);
nfcAdapter = manager.getDefaultAdapter();
}
#Override
public boolean isAvailable() {
return nfcAdapter != null;
}
#Override
public boolean isEnabled() {
return isAvailable() && nfcAdapter.isEnabled();
}
#Override
public StringProperty tagIdProperty() {
if (tagId == null) {
tagId = new SimpleStringProperty();
}
return tagId;
}
#Override
public void onTagDiscovered(Tag tag) {
Platform.runLater(() ->
tagIdProperty().set(new String(tag.getId())));
}
}
Finally, you can use this service on your main class, once you get an instance of the NFCPlatform.
public static NFCPlatform getNFCPlatform() {
try {
if ("android".equals(System.getProperty("javafx.platform", "desktop"))) {
return (NFCPlatform) Class.forName("<your package>.AndroidNFCPlatform").newInstance();
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
System.out.println("Error " + ex);
}
return null;
}
#Override
public void start(Stage stage) {
NFCService nfcService = getNFCPlatform().getNFCService();
if (nfcService != null) {
System.out.println("Available: " + nfcService.isAvailable());
...
}
}

Native functions throw UnsatisfiedLinkError in custom view, despite working in main activity

For some reason I can only call native functions from my main activity and not any custom views that I've created. Here is an example file (I followed a tutorial, but renamed the classes http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/)
See the usage of the native function "getNewString".
package com.example.native;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
public class NativeTestActivity extends Activity
{
static
{
System.loadLibrary("nativeTest");
}
private native String getNewString();
#Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(new BitmapView(this));
String hello = getNewString(); // This line works fine
new AlertDialog.Builder(this).setMessage(hello).show();
}
}
class BitmapView extends View
{
static
{
System.loadLibrary("nativeTest");
}
private native String getNewString();
public BitmapView(Context context)
{
super(context);
String hello = getNewString(); // This line throws the UnsatisfiedLinkError
new AlertDialog.Builder(this.getContext()).setMessage(hello).show();
}
}
How can I call native functions in my custom views?
I've built the application as an Android 2.2 app. I'm running the application on my HTC Desire. I have the latest SDK (9) and latest NDK (r5).
Your problem is that you are trying to call the native function from a class where it dont belongs to.
You defined the following JNI function in your c file:
jstring Java_com_example_native_NativeTestActivity_getNewString()
This states that the native function when loaded will bind with the method declared as native in NativeTestActivity class. So when you try to call it from your View class it doesn't find any function to bind to.
In that case it will look for the following function (which of course does not exist in your .so):
jstring Java_com_example_native_BitmapView_getNewString()
If you still want to be able to call the same function from different classes you can declare it in a container class that can be accessed from any class you want.
eg:
java code:
package com.example.native;
public class NativeHelper {
public native String getNewString();
static
{
System.loadLibrary("nativeTest");
}
}
c code:
jstring Java_com_example_native_NativeHelper_getNewString(JNIEnv* env, jobject javaThis)
{
return (*env)->NewStringUTF(env, "Hello from native code!");
}

Categories