简体   繁体   中英

(FrameLayout) findViewById - returns null?

Why do I always (FrameLayout) findViewById always returns null? The file exists. Working with Google Maps. I read the related topics. Powered by: Project-Clean. Delete folder gen.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.travel_map_activity);
    mapView = (MapView) findViewById(R.id.travelMapView);

    FrameLayout mainLayout = (FrameLayout) findViewById(R.id.mapFrame);//null

}

What's the problem?

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapFrame"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/tvTop"
    android:layout_width="fill_parent"
    android:layout_height="20dip"
    android:background="#FFFFFF"
    android:gravity="top|center_horizontal"
    android:text="FLOATING VIEW - TOP"
    android:textColor="#000000"
    android:textStyle="bold" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lvMain"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout></FrameLayout>

travel_map_activity.xml

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

<com.google.android.maps.MapView
    android:id="@+id/travelMapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="key" />

</LinearLayout>

thanks

Are those two Views you are finding ( travelMapView and mapFrame ) in the same layout travel_map_activity . Because the layout you provided isn't holding travelMapView at all, yet you don't get a NullPointerException there.

EDIT : As you show right now, mapFrame is not in the same view, so there is no way to get to that...

Well , there is one, but it won't be useful, as setContentView only shows up one single layout at a time. Anyway, you could still access mapFrame with an inflated layout, without it being actually used (and that's because it is completely useless ):

LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.your_other_layout_with_framelayout, null );
FrameLayout mainLayout = (FrameLayout) view.findViewById(R.id.mapFrame); 

Using this will let you escape from that null, but again , there's no chance you can use that FrameLayout if it isn't in your current view.

You're inflating the layout from travel_map_activity.xml , which only contains one ID: R.id.travelMapView . You cannot select items from other layouts ( with a few small exceptions that are more like abuses of the system ).

You either need to set the content view to whichever layout it is that contains R.id.mapFrame , or you need to find another way to pass that data in (probably using a Bundle packaged with the Intent with .putExtra() and .getIntExtra() , for example; there are other questions and blogs that cover this kind of thing).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM