我的目标是在我的Android程序崩溃时显示一些错误消息.
我在我的程序的应用程序(MyApplication)onCreate中注册了一个未捕获的异常处理程序:
Thread.setDefaultUncaughtExceptionHandler(new HelloUncaughtExceptionHandler());
Run Code Online (Sandbox Code Playgroud)
当发现异常时,我想为用户启动一个新活动来保存/提交错误.这就是我目前正在做的事情:
class HelloUncaughtExceptionHandler implements UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable e) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
e.printStackTrace(printWriter);
String stacktrace = result.toString();
printWriter.close();
Intent intent = new Intent(MyApplication.this,
BugReportActivity.class);
intent.putExtra("stacktrace", stacktrace);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到一个没有BugReportActivity出现的空白屏幕(如果我从一个活动开始它工作正常)."MyApplication.this"让我担心......是否真的无法从我的应用程序中启动任意活动?
谢谢大家.
这个问题可能听起来有点傻;)
如何处理UncaughtExceptionHandler中未捕获异常的可能性?
我有一个模拟骰子卷的程序,并将它们与图表中的值(字符串列表集)进行比较.我目前从TEdit获得价值.如果该框为空,则会引发应该由我的Try/Except语句捕获的EConvertError,但事实并非如此.想法和建议?代码如下,Delphi 7.
try
//Shooting
if ShootingRadio.Checked then
BS := StrToInt(Edit1.Text);
Randomize;
Roll := RandomRange(1,7);
Label3.Caption := IntToStr(Roll);
if (Roll < StrToInt(ShootingHitChart[BS-1])) then
begin
Label3.Caption := (IntToStr(Roll)+' Miss');
RichView1.AddTextNL((IntToStr(Roll)+' Miss'),7,0,1);
RichView1.Reformat;
end
else
begin
Label3.Caption := (IntToStr(Roll)+' Hit');
RichView1.AddTextNL((IntToStr(Roll)+' Hit'),6,0,1);
RichView1.Reformat;
end;
except
MessageBox(0,'No number entered.','Error',mb_OK);
end;
Run Code Online (Sandbox Code Playgroud) 我正在解决这个问题.这些是我使用的代码,并引起了上述问题.
$(document).ready(function () {
$("#at-site-btn").bind("tap", function () {
$.mobile.changePage("view/dialog/at-site.php", { transition:"slidedown", role:"dialog" });
});
$('#at-site-page').live('pagecreate', function(){
var $checked_emp = $("input[type=checkbox]:checked");
var $this = $(this);
var $msg = $this.find("#at-site-msg");
$checked_emp.appendTo($msg);
$checked_emp.trigger('create');
$msg.trigger('create');
$(document).trigger('create');
$this.trigger('create');
$("html").trigger('create');
});
});
Run Code Online (Sandbox Code Playgroud)
说明:
上面的代码位于名为hod.php的文件中.该文件包含许多复选框.这些复选框同时被检查,当我按下#at-site-btn按钮时,at-site.php出现(作为对话框)并显示每个选中的复选框.
这就是问题发生的地方.当我按下对话框中的后退按钮返回上一页并尝试取消选中这些复选框时,错误会弹出标题中提到的内容.在我的代码中没有调用'刷新方法'所以我没有看到修复它的方法.
致以最诚挚的问候,非常感谢您的回答.
我正在尝试为我的应用程序编写一个单元测试未被捕获的线程异常处理程序,但到目前为止还没有运气.处理程序是应用程序范围的,我知道它的工作原理.它也基于此处的代码.但是我想不出一种实际为它编写单元测试的方法,考虑到如果我从我的测试项目中抛出一个异常就输入了那个代码,但它永远不会返回.这导致我到目前为止所写的任何测试都失败了.
这是我的处理程序,有没有人有任何建议如何我可以单元测试这个?
public class MyApplication extends Application {
// uncaught exception handler variable
private final UncaughtExceptionHandler defaultUEH;
// handler listener
private final Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// Try restarting the application if an uncaught exception has
// occurred.
PendingIntent myActivity = PendingIntent
.getActivity(getApplicationContext(), 192837, new Intent(
getApplicationContext(), MainGui.class), PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager;
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 15000, myActivity);
System.exit(2);
// re-throw critical exception further to the os (important) …Run Code Online (Sandbox Code Playgroud) 我尝试了很多东西,但仍然没有结果.
所以我在UIViewController的子类中以编程方式创建了以下按钮:
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0.0, 0.0, 110.0, 40.0);
rightButton.titleLabel.font = [UIFont fontWithName:GAME_FONT_NAME_STRING size:20.0];
[rightButton setTitle:@"MyTitle" forState:UIControlStateNormal];
rightButton.backgroundColor = [UIColor clearColor];
[rightButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[rightButton setBackgroundImage:normalImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rightButton];
Run Code Online (Sandbox Code Playgroud)
选择器在哪里:
- (void)myButton;
Run Code Online (Sandbox Code Playgroud)
我尝试了一切:
- (void)myButton;
- (void)myButton:(id)sender;
- (void)myButton:(id)sender forEvent:(UIEvent *)event;
- (IBAction)myButton;
- (IBAction)myButton:(id)sender;
- (IBAction)myButton:(id)sender forEvent:(UIEvent *)event;
Run Code Online (Sandbox Code Playgroud)
和相应的选择器,当然:
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:forEvent:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton) …Run Code Online (Sandbox Code Playgroud) 我有一个使用 Cardboard SDK 构建的 Unity 场景,并导出为 Android 库。该库用于在 Android 应用程序上以纸板模式播放视频。它不是整个应用程序,而是其中的一部分。Android 应用程序的其余部分是使用 Kotlin 和 Java 构建的。
我已经实现了,所有功能都按预期工作,但是退出场景会使 android 崩溃。
我们尝试了各种方法来清除玩家首选项,甚至在关闭场景之前清除内存。但在安卓上总是崩溃。我有两部 Android 手机(Android 9 和 10)用于测试。
在 Android 应用程序中,我已经做到了,一旦应用程序崩溃,我就会尝试恢复。我的崩溃是一些lateinit var变量被破坏了。它们的值变为 null 并且恢复先前的活动会使其崩溃。因此,在退出 Unity 场景后,我将取消引用的变量加载回内存中,一切都会恢复正常。
注意:我尝试过使用
Application.Quit();unity,但它只是关闭了整个应用程序。另一方面,我只想关闭跑步场景
在统一中[我调用goBackandroid部分中的一个函数来关闭应用程序]:
public void GoToHome()
{
Pause();
Stop();
Resources.UnloadUnusedAssets();
PlayerPrefs.DeleteAll();
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("goBack");
}
Run Code Online (Sandbox Code Playgroud)
在应用程序中:
public void goBack()
{
UnityPlayer.currentActivity.finish();
finish();
loadDerereferencedVars();
}
Run Code Online (Sandbox Code Playgroud)
这在 android 9 上完美运行。在另一部装有 android 10 的手机上,关闭场景后,应用程序继续运行,但是出现一条消息
当我单击关闭应用程序时,该应用程序继续工作。
我检查了日志,null pointer dereference …
java android unity-game-engine uncaught-exception google-cardboard
经过几个小时的网络搜索后,我还没有找到解决我的问题的方法。所以我正在 React 和 Java Spring Boot 上构建一个全栈 CRUD 应用程序。到目前为止,我已经能够在 localhost://8080 上运行后端存储启动。当我在 localhost://3000 上运行前端 React 时出现问题 -> React 应用程序不断加载并且我看到\xe2\x80\x9cUncaught (承诺中) SyntaxError: Unexpected token < in JSON at 位置 0\xe2\x80\ x9d控制台中的错误。
\n应用程序.js:
\nimport React, { Component } from \'react\';\nimport logo from \'./logo.svg\';\nimport \'./App.css\';\n\nclass App extends Component {\nstate = {\nisLoading: true,\ngroups: []\n};\n\nasync componentDidMount() {\nconst response = await fetch(\'/api/groups\');\nconst body = await response.json();\nthis.setState({ groups: body, isLoading: false });\n} ->\nRun Code Online (Sandbox Code Playgroud)\n此处出现语法错误
\nrender() {\nconst {groups, isLoading} = this.state;\n\nif …Run Code Online (Sandbox Code Playgroud) 我有以下示例脚本来试验 Cypress 中的异常处理。但异常没有被捕获。我在这里缺少什么?
Cypress.on('uncaught:exception', (err, runnable) => {
Cypress.log("this is top-level exception hadling")
return false
})
context('Actions', () => {
it('sample_testing', () => {
cy.on('uncaught:exception', (err, runnable) => {
cy.log("this is test-level exception hadling")
return false
})
cy.get("#notfound", {timeout:1000})
})
})
Run Code Online (Sandbox Code Playgroud)
请注意,我的网页中没有 id notfound 的元素。
新的 Java 程序员经常遇到如下错误:
"error: unreported exception <XXX>; must be caught or declared to be thrown"
Run Code Online (Sandbox Code Playgroud)
其中 XXX 是某个异常类的名称。
请解释:
java ×4
android ×3
cypress ×1
delphi ×1
exception ×1
iphone ×1
junit ×1
reactjs ×1
refresh ×1
spring-boot ×1
try-except ×1
uibutton ×1
uicontrol ×1
unit-testing ×1