jeudi 28 mai 2015

How to show warning/error if used resource id belongs to another activity layout

Is there a way to show some warning/error when Android resource id does not belong to layout inflated in Activity?

Following is simple example, but in more complex layouts it is much harder to track down such issues as they can only be found during run-time. I would like to be able to catch them at compile-time.

MainActivity initialization

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // how to prevent erroneous usage of R.id.text2 at compile time
    TextView t = (TextView) findViewById(R.id.text2);
}

activity_main.xml

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
                xmlns:tools="http://ift.tt/LrGmb4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity">

    <TextView android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

activity_second.xml

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
                xmlns:tools="http://ift.tt/LrGmb4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".SecondActivity">

    <TextView android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

Aucun commentaire:

Enregistrer un commentaire