我是机器人的noob,当我尝试settext或setcolor或之前正在工作的任何东西时,我面临一个奇怪的异常
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageNoSeek = (SeekBar)findViewById(R.id.pageNoSeekBar);
pageNotv = (TextView)findViewById(R.id.pageNotv);
nextButton=(ImageButton)findViewById(R.id.leftButton);
backButton=(ImageButton)findViewById(R.id.rightButton);
mainText = (TextView)findViewById(R.id.mainText);
mainText.setTextColor(Color.RED);
}
如上面的代码所示,没有什么不寻常的......我在另一台机器上使用过相同的代码..如果我添加它,它会在setTextColor或setTextSize中出错..
> 03-20 07:55:17.552: E/AndroidRuntime(1718): FATAL EXCEPTION: main
03-20 07:55:17.552: E/AndroidRuntime(1718): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thedarkdimension.keyboardShortcut/com.thedarkdimension.keyboardShortcut.StoryViewController}: java.lang.NullPointerException
03-20 07:55:17.552: E/AndroidRuntime(1718): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-20 07:55:17.552: E/AndroidRuntime(1718): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-20 07:55:17.552: E/AndroidRuntime(1718): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-20 07:55:17.552: E/AndroidRuntime(1718): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-20 07:55:17.552: E/AndroidRuntime(1718): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 07:55:17.552: E/AndroidRuntime(1718): at android.os.Looper.loop(Looper.java:137)
03-20 07:55:17.552: E/AndroidRuntime(1718): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-20 07:55:17.552: E/AndroidRuntime(1718): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 07:55:17.552: E/AndroidRuntime(1718): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 07:55:17.552: E/AndroidRuntime(1718): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-20 07:55:17.552: E/AndroidRuntime(1718): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-20 07:55:17.552: E/AndroidRuntime(1718): at dalvik.system.NativeStart.main(Native Method)
03-20 07:55:17.552: E/AndroidRuntime(1718): Caused by: java.lang.NullPointerException
03-20 07:55:17.552: E/AndroidRuntime(1718): at com.thedarkdimension.keyboardShortcut.StoryViewController.getNextPage(StoryViewController.java:95)
03-20 07:55:17.552: E/AndroidRuntime(1718): at com.thedarkdimension.keyboardShortcut.StoryViewController.connectToDB(StoryViewController.java:84)
03-20 07:55:17.552: E/AndroidRuntime(1718): at com.thedarkdimension.keyboardShortcut.StoryViewController.onCreate(StoryViewController.java:47)
03-20 07:55:17.552: E/AndroidRuntime(1718): at android.app.Activity.performCreate(Activity.java:5104)
03-20 07:55:17.552: E/AndroidRuntime(1718): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-20 07:55:17.552: E/AndroidRuntime(1718): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-20 07:55:17.552: E/AndroidRuntime(1718): ... 11 more
我无法得到它..任何人都可以帮忙:)。
答案 0 :(得分:4)
从onCreate
的代码段开始,您似乎没有调用setContentView
。
因此,(X)findViewById(R.id.x);
的所有调用都返回null,因此当您尝试对变量进行操作时,您将获得空指针异常。 [mainText因为找不到视图而为空]。
<强>解决方案:强>
首先调用setContentView
,然后初始化变量并执行剩余的操作