NullPointerException when creating a View on Android

0

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);
                    }
                });
        }
    
asked by anonymous 07.05.2014 / 15:44

2 answers

2

At line 40 of posted code

  

btnCalc.setOnClickListener (new View.OnClickListener ()

You are using btnCalc , but it has not been assigned before. Before using btnCalc something is missing like

btnCalc = (Button)findViewById(R.id.btnCalc);

One more detail, to convert an integer x to its hexadecimal notation, you can simply do this:

Integer.toHexString(x)
    
07.05.2014 / 16:52
1

Final - For declaration of constants. Home Now in question good practice, this line of your code.
final TextView txtResult = new TextView(this); Home In my case I keep good practices and do the following:

 - I declare all interface objects that I will use, in a global way (just below the public class).  -
 - In this way: private TextView txtResult;  - Within the onCreate () method I reference the variable txtResult as follows:

txtResult = (TextView) findViewById(R.id.ID_DO_OBJETO);

So in this way you can use txtResult in various parts of the code.

Another good practice I use is the following, in XML of the layout, in the button code, I add:

android:onClick="METODO"

obs: within onClick="" the method must be written without (). This way it is not necessary to do what you did, which in the case was:

btnCalc.setOnClickListener (new View.OnClickListener()  {   
    public void onClick(View v){
        result = "#" + rst1 + remainderR + rst2 + remainderG + rst3 + remainderB;
        txtResult.setText(result);  
    }
});
    
07.05.2014 / 16:10