Android - findViewById (R.id.list) returns null

I have a class that is called when my application starts.

public class MainActivity extends Activity implements NetworkEvent.

In this situation

list = (ListView) findViewById(R.id.list);

works great. However, if I then name the new intention with:

   String[] names = object.names();

Intent myIntent = new Intent (MainActivity.this, SimpleList.class); myIntent.putExtra ("names", names); startActivityForResult (myIntent, 0);

where SimpleList is defined as:

public class SimpleList extends ListActivity implements NetworkEvent

then when i call

   list=(ListView) findViewById(R.id.list);

Log.i ("MyApp", "List:" + list);

from the SimpleList class, null: (

How did it happen? Both classes are in the same package.

Thank.

+5
source share
5 answers

, , ListView XML <ListView android:id="@android:id/list", <ListView android:id="@+id/list". ListActivity, getListView() .
: http://developer.android.com/reference/android/app/ListActivity.html

+16

, setContentView(R.whatever), - .

+9

findViewById LinearLayout.

:

xmlns:android="http://schemas.android.com/apk/res/android"

LinearLayout.

+1

"http://schemas.android.com/apk/res/android"

listView, :

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

  <TextView 
    android:text="test"
    android:layout_width="fill_parent" 
    android:layout_height="40dp">
  </TextView>

  <ListView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">     
  </ListView>  

</LinearLayout>
0

I just want to mention what my problem was. In my XML, I assigned android:name="@+id/bob"instead android:id="@+id/bob", but due to @ + id, it still registered it as an intellisensable field.

0
source

All Articles