Android (FrameLayout)findViewById-返回空值?

Android (FrameLayout)findViewById-返回空值?,android,android-layout,android-framelayout,Android,Android Layout,Android Framelayout,为什么我总是(FrameLayout)findViewById总是返回null? 该文件已存在。 使用谷歌地图。 我读了相关的话题。 技术支持: 项目清洁。 删除文件夹gen protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.travel_map_activity); mapView = (

为什么我总是(FrameLayout)findViewById总是返回null? 该文件已存在。 使用谷歌地图。 我读了相关的话题。 技术支持: 项目清洁。 删除文件夹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

}
有什么问题吗

<?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>

旅游地图活动.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>


谢谢

您正在同一布局中找到的两个视图(
travelMapView
mapFrame
)是
travel\u map\u activity
)。因为您提供的布局根本不包含
travelMapView
,但是您在那里没有得到
NullPointerException

编辑:如您现在所示,
mapFrame
不在同一视图中,因此无法访问该视图

嗯,有一个,但它不会有用,因为
setContentView
一次只显示一个布局。无论如何,您仍然可以使用膨胀的布局访问
mapFrame
,而不实际使用它(这是因为它完全没有用处):


使用此选项可以使您摆脱该空值,但是如果该框架布局不在当前视图中,您就不可能使用它。

您正在从
travel\u map\u activity.xml
(仅包含一个ID:
R.ID.travelMapView
)中扩展布局。您不能从其他布局中选择项目(有一些小的例外情况更像是系统的滥用)


您要么需要将内容视图设置为包含
R.id.mapFrame
的任何布局,要么需要找到另一种方法来传入该数据(可能使用
捆绑包
.putExtra()
.getIntExtra()打包在一起)
,例如;有和涵盖了这类事情)。

这是
旅行地图活动.xml
?我觉得不是这样,因为在post中添加了.travel\u map\u activity.xml上面的行中引用了
R.id.travelMapView
。我没有显示只有在NullPointerException FrameLayout时才会弹出的所有代码。请检查Eric的答案和我的编辑。请仔细阅读这两个答案。你的回答不好
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);