Nikunj
Nikunj

Reputation: 123

how to get static string initialize a textview when activity starts up?

i'm trying to initialize my textview using a static string whenever i launch this activity but emulator always force closes the activity.. can anyone help me out?

Here's the code:

// ServiceAct.java
public class ServiceAct extends Activity
{
    static String[] phoneno = {"No Number", "No Number", "No Number"};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // to set the contacts value in status module
        TextView v1 = (TextView) findViewById(R.id.stat_con1);
        v1.setText(phoneno[0]);

        //remaining code of the activity
        ....
    }
}

here's the logcat

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.service/com.example.service.ServiceAct}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.service.ServiceAct.onCreate(ServiceAct.java:31)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

here's the layout code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:weightSum="1"  android:layout_height="fill_parent"  android:orientation="horizontal"  android:layout_width="fill_parent"  android:background="@color/background" >

    <RelativeLayout android:layout_height="match_parent"  android:layout_width="match_parent"  android:layout_gravity="center"  android:id="@+id/relativeLayout2">

        <TextView android:text="No Number"  android:id="@+id/stat_con1"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>

</LinearLayout>

Upvotes: 1

Views: 2234

Answers (2)

Pedro Loureiro
Pedro Loureiro

Reputation: 11514

From the code you posted, I can see two possible errors:

  • TextView v1 is null, which means the layout file doesn't have any TextView with the id R.id.stat_con1
  • the view with the id R.id.stat_con1 is not a TextView and you fail when casting to a TextView

Update:

Caused by: java.lang.NullPointerException
at com.example.service.ServiceAct.onCreate(ServiceAct.java:31)

Check line 31... something in that line is null.

Upvotes: 1

Boris Strandjev
Boris Strandjev

Reputation: 46963

Have you tried to clean your project? This looks just like such kind of error (because everything else seems perfectly fine). If you develop in Eclipse go to project -> clean. Select all projects. Afterwords you might need to also rebuild. Also if this does nto help, try restarting your IDE.

Upvotes: 1

Related Questions