Android:从mapfragment调用活动

时间:2019-03-06 18:00:05

标签: android

我想用浮动按钮从我的地图片段中调用一个活动,但它不起作用,应用程序不断崩溃。

MapsActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

String json_string; //ici seront stockées les information concernant les établissements


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

public void getBar(View view){
    Log.v("myTag","button Clicked");
    Intent intent = new Intent(this, DisplayFilter.class);
    startActivity(intent);
}
}

DisplayFilter.java

public class DisplayFilter extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.filtreview);
    Log.v("myTag","vue affichée");
}
}

logcat显示此

the log

它讨论了android清单,但我认为我不应该对其进行编辑吗?

1 个答案:

答案 0 :(得分:1)

我猜您的问题来自此代码

public void getBar(View view){
  Log.v("myTag","button Clicked");
  Intent intent = new Intent(this, DisplayFilter.class); 
  //Intent intent = new Intent(getActivity(), DisplayFilter.class);
  startActivity(intent);
}

问题是您在Fragment中,因此您无法通过this,而必须使用getActivity()

您以某种方式需要context,因此您也可以致电view.getContext()

只要您想创建Activity,请执行以下步骤:

图片取自:this answer

您的清单应如下所示:

<application 
....
<activity1></activity1>
<activity  
         android:name=". DisplayFilter" />
...
</application>

enter image description here