我想以编程方式启用/禁用数据连接.我使用了以下代码:
void enableInternet(boolean yes)
{
ConnectivityManager iMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Method iMthd = null;
try {
iMthd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
} catch (Exception e) {
}
iMthd.setAccessible(false);
if(yes)
{
try {
iMthd.invoke(iMgr, true);
Toast.makeText(getApplicationContext(), "Data connection Enabled", Toast.LENGTH_SHORT).show();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
dataButton.setChecked(false);
Toast.makeText(getApplicationContext(), "IllegalArgumentException", Toast.LENGTH_SHORT).show();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "IllegalAccessException", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
dataButton.setChecked(false);
Toast.makeText(getApplicationContext(), "InvocationTargetException", Toast.LENGTH_SHORT).show();
} …Run Code Online (Sandbox Code Playgroud) 在尝试从正在运行的Java进程中获取堆转储时,我得到了这个Stacktrace.导致这种情况的原因以及如何进行正确的堆转储?
Dumping heap to dump.bin ...
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.tools.jmap.JMap.runTool(JMap.java:201)
at sun.tools.jmap.JMap.main(JMap.java:130)
Caused by: java.lang.InternalError: Metadata does not appear to be polymorphic
at sun.jvm.hotspot.types.basic.BasicTypeDataBase.findDynamicTypeForAddress(BasicTypeDataBase.java:278)
at sun.jvm.hotspot.runtime.VirtualBaseConstructor.instantiateWrapperFor(VirtualBaseConstructor.java:102)
at sun.jvm.hotspot.oops.Metadata.instantiateWrapperFor(Metadata.java:68)
at sun.jvm.hotspot.memory.DictionaryEntry.klass(DictionaryEntry.java:71)
at sun.jvm.hotspot.memory.Dictionary.classesDo(Dictionary.java:66)
at sun.jvm.hotspot.memory.SystemDictionary.classesDo(SystemDictionary.java:190)
at sun.jvm.hotspot.memory.SystemDictionary.allClassesDo(SystemDictionary.java:183)
at sun.jvm.hotspot.utilities.HeapHprofBinWriter.writeClasses(HeapHprofBinWriter.java:942)
at sun.jvm.hotspot.utilities.HeapHprofBinWriter.write(HeapHprofBinWriter.java:427)
at sun.jvm.hotspot.tools.HeapDumper.run(HeapDumper.java:62)
at sun.jvm.hotspot.tools.Tool.startInternal(Tool.java:260)
at sun.jvm.hotspot.tools.Tool.start(Tool.java:223)
at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
at sun.jvm.hotspot.tools.HeapDumper.main(HeapDumper.java:83)
... 6 more
Run Code Online (Sandbox Code Playgroud)
环境:CentOS 64位,Java OpenJDK运行时环境(内置1.8.0_31-b13)OpenJDK 64位服务器VM(内置25.31-b07,混合模式)
Usign ps看到所使用的Java版本:
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.31-1.b13.el6_6.x86_64/jre/bin/java
Run Code Online (Sandbox Code Playgroud)
我的第一次尝试是:
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.31-1.b13.el6_6.x86_64/bin/jmap -dump:format=b,file=dump.bin 14984
Run Code Online (Sandbox Code Playgroud)
这让我:
14984: Unable …Run Code Online (Sandbox Code Playgroud) 我正在使用Cocos2D在Android中制作2D游戏,用Java编写.这是我的主要代码:
public void gameLoop(float dt) {
//Player Gravity
if(canExecuteMovement(0, 6)) {
guy.moveY(6);
}
//Player Movement
if(direction == 1) {
if(canExecuteMovement(-3, 0))
guy.moveX(-3);
} else if(direction == 2) {
if(canExecuteMovement(3, 0))
guy.moveX(3);
}
}
private boolean canExecuteMovement(int xChange, int yChange) {
int projectedX = guy.getBounds().left + xChange;
int projectedY = guy.getBounds().top + yChange;
Log.i("DD", "guy:" + guy.getBounds().toString());
Rect projectedBounds = new Rect(projectedX, projectedY, projectedX + guy.getWidth(), projectedY + guy.getHeight());
Log.i("DD", "guy:" + projectedBounds.toString());
for (int i = 0; i < platformCount; …Run Code Online (Sandbox Code Playgroud) 根据javadocs,InvocationTargetException.getCause()可以为null:
返回此异常的原因(抛出的目标异常,可能为null).
但文档还说它包装了一个现有的异常:
InvocationTargetException是一个已检查的异常,它包装被调用的方法或构造函数抛出的异常.
所以在我看来,InvocationTargetException.getCause() 永远不可能null.
我错过了什么吗?
UPDATE
是的,我错过了一些东西 - 默认的构造函数InvocationTargetException会导致getCause()为null.
我现在的问题是为什么要提供这个类的默认构造函数.是否存在需要使用null原因抛出异常的用例?
如何重新抛出InvocationTargetException的目标异常.我有一个方法,它使用反射来调用我的一个类中的invoke()方法.但是,如果在我的代码中抛出异常,我不关心InvocationTargetException并且只想要目标异常.这是一个例子:
public static Object executeViewComponent(String name, Component c,
HttpServletRequest request) throws Exception {
try {
return c.getClass()
.getMethod(c.getMetaData().getMethod(), HttpServletRequest.class)
.invoke(c, request);
} catch (InvocationTargetException e) {
// throw the target exception here
}
}
Run Code Online (Sandbox Code Playgroud)
我面临的主要问题是调用throw e.getCause(); 不会抛出异常而是抛出一个Throwable.也许我接近这个错误?
我在Xamarin Android中开发应用程序并且有一个非常奇怪的问题:断点和异常已经停止正常工作.当我设置断点时,它永远不会被击中.我在我的项目中设置了断点,但没有一个被击中.这也发生在Visual Studio和Xamarin Studio中.
我还有第二个也更烦人的问题,每次有异常时,它都会在Xamarin Studio中抛出完全无用的"java.lang.reflect.InvocationTargetException"异常,并在Visual Studio中抛出"异常",没有关于异常的更多细节.这只发生在一个特定项目中,其他项目工作正常.我在各种论坛和博客中寻找解决方案,但没有一个有效.我是Xamarin Android开发的新手,希望你能帮助我.
Exoplayer2 在三星、Vivo、小米等设备的发布和调试版本中运行良好。但是,当我在 Play 商店上发布我的应用程序时,它在某些设备(例如 Nexus、Pixel 手机)上抛出错误。
Crashlytics showing this error: Caused by android.view.InflateException:
Binary XML file line #11: Error inflating class com.google.android.exoplayer2.ui.PlayerView
Run Code Online (Sandbox Code Playgroud)
我发现已经就同一问题提出了一些类似的问题,但对我来说没有任何用处。(我使用的是默认播放器,没有任何自定义)任何人都可以帮我解决这个问题吗?
我的代码:
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/playerView"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
var player:SimpleExoPlayer?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.pdf_view_normal)
player = ExoPlayerFactory.newSimpleInstance(this)
val mediaDataSourceFactory = DefaultDataSourceFactory(this, getUserAgent(this, "demo.smart.com"))
val mediaSource = ProgressiveMediaSource.Factory(mediaDataSourceFactory)
.createMediaSource(Uri.parse(""))
with(player) {
this!!.prepare(mediaSource, false, false)
playWhenReady = true
}
playerView.setShutterBackgroundColor(Color.TRANSPARENT)
playerView.player = player
playerView.requestFocus()
}
override fun onDestroy() {
super.onDestroy()
if(player!=null){
player!!.stop()
}
}
Run Code Online (Sandbox Code Playgroud)
摇篮 …
crash android invocationtargetexception inflate-exception exoplayer2.x
我是javafx全新的!! 我尝试了一个非常简单的代码而且卡住了.当我尝试在布局中添加按钮时它不起作用.我知道这个问题可能太简单但我真的不知道如何解决它.如果你能帮助我,我将不胜感激.这是我的代码:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application{
Button button;
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Title");
StackPane layout = new StackPane();
button = new Button();
layout.getChildren().add(button);
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at …Run Code Online (Sandbox Code Playgroud) 我试图根据我从Node.js API获得的JSON响应,在命名的StackPanel中动态添加按钮.我将这个JSON数组解析为一个对象数组,该对象具有将所有属性放在适当位置的类.然后,在foreach中,我想使用我放在对象数组中的信息来构建按钮.请注意,对象数组包含良好的属性.如果我试着简单地MessageBox.Show(item.name)在我的foreach中做一个,它将显示我所有对象的所有名称.没有值为null,我检查了三次.
以下是我创建按钮的方法:
ItemList[] items = JsonConvert.DeserializeObject<ItemList[]>(jsonString);
Dispatcher.BeginInvoke(new Action(() =>
{
foreach (ItemList item in items)
{
Button ItemName = new Button();
//defining properties of the button, like width, etc..
ItemName.Content = item.title;
ItemName.Name = item.id;
//Will add Click event.
ItemPanel.Children.Add(ItemName);
}
}));
Run Code Online (Sandbox Code Playgroud)
在尝试时,我得到一个TargetInvocationException:
[System.Reflection.TargetInvocationException {System.Reflection.TargetInvocationException:调用目标抛出了异常.---> System.NullReferenceException:对象引用未设置为对象的实例.
这是为什么 ?神秘.我试着创建按钮对象并将其添加到我的StackPanel中而不做任何其他事情,并且错误是相同的.
我究竟做错了什么 ?如何摆脱这个?我已经在另一个应用程序中创建了这样的动态元素,这样做有效.
函数创建按钮在函数末尾调用,该函数使用HTTP请求从api获取JSON.在OnNavigatedTo方法中调用那个HTTP请求函数(是的,这很奇怪,但这是我发现在到达页面时自动调用函数的唯一方法,使用来自OnNavigatedTo方法的变量.我在其他页面上做过,而且它是工作(虽然它不是创建按钮,只是修改现有的文本块).
这是完整的错误:
System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. --->
System.NullReferenceException: Object reference not set to an instance of an object.
at CubbyHole_Mobile.BrowseFiles.<>c__DisplayClass1.b__0() …Run Code Online (Sandbox Code Playgroud) c# nullreferenceexception invocationtargetexception windows-phone-8
我正在构建一个小型 D&D 副项目,但在使用 JavaFX 时遇到问题。我试图在保持同一舞台的同时穿过几个场景。我正在使用 MVC 设计,并在调用控制器时传递 PrimaryStage。但是,当我尝试从 UI 类调用并从控制器获取舞台时,它会抛出 InvocableTargetException。
主要类别:
package dnd;
import javafx.application.Application;
import javafx.stage.Stage;
public class DnD extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Dungeons and Dragons");
StartMenuCtrl startMenuCtrl = new StartMenuCtrl(primaryStage);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
开始菜单控制器类:
package dnd;
import javafx.stage.Stage;
public class StartMenuCtrl {
private final StartMenuUI startMenuUI;
private final Stage primaryStage;
public StartMenuCtrl(Stage primaryStage) {
this.startMenuUI = new StartMenuUI(this);
this.primaryStage = new Stage(); …Run Code Online (Sandbox Code Playgroud)