我目前正在开发有关谷歌地图服务的应用程序。我使用带有 2 个选项卡的选项卡布局,这些选项卡是片段。在一个标签中,我使用谷歌地图 api,它工作正常。但是在第二个选项卡中,我希望在按钮上单击带有位置选择器的启动意图。单击它会出现 1 秒钟,然后隐藏。我不知道是什么问题,所以请帮忙。
这是代码:
主要活动:
public class MainActivity extends AppCompatActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
// fab.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
// }
// });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabDirectionsActivity tabDirectionsActivity = new TabDirectionsActivity();
return tabDirectionsActivity;
case 1:
TabPlacesActivity tabPlacesActivity = new TabPlacesActivity();
return tabPlacesActivity;
default:
return null;
}
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "DIRECTIONS";
case 1:
return "PLACES";
}
return null;
}
}
}
这是 TabPlaces 片段:
public class TabPlacesActivity extends Fragment {
TextView placeNameText;
TextView placeAddressText;
WebView attributionText;
Button getPlaceButton;
private final static int PLACE_PICKER_REQUEST = 1;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View placesView = inflater.inflate(R.layout.activity_places, container, false);
placeNameText = (TextView) placesView.findViewById(R.id.tv_PlaceName);
placeAddressText = (TextView) placesView.findViewById(R.id.tv_PlaceAddress);
attributionText = (WebView) placesView.findViewById(R.id.wv_Attribution);
getPlaceButton = (Button) placesView.findViewById(R.id.btn_GetPlace);
getPlaceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
Intent intent = builder.build(getActivity());
startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
return placesView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST){
if (resultCode == getActivity().RESULT_OK){
Place place = PlacePicker.getPlace(getActivity(),data);
placeNameText.setText(place.getName());
placeAddressText.setText(place.getAddress());
if (place.getAttributions() == null) {
attributionText.loadData("no attribution", "text/html; charset=utf-8", "UFT-8");
} else {
attributionText.loadData(place.getAttributions().toString(), "text/html; charset=utf-8", "UFT-8");
}
}
}
}
}
这是activity_places.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="rs.fon.mapapp.TabPlacesActivity"
android:gravity="top|center">
<Button
android:id="@+id/btn_GetPlace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Place" />
<TextView
android:id="@+id/tv_PlaceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="Place Name"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_PlaceAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Place Address"
android:textSize="16sp" />
<WebView
android:id="@+id/wv_Attribution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attribution"
android:textSize="16sp" />
</LinearLayout>