-2

可能重复:
启动活动时出现 NullpointerException

我现在得到这个 logcat !每次我收到此消息并强制关闭!我以前问过这个问题,但我得到一个新的错误!出!

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{yaAli.package313.hossein110/yaAli.package313.hossein110.know}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:301)
    at java.lang.Float.parseFloat(Float.java:291)
    at java.lang.Float.valueOf(Float.java:330)
    at yaAli.package313.hossein110.know.onCreate(know.java:67)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    ... 11 more

应用您的答案后,我得到了新的 logcat:

FATAL EXCEPTION: main
java.lang.NumberFormatException: unable to parse '' as integer
at java.lang.Integer.parseInt(Integer.java:412)
at java.lang.Integer.parseInt(Integer.java:382)
at java.lang.Integer.valueOf(Integer.java:682)
at yaAli.package313.hossein110.know$1.run(know.java:37)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

4

从您的代码:

String sizefa = sharedpreferences.getString("fontsizefa",null);
...
tvfa.setTextSize(1, Float.valueOf(sizefa).floatValue());

当您的首选项不包含“fontsizefa”时,您将其设置为空。抛出异常是因为稍后您尝试将空字符串转换为浮点数。

将其更改为:

String sizefa = sharedpreferences.getString("fontsizefa","0.0");

或任何您想要的默认大小。

colorfa当它不在您的偏好中时,您也会遇到同样的问题。

于 2012-10-05T17:18:21.353 回答