Estou criando uma Activity
em Android, cujo código está aqui, e estou obtendo um NullPointerException
próximo da linha
final TextView txtResult = new TextView(this);
Obtive o seguinte dump do stack:
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
Será que tem algo relacionado com meu método onCreate
?
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);
}
});
}
final
significa que você está declarando uma constante ao invés de uma variável, logo só poderá atribuir o valor àtxtResult
em sua criação. Sobre o NPE, você tem TextView chamadotxtResult
no seu xml?final TextView txtResult = new TextView(ATuaActivity.this);
br.com.colorcalc.Calculator
é suaActivity
, correto? O<init>
significa que o código está sendo executado no construtor. Você está mesmo fazendo tudo isso dentro do construtor daActivity
ou eu entendi algo errado? Se estiver criando as views dentro do construtor, o conceito está errado, você deveria criar as views (manualmente, ou através do métodosetContentView
) dentro doonCreate
daActivity
, ou outro lugar parecido.