适用于 Android 应用程序的 Java 代码中的实例化异常
Instantiation exception in Java code for Android app
我已经编写了从 HTML 格式的轮播滑块图像中获取当前页面的代码,并通过电子邮件和彩信在 Android 中使用 Java 进行 phonegap 项目共享。
这是我的 Java 代码:
public class Share extends CordovaPlugin {
private FileOutputStream outStream;
private File file;
Bitmap bm;
public static final String ACTION_POSITION = "ShareImage";
Context context;
public Share(Context context) {
this.context = context;
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
throws JSONException {
if (ACTION_POSITION.equals(action)) {
try {
JSONObject arg_object = args.getJSONObject(0);
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("image/jpg");
String uri = "@drawable/"+arg_object.getString("image")+".jpg";
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
bm = BitmapFactory.decodeResource( context.getResources(), imageResource);
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
file = new File(extStorageDirectory, "image.png");
try {
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, arg_object.getString("image"));
this.cordova.getActivity().startActivity(sendIntent);
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
callbackContext.error(e.getMessage());
return false;
}
}
return true;
}
}
一切顺利,但是当我单击"共享"按钮时,我收到异常,因为"找不到类"。问题出在上下文类中,尝试通过清理并删除Google中给出的gen文件,但没有任何效果。请帮我解决这个问题。
日志输出:
11-09 20:30:44.156: W/System.err(2842): java.lang.InstantiationException: com.picsswipe.Share
11-09 20:30:44.156: W/System.err(2842): at java.lang.Class.newInstanceImpl(Native Method)
11-09 20:30:44.156: W/System.err(2842): at java.lang.Class.newInstance(Class.java:1409)
11-09 20:30:44.156: W/System.err(2842): at org.apache.cordova.api.PluginEntry.createPlugin(PluginEntry.java:80)
11-09 20:30:44.164: W/System.err(2842): at org.apache.cordova.api.PluginManager.getPlugin(PluginManager.java:249)
11-09 20:30:44.164: W/System.err(2842): at org.apache.cordova.api.PluginManager.exec(PluginManager.java:206)
11-09 20:30:44.164: W/System.err(2842): at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:51)
11-09 20:30:44.164: W/System.err(2842): at android.webkit.WebViewCore.nativeHandleTouchEvent(Native Method)
11-09 20:30:44.164: W/System.err(2842): at android.webkit.WebViewCore.nativeHandleTouchEvent(Native Method)
11-09 20:30:44.164: W/System.err(2842): at android.webkit.WebViewCore.access$6200(WebViewCore.java:54)
11-09 20:30:44.164: W/System.err(2842): at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1658)
11-09 20:30:44.164: W/System.err(2842): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 20:30:44.164: W/System.err(2842): at android.os.Looper.loop(Looper.java:130)
11-09 20:30:44.164: W/System.err(2842): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:685)
11-09 20:30:44.164: W/System.err(2842): at java.lang.Thread.run(Thread.java:1019)
share.js
插件:
var Share = function() {};
Share.prototype.show = function(success, fail, path) {
return cordova.exec( function(args) {
success(args);
}, function(args) {
fail(args);
}, 'Share', 'ShareImage', [{"image": path}]);
};
if(!window.plugins) {
window.plugins = {};
}
if (!window.plugins.share) {
window.plugins.share = new Share();
}
config.xml
:
<feature name="Share">
<param name="android-package" value="com.picsswipe.Share"/>
</feature>
<?xml version="1.0" encoding="utf-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<cordova>
<!--
access elements control the Android whitelist.
Domains are assumed blocked unless set otherwise
-->
<access origin="http://127.0.0.1*"/> <!-- allow local pages -->
<!-- <access origin="https://example.com" /> allow any secure requests to example.com -->
<!-- <access origin="https://example.com" subdomains="true" /> such as above, but including subdomains, such as www -->
<access origin=".*"/>
<!-- <content src="http://mysite.com/myapp.html" /> for external pages -->
<content src="home.html" />
<log level="DEBUG"/>
<preference name="useBrowserHistory" value="true" />
<preference name="exit-on-suspend" value="false" />
<plugins>
<plugin name="App" value="org.apache.cordova.App"/>
<plugin name="Geolocation" value="org.apache.cordova.GeoBroker"/>
<plugin name="Device" value="org.apache.cordova.Device"/>
<plugin name="Accelerometer" value="org.apache.cordova.AccelListener"/>
<plugin name="Compass" value="org.apache.cordova.CompassListener"/>
<plugin name="Media" value="org.apache.cordova.AudioHandler"/>
<plugin name="Camera" value="org.apache.cordova.CameraLauncher"/>
<plugin name="Contacts" value="org.apache.cordova.ContactManager"/>
<plugin name="File" value="org.apache.cordova.FileUtils"/>
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/>
<plugin name="Notification" value="org.apache.cordova.Notification"/>
<plugin name="Storage" value="org.apache.cordova.Storage"/>
<plugin name="FileTransfer" value="org.apache.cordova.FileTransfer"/>
<plugin name="Capture" value="org.apache.cordova.Capture"/>
<plugin name="Battery" value="org.apache.cordova.BatteryListener"/>
<plugin name="SplashScreen" value="org.apache.cordova.SplashScreen"/>
<plugin name="Echo" value="org.apache.cordova.Echo" />
<plugin name="Globalization" value="org.apache.cordova.Globalization"/>
<plugin name="InAppBrowser" value="org.apache.cordova.InAppBrowser"/>
<plugin name="SQLitePlugin" value="com.phonegap.plugin.sqlitePlugin.SQLitePlugin"/>
<plugin name="Sync" value="com.phonegap.plugin.syncPlugin.SyncDB" />
</plugins>
</cordova>
这是我拥有的默认配置.xml。您需要在 value 属性中指定插件名称和类名,如上面的 xml 所示。
请参考以下链接 http://docs.phonegap.com/en/2.7.0/guide_plugin-development_index.md.html
如果有帮助,请告诉我。
相关文章:
- 如何在GoogleWeb工具包(GWT)中从JSNI调用接口(实例化)
- 如何在用户输入时实例化数组
- 在requirejs中共享实例化对象
- Mongoose TypeError:实例化模式类型的对象时,对象不是函数
- Angular重命名模块后未能实例化模块
- Backbone relational无法实例化两个RelationalModel对象
- 我应该如何用javascript实例化这个日期
- AngularJS出错-无法实例化模块
- 谷歌地图重叠MarkerSpiderfier实例化问题
- 未捕获错误:由于,[$injector:moduler]未能实例化模块polmgr
- ngRoute不是因果报应实例化的
- JavaScript 中的动态实例化
- 在文本框上绑定谷歌地点自动完成,而无需实例化谷歌地图
- UI5 路由不实例化视图
- 在 JavaScript 中,当我们实例化派生对象时,原型的函数隐藏在哪里
- 仅在需要时实例化聚合物元素
- 角度 JS 控制器未实例化
- 如何理解“当类实例化时,JavaScript静态方法也是不可调用的”
- 在 Typescript 中实例化对象,遵循松散耦合
- 适用于 Android 应用程序的 Java 代码中的实例化异常