SmashCode
SmashCode

Reputation: 4257

findResourceById() returning null

I've read a few questions and answers regarding this topic, but none of them applied to my issue.

My problem is in the following code:

 public class Activity2 extends Activity {

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alt);

}


public void buttonPress(View v){
    TextView tv1 = (TextView) findViewById(R.id.text1);
    TextView tv2 = (TextView) findViewById(R.id.text2);
    Button b1 = (Button) findViewById(R.id.button1);
    if((tv1 != null) & (tv2 != null) & (b1 != null)){
        tv1.setText(R.string.changed);
        tv2.setText(R.string.changed);
        b1.setText(R.string.changed);
    }
    else
        Toast.makeText(getApplicationContext(), "VIEW ELEMENTS ARE NOT BEING ASSIGNED", Toast.LENGTH_SHORT).show();
}

alt.xml looks like this

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:name="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/stuff" />

<TextView
    android:name="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/stuff2" />

<Button
    android:name="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/buttonText"
    android:onClick="buttonPress" />        

The IDs are being generated in R.id, but they aren't being assigned to my two TextView elements and my one Button element. (I'm seeing the toast popup every time)

Does anyone have any idea?

Upvotes: 0

Views: 646

Answers (2)

ChristopheCVB
ChristopheCVB

Reputation: 7315

Try replacing

android:name="@+id/text1"

By

android:id="@+id/text1"

Upvotes: 1

bschultz
bschultz

Reputation: 4254

Instead of android:name="..." use android:id="@+id/..."

Upvotes: 7

Related Questions