Search code examples
androidandroid-4.0-ice-cream-sandwichandroid-2.2-froyo

Troubles starting to develop on froyo


Doing an Apprenticeship, a company told me to develop an app for Jelly-bean, but suddenly now they want to port some of my work to froyo.

I wanted to start over but i can't even start, changing a TextView text drops me a nullPointerException:

package com.videopleyer22ics;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

    public class MainActivity extends Activity
    {
        TextView tv1;

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);

            tv1 = (TextView) findViewById(R.id.texto1);

            setContentView(R.layout.activity_main);

            tv1.setText("Text changed");
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }

}

Here's the Logcat dump

02-11 14:52:34.058: E/AndroidRuntime(510): FATAL EXCEPTION: main
02-11 14:52:34.058: E/AndroidRuntime(510): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.videopleyer22ics/com.videopleyer22ics.MainActivity}: java.lang.NullPointerException
02-11 14:52:34.058: E/AndroidRuntime(510):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-11 14:52:34.058: E/AndroidRuntime(510):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-11 14:52:34.058: E/AndroidRuntime(510):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-11 14:52:34.058: E/AndroidRuntime(510):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-11 14:52:34.058: E/AndroidRuntime(510):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 14:52:34.058: E/AndroidRuntime(510):  at android.os.Looper.loop(Looper.java:123)
02-11 14:52:34.058: E/AndroidRuntime(510):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-11 14:52:34.058: E/AndroidRuntime(510):  at java.lang.reflect.Method.invokeNative(Native Method)
02-11 14:52:34.058: E/AndroidRuntime(510):  at java.lang.reflect.Method.invoke(Method.java:521)
02-11 14:52:34.058: E/AndroidRuntime(510):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-11 14:52:34.058: E/AndroidRuntime(510):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-11 14:52:34.058: E/AndroidRuntime(510):  at dalvik.system.NativeStart.main(Native Method)
02-11 14:52:34.058: E/AndroidRuntime(510): Caused by: java.lang.NullPointerException
02-11 14:52:34.058: E/AndroidRuntime(510):  at com.videopleyer22ics.MainActivity.onCreate(MainActivity.java:54)
02-11 14:52:34.058: E/AndroidRuntime(510):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 14:52:34.058: E/AndroidRuntime(510):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-11 14:52:34.058: E/AndroidRuntime(510):  ... 11 more

I Just don't know what to do since it works fine on ICS, and i need help. Do you know a way to port an ICS proyect to froyo? I don't mind to do it again on scratch.


Solution

  • Swap the order of setContentView() and findViewById(). You are trying to find the TextView before the main layout has been inflated.