标签: runtimeexception

Java:已检查vs未经检查的异常说明

我已经在StackOverFlow上阅读了有关已检查和未经检查的异常的多个帖子.老实说,我还是不太确定如何正确使用它们.

Joshua Bloch在" Effective Java "中说过

对可恢复条件使用已检查的异常,对编程错误使用运行时异常(第2版中的第58项)

让我们看看我是否正确理解这一点.

以下是我对已检查异常的理解:

try{
    String userInput = //read in user input
    Long id = Long.parseLong(userInput);
}catch(NumberFormatException e){
    id = 0; //recover the situation by setting the id to 0
}
Run Code Online (Sandbox Code Playgroud)

1.以上是否考虑了检查异常?

2. RuntimeException是未经检查的异常吗?

以下是我对未经检查的异常的理解:

try{
    File file = new File("my/file/path");
    FileInputStream fis = new FileInputStream(file);   
}catch(FileNotFoundException e){

//3. What should I do here?
    //Should I "throw new FileNotFoundException("File not found");"?
    //Should I log?
    //Or should I System.exit(0);?
}
Run Code Online (Sandbox Code Playgroud)

4.现在,上述代码也不能成为检查异常吗?我可以尝试恢复这样的情况吗?我可以吗?(注意:我的第3个问题在catch上面)

try{
    String …
Run Code Online (Sandbox Code Playgroud)

java exception runtimeexception checked-exceptions unchecked-exception

678
推荐指数
11
解决办法
31万
查看次数

Android 1.6:"android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序"

我正在尝试打开一个对话框窗口,但每次我尝试打开它都会抛出此异常:

Uncaught handler: thread main exiting due to uncaught exception
android.view.WindowManager$BadTokenException: 
     Unable to add window -- token null is not for an application
  at android.view.ViewRoot.setView(ViewRoot.java:460)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
  at android.app.Dialog.show(Dialog.java:238)
  at android.app.Activity.showDialog(Activity.java:2413)
Run Code Online (Sandbox Code Playgroud)

我是通过showDialog显示器的id 调用来创建的.该onCreateDialog处理器记录良好,我可以通过它没有问题的一步,但因为它看起来像我想的东西我已经把它贴吧:

@Override
public Dialog onCreateDialog(int id)
{
    Dialog dialog;
    Context appContext = this.getApplicationContext();
    switch(id)
    {
        case RENAME_DIALOG_ID:
            Log.i("Edit", "Creating rename dialog...");
            dialog = new Dialog(appContext);
            dialog.setContentView(R.layout.rename);
            dialog.setTitle("Rename " + noteName);
            break;
        default:
            dialog = null;
            break;
    }
    return dialog;      
}
Run Code Online (Sandbox Code Playgroud)

这有什么不足之处吗?有些问题在创建对话框时已经讨论过这个问题onCreate …

android runtimeexception android-dialog android-windowmanager

301
推荐指数
8
解决办法
19万
查看次数

NameError:全局名称'xrange'未在Python 3中定义

我在运行python程序时遇到错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 110, in <module>
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 27, in __init__
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\class\inventory.py", line 17, in __init__
builtins.NameError: global name 'xrange' is not defined
Run Code Online (Sandbox Code Playgroud)

游戏来自这里.

是什么导致这个错误?

python range xrange python-3.x runtimeexception

253
推荐指数
5
解决办法
31万
查看次数

从Activity外部调用startActivity()?

我正在使用一个AlarmManager触发广播信号的意图.以下是我的代码:

AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, Wakeup.class);
try
{
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
    Long elapsed +=  // sleep time;
    mgr.set(AlarmManager.RTC_WAKEUP, elapsed, pi);
}
catch(Exception r)
{
    Log.v(TAG, "RunTimeException: " + r);
}
Run Code Online (Sandbox Code Playgroud)

我从一个调用这个代码Activity,所以我不知道如何得到以下错误...

ERROR/AndroidRuntime(7557): java.lang.RuntimeException: Unable to start receiver com.wcc.Wakeup: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Run Code Online (Sandbox Code Playgroud)

android alarmmanager runtimeexception android-pendingintent

181
推荐指数
5
解决办法
18万
查看次数

Android'无法添加窗口 - 令牌null不适用于应用程序'异常

当我尝试打开对话框时,我收到以下Android异常.有人可以帮我理解发生了什么,我该如何解决这个问题?

android.view.WindowManager$BadTokenException: 
  Unable to add window -- token null is not for an application
    at android.view.ViewRoot.setView(ViewRoot.java:509)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    at android.app.Dialog.show(Dialog.java:241)
Run Code Online (Sandbox Code Playgroud)

android runtimeexception android-dialog android-windowmanager

134
推荐指数
4
解决办法
13万
查看次数

ActionBarCompat:java.lang.IllegalStateException:您需要使用Theme.AppCompat

我得到在Android 2.3.5一个RuntimeException,但我正在用Theme.AppCompat(RES /价值/的themes.xml).这是电话:http://www.gsmarena.com/samsung_galaxy_y_s5360-4117.php

 <!-- res/values/themes.xml -->
 <?xml version="1.0" encoding="utf-8"?>
 <resources>

     <style name="Theme.Styled" parent="@style/Theme.AppCompat">
         <item name="actionBarStyle">@style/QueryActionBar</item>
         <item name="android:actionBarStyle">@style/QueryActionBar</item>
     </style>

     <style name="QueryActionBar" parent="@style/Widget.AppCompat.ActionBar">
         <item name="background">@color/blueback</item>
         <item name="android:background">@color/blueback</item>
         <item name="backgroundSplit">@color/blueback</item>
         <item name="android:backgroundSplit">@color/blueback</item>
     </style>

 </resources>
Run Code Online (Sandbox Code Playgroud)

这是values-v11的文件.

 <!-- res/values-v11/themes.xml -->
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
    <style name="QueryTheme" parent="@android:style/Theme.Holo">
    <!-- Any customizations for your app running on devices with Theme.Holo here -->
    </style>
 </resources>
Run Code Online (Sandbox Code Playgroud)

这是错误.

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.txt2lrn.www/com.txt2lrn.www.LandingActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with …
Run Code Online (Sandbox Code Playgroud)

android runtimeexception android-actionbar

102
推荐指数
6
解决办法
16万
查看次数

无法实例化android.gms.maps.MapFragment

我试着用谷歌地图android v2做一个非常简单的活动的演示,只需从谷歌页面复制代码:https: //developers.google.com/maps/documentation/android/start#adding_the_api_key_to_your_application

活动:

package com.example.mapdemo;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
Run Code Online (Sandbox Code Playgroud)

布局:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.MapFragment"/>
Run Code Online (Sandbox Code Playgroud)

我已经根据页面申请了api密钥并修改了我的androidmanifest.xml文件,就像这样:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wenhai.driverschool"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.INTERNET" />

    <!-- add for map2 -->
    <permission
        android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!-- External storage for caching. -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Maps API needs …
Run Code Online (Sandbox Code Playgroud)

android noclassdeffounderror runtimeexception

81
推荐指数
6
解决办法
11万
查看次数

错误:注册表项'Software\JavaSoft\Java Runtime Environment'\ CurrentVersion'?

当我运行:C:\ Users\ashahria\Downloads> java -jar schemaSpy_5.0.0.jar

我收到以下错误.怎么了?我该如何解决?

错误:注册表项'Software\JavaSoft\Java Runtime Environment'\ CurrentVersion'

值为'1.5',但需要'1.7'.
错误:找不到java.dll
错误:找不到Java SE Runtime Environment.

java environment exception runtimeexception

80
推荐指数
5
解决办法
26万
查看次数

无法在未调用Looper.prepare()的线程内创建处理程序

我有一个活动,并且我有一个班级.

text=new Dynamictext(...);
text.setText("txt");
Run Code Online (Sandbox Code Playgroud)

在我的DynamicText java中,我有这样的代码:

public void setText(String text) {
    this.text=text;
    new asyncCreateText().execute();
    //this.createText(text);
}

//private Handler handler = new Handler();

private class asyncCreateText extends AsyncTask<Void, Void, Void> 
{
    @Override
    protected Void doInBackground(Void... unused) {
        return null;
    }

    @Override
    protected void onPostExecute(Void unused) {

    }
}
Run Code Online (Sandbox Code Playgroud)

我明白了:

ERROR/AndroidRuntime(5176):引起:java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序

我该如何处理这个错误?

ERROR/AndroidRuntime(5370): java.lang.ExceptionInInitializerError
ERROR/AndroidRuntime(5370):     at com.l.start.DynamicText.setText(DynamicText.java:125)
ERROR/AndroidRuntime(5370):     at com.l.start.OpenGLRenderer.initfonts(OpenGLRenderer.java:168)
ERROR/AndroidRuntime(5370):     at com.l.start.OpenGLRenderer.init(OpenGLRenderer.java:119)
ERROR/AndroidRuntime(5370):     at com.l.start.OpenGLRenderer.onSurfaceChanged(OpenGLRenderer.java:90)
ERROR/AndroidRuntime(5370):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1120)
ERROR/AndroidRuntime(5370):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:975)

ERROR/AndroidRuntime(5370): Caused by: java.lang.RuntimeException: 
    Can't create handler inside thread that …
Run Code Online (Sandbox Code Playgroud)

android runtimeexception

79
推荐指数
5
解决办法
22万
查看次数

加载字体时,"RuntimeException:无法生成本机字体"

我试图在Android上使用自定义字体用于TextView,遵循此处的指南.使用相同的字体,相同的代码,相同的一切,我在adb logcat中得到这个:

W/dalvikvm(  317): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(  317): FATAL EXCEPTION: main
E/AndroidRuntime(  317): java.lang.RuntimeException: Unable to start activity  ComponentInfo{org.evilx.quacklock/org.evilx.quacklock.MainActivity}:             java.lang.RuntimeException: native typeface cannot be made
E/AndroidRuntime(  317):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(  317):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  317):        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  317):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  317):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  317):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  317):        at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  317):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  317):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  317):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  317):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  317):        at …
Run Code Online (Sandbox Code Playgroud)

java android android-fonts runtimeexception

64
推荐指数
6
解决办法
2万
查看次数