My first app was showing promise but now "stops unexpectedly". I have reduced it to the minimum to show the failure, which appears to happen when SetContentView is executed.
Questions:
a) is there an obvious mistake here? b) how should I go about debugging this failure?
Thanks!
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;
}
}
Error log: OK, here are the errors from LogCat:
09-07 16:02:59.037: ERROR/AndroidRuntime(492): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.chrisdev.geturltry1/com.chrisdev.geturltry1.Chrisgeturltry1开发者_JAVA百科Activity}: 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
When looking through LogCat errors like this, I start looking for any lines that refer to my code directly. In this case, that's com.chrisdev.geturltry1.Chrisgeturltry1Activity.(Chrisgeturltry1Activity.java:28)
You can double-click the line to jump straight to the point in your code (assuming you're using Eclipse) and debug from there.
Edit: Stefan is probably right - try instantiating lblDATE in onCreate or in your makeForm method instead of where you've declared it and see if that helps.
The issue is that lblDATE is null as far as I can see. Instantiate it in onCreate instead.
精彩评论