我的第一个应用程序显示了承诺,但现在“意外停止”。我已将其减少到最小值以显示失败,这似乎是在执行SetContentView时发生的。
问题:
a)这里有一个明显的错误吗? b)我应该如何调试这个失败?
谢谢!
package com.xxx.try1;
import android.app.Activity;
// sundry imports
public class Chrisgeturltry1Activity extends Activity {
private TextView lblDATE = new TextView(this);
private ViewGroup mainPanel;
//
public void onCreate(Bundle istate) {
super.onCreate(istate);
// create the panel to enclose everything
mainPanel = makeForm();
// show the panel on the screen
setContentView(mainPanel);
}
//------------------------------------------------------------------------------------------------------------
// create the form and establish onClick method for the button
private ViewGroup makeForm() {
LinearLayout panel = new LinearLayout(this);
panel.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
panel.setOrientation(LinearLayout.VERTICAL);
panel.setBackgroundColor( -0xDD99DD);
lblDATE.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
lblDATE.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
lblDATE.setBackgroundColor(Color.WHITE);
lblDATE.setTextColor(Color.BLACK);
lblDATE.setTypeface(Typeface.MONOSPACE,2);
lblDATE.setText("date");
return panel;
}
}
错误日志: 好的,这是来自LogCat的错误:
09-07 16:02:59.037: ERROR/AndroidRuntime(492): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.chrisdev.geturltry1/com.chrisdev.geturltry1.Chrisgeturltry1Activity}: java.lang.NullPointerException
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.os.Looper.loop(Looper.java:123)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.app.ActivityThread.main(ActivityThread.java:3647)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at java.lang.reflect.Method.invoke(Method.java:507)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at dalvik.system.NativeStart.main(Native Method)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): Caused by: java.lang.NullPointerException
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.view.View.(View.java:1874)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.view.View.(View.java:1921)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.widget.TextView.(TextView.java:344)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.widget.TextView.(TextView.java:337)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.widget.TextView.(TextView.java:332)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at com.chrisdev.geturltry1.Chrisgeturltry1Activity.(Chrisgeturltry1Activity.java:28)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at java.lang.Class.newInstanceImpl(Native Method)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at java.lang.Class.newInstance(Class.java:1409)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536)
09-07 16:02:59.037: ERROR/AndroidRuntime(492): ... 11 more
答案 0 :(得分:0)
问题在于,就我所见,lblDATE为null。而是在onCreate中实例化它。
答案 1 :(得分:0)
当查看像这样的LogCat错误时,我开始寻找直接引用我的代码的任何行。在这种情况下,那是com.chrisdev.geturltry1.Chrisgeturltry1Activity.(Chrisgeturltry1Activity.java:28)
您可以双击该行直接跳转到代码中的某个点(假设您正在使用Eclipse)并从那里进行调试。
编辑:Stefan可能是对的 - 尝试在onCreate或makeForm方法中实例化lblDATE,而不是在你声明它的地方,看看是否有帮助。