Question

when i try to set the text of an textview i get a nullpointerexception. somehow the findviewbyid method return null but i dont know why.

this is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<RelativeLayout
    android:id="@+id/rl_ros_layout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@+id/tv_ros_view1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ProgressBar
        android:id="@+id/pb_ros_bar1"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:visibility="invisible" />
</RelativeLayout>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/rl_ros_layout1" >

    <TableLayout
        android:id="@+id/tl_rostertable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:stretchColumns="2" >

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="3dp"
                android:text="@string/team" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="3dp"
                android:text="@string/pos" />

            <TextView
                android:layout_height="wrap_content"
                android:padding="3dp"
                android:text="@string/name" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:padding="3dp"
                android:text="@string/score" />
        </TableRow>
    </TableLayout>
</ScrollView>
</RelativeLayout>

and my oncreate method

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.roster);
    final Bundle extra = getIntent().getExtras();
    position = extra.getInt("pos");
    int id = de.damps.fantasy.HomeActivity.ID[position];
    url = de.damps.fantasy.HomeActivity.URL + "/roster/2011/" + id;
    tbl = (TableLayout) findViewById(R.id.tl_rostertable);
    team = (TextView) findViewById(R.id.tv_ros_view1);
    String  s= de.damps.fantasy.HomeActivity.TEAMS[position];
    team.setText(s);

    new GetRoster().execute(url);
}

the tablelayout is found without any problmes. but the textview is null. i already cleaned the project but it didnt solve the problem

would be nice if someone can help me

Was it helpful?

Solution

You have a typo:

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:id="@+id/tv_ros_view1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

(android:text instead of android:id)

OTHER TIPS

You wrote

 android:text="@+id/tv_ros_view1"

instead of

android:id

you create ID your text view like this android:id="@+id/tv_ros_view1" in place of android:text="@+id/tv_ros_view1" this is use full to you.

In you xml layout:

android:text="@+id/tv_ros_view1"

should be:

android:id="@+id/tv_ros_view1"

the text attribute is used to tell textview what text to show.

In your faulty/first text view you have written like this:

         android:text="@+id/tv_ros_view1"

Just replace this with this:

         android:id="@+id/tv_ros_view1"

This is how you have to assign id to a view(TextView).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top