地图片段上的按钮?

时间:2018-01-10 12:30:12

标签: android xml android-fragments

我通过xml在片段Map中定义了一个简单的按钮,如何在java中捕获“id”引用,findViewById(R.id.followed)不起作用! 这也是一个好方法吗?有更好的方法吗?

    <fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="16dp"
    android:layout_marginTop="8dp"
    android:id="@+id/map"
    tools:context=".ProfileActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment">

<Button
    android:id="@+id/followed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|left"
    android:text="Followed"
    android:padding="10dp"
    android:layout_marginTop="20dp"
    android:paddingRight="10dp"/>
</fragment>

我尝试以这种方式在MainActivity.class中捕获引用:

> protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    followed = (Button)findViewById(R.id.followed)

1 个答案:

答案 0 :(得分:-1)

我只是重新启动项目解决了我的问题!这可能是一个更新问题。

相关问题