我正在尝试编写插件以在react-
native中获得带有包名称和图标的应用列表。我正在以JSONArray的形式获取所有必需的数据,并且图像已存储在sdcard中。
我正在使用回调将结果(JSON数组)返回给javascript(成功或失败)。我收到上述错误。
请找到以下获取详细信息的Java代码。
包com.sampleapp;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.views.drawer.ReactDrawerLayoutManager;
import com.facebook.react.views.image.ReactImageManager;
import com.facebook.react.views.progressbar.ReactProgressBarViewManager;
import com.facebook.react.views.scroll.ReactHorizontalScrollViewManager;
import com.facebook.react.views.scroll.ReactScrollViewManager;
import com.facebook.react.views.switchview.ReactSwitchManager;
import com.facebook.react.views.text.ReactRawTextManager;
import com.facebook.react.views.text.ReactTextViewManager;
import com.facebook.react.views.text.ReactVirtualTextViewManager;
import com.facebook.react.views.textinput.ReactTextInputManager;
import com.facebook.react.views.toolbar.ReactToolbarManager;
import com.facebook.react.views.view.ReactViewManager;
import com.facebook.react.views.viewpager.ReactViewPagerManager;
public class ToastModule extends ReactContextBaseJavaModule {
private static final String DURATION_SHORT_KEY = "SHORT";
private static final String DURATION_LONG_KEY = "LONG";
public static JSONArray applist;
public ToastModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "ToastAnd";
}
@ReactMethod
public void show(Callback errorCallback, Callback successCallback) {
try {
//get a list of installed apps.
PackageManager pm = getReactApplicationContext().getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(0);
JSONArray app_list = new JSONArray();
int cnt = 0;
String path = getSDPath();
makeRootDirectory(path + "/com.framework.xxx/");
makeRootDirectory(path + "/com.framework.xxx/Cache/");
for (ApplicationInfo packageInfo : packages) {
try {
if (getReactApplicationContext().getPackageManager().getLaunchIntentForPackage(packageInfo.packageName) != null) {
JSONObject info = new JSONObject();
info.put("name", packageInfo.loadLabel(pm));
info.put("packagename", packageInfo.packageName);
String img_name = "/com.ionicframework.xxx/Cache/" + packageInfo.packageName + ".png";
info.put("img", path + img_name);
//cheak exist or not
File cheakfile = new File(path + img_name);
if (!cheakfile.exists()) {
Drawable icon = pm.getApplicationIcon(packageInfo);
if (icon != null) {
drawableTofile(icon, path + img_name);
}
}
app_list.put(cnt++, info);
}
} catch (Exception ex) {
System.err.println("Exception: " + ex.getMessage());
}
}
applist = app_list;
successCallback.invoke(applist);
} catch (Exception e) {
errorCallback.invoke(e.getMessage());
}
}
public static void drawableTofile(Drawable drawable,String path)
{
File file = new File(path);
Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
fos.write(bitmapdata);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getSDPath()
{
File SDdir = null;
boolean sdCardExist= Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(sdCardExist){
SDdir=Environment.getExternalStorageDirectory();
}
if(SDdir!=null){
return SDdir.toString();
}
else{
return null;
}
}
public static void makeRootDirectory(String filePath) {
File file = null;
try {
file = new File(filePath);
if (!file.exists()) {
file.mkdirs(); //make Directory
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
在android.index.js中,我正在调用本机方法,如下所示:
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
} = React;
var toastMessage = require('./sampleToast');
toastMessage.show(
(msg) => { console.log(msg); },
(result) => { alert(JSON.stringify(result)); }
);
var SampleApp = React.createClass({
render: function() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.android.js
</Text>
<Text style={styles.instructions}>
Shake or press menu button for dev menu
</Text>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('SampleApp', () => SampleApp);
但是在Java代码中返回applist(successCallback.invoke(applist);)时,我遇到了上述错误。
你能帮我哪里我做错了。谢谢
详细错误:
Exception in native call from JS
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: java.lang.RuntimeException: Cannot convert argument of type class org.json.JSONArray
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at com.facebook.react.bridge.Arguments.fromJavaArgs(Arguments.java:55)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at com.facebook.react.bridge.CallbackImpl.invoke(CallbackImpl.java:27)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at com.sampleapp.ToastModule.show(ToastModule.java:100)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at java.lang.reflect.Method.invokeNative(Native Method)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at java.lang.reflect.Method.invoke(Method.java:515)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at com.facebook.react.bridge.BaseJavaModule$JavaMethod.invoke(BaseJavaModule.java:106)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at com.facebook.react.bridge.NativeModuleRegistry$ModuleDefinition.call(NativeModuleRegistry.java:143)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at com.facebook.react.bridge.NativeModuleRegistry.call(NativeModuleRegistry.java:64)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at com.facebook.react.bridge.CatalystInstance$NativeModulesReactCallback.call(CatalystInstance.java:366)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at android.os.Handler.handleCallback(Handler.java:733)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at android.os.Handler.dispatchMessage(Handler.java:95)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at android.os.Looper.loop(Looper.java:136)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at com.facebook.react.bridge.queue.MessageQueueThread$1.run(MessageQueueThread.java:137)
11-14 09:01:24.780 20592-20624/com.sampleapp E/unknown:React: at java.lang.Thread.run(Thread.java:841)
使用本机网桥时,应使用包中的WritableNative
组件调用回调com.facebook.react.bridge
。
而不是a JSONObject
使用WritableNativeMap
。
而不是a JSONArray
,请使用WritableNativeArray
。
例如,带有WritableNativeMap
:
@ReactMethod
public void reactMethod(Callback onSuccess) {
WritableMap resultData = new WritableNativeMap();
resultData.putString("key1", "data");
resultData.putString("key2", "data2");
onSuccessCallback(resultData);
}
对于您的情况,您应该具有以下内容:
public static WritableArray applist;
// ...
@ReactMethod
public void show(Callback errorCallback, Callback successCallback) {
try {
//get a list of installed apps.
PackageManager pm = getReactApplicationContext().getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(0);
WritableArray app_list = new WritableNativeArray();
// ...
for (ApplicationInfo packageInfo : packages) {
try {
if (getReactApplicationContext().getPackageManager().getLaunchIntentForPackage(packageInfo.packageName) != null) {
WritableMap info = new WritableNativeMap();
info.putString("name", packageInfo.loadLabel(pm).toString());
info.putString("packagename", packageInfo.packageName);
// ...
app_list.pushMap(info);
}
} catch (Exception ex) {
System.err.println("Exception: " + ex.getMessage());
}
}
applist = app_list;
successCallback.invoke(applist);
} catch (Exception e) {
errorCallback.invoke(e.getMessage());
}
}
C# 10 / .NET 6 / System.Text.Json 我正在使用一个作为JSON响应返回的API。我正在尝试使用将JSON响应反序列化为一个类。我收到了一个JsonException,可以帮助理解我做错了什么。 我调用API并存储JSON响应:< code > string json = await Retreive。Fetch(目标); 下面是< code >控制台的输出。Writ
问题内容: 我想知道为什么你不能做: 我发现这将需要运行时在片上执行循环以转换每个元素,这将是非惯用的Go。这很有道理。 然而,这可能不会被刚刚走样编译器解决的,所以在内部它们是相同的,他们使用相同类型的头底下?我猜答案虽然不是我好奇为什么。 问题答案: 这个: 是类型转换。根据规范,转换具有特定的规则: 在以下任何一种情况下,可以将非恒定值转换为类型: 是分配给。 的类型,并且具有相同的基础类型
被这个错误卡住了:
在下面来自的语法中,泛型类型参数在实例化原始类型数组后用于类型转换,
我面临泛型的问题。 下面是我使用泛型的一个类: 在这里,我们的类:使用进行参数化。 我想在我们的一种方法中指定类型,如下所示: 但是,我遇到以下编译器错误: