I'm creating a Activity
on Android, whose code is here , and I'm getting a NullPointerException
near the line
final TextView txtResult = new TextView(this);
I got the following stack dump:
05-07 09:12:57.180: E/AndroidRuntime(887): FATAL EXCEPTION: main
05-07 09:12:57.180: E/AndroidRuntime(887): Process: br.com.colorcalc, PID: 887
05-07 09:12:57.180: E/AndroidRuntime(887): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.com.colorcalc/br.com.colorcalc.Calculator}: java.lang.NullPointerException
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.ActivityThread.access$700(ActivityThread.java:135)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.os.Looper.loop(Looper.java:137)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.ActivityThread.main(ActivityThread.java:4998)
05-07 09:12:57.180: E/AndroidRuntime(887): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 09:12:57.180: E/AndroidRuntime(887): at java.lang.reflect.Method.invoke(Method.java:515)
05-07 09:12:57.180: E/AndroidRuntime(887): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-07 09:12:57.180: E/AndroidRuntime(887): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-07 09:12:57.180: E/AndroidRuntime(887): at dalvik.system.NativeStart.main(Native Method)
05-07 09:12:57.180: E/AndroidRuntime(887): Caused by: java.lang.NullPointerException
05-07 09:12:57.180: E/AndroidRuntime(887): at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.view.View.<init>(View.java:3429)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.view.View.<init>(View.java:3496)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.widget.TextView.<init>(TextView.java:622)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.widget.TextView.<init>(TextView.java:617)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.widget.TextView.<init>(TextView.java:613)
05-07 09:12:57.180: E/AndroidRuntime(887): at br.com.colorcalc.Calculator.<init>(Calculator.java:26)
05-07 09:12:57.180: E/AndroidRuntime(887): at java.lang.Class.newInstanceImpl(Native Method)
05-07 09:12:57.180: E/AndroidRuntime(887): at java.lang.Class.newInstance(Class.java:1208)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
05-07 09:12:57.180: E/AndroidRuntime(887): ... 11 more
Does it have anything to do with my onCreate
method?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtResult = (TextView) findViewById(R.id.txtResult);
btnCalc.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
result = "#" + rst1 + remainderR + rst2 + remainderG + rst3 + remainderB;
txtResult.setText(result);
}
});
}