我正在尝试制作一个应用,当用户点击ListView中的项目时,会显示GoogleMaps.
我试过以下代码:
ShowPlacesActivity.java
public class ShowPlaceActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_place);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container_showplaces, new PlaceholderFragment())
.commit();
}
getActionBar().setSubtitle(getString(R.string.activity_showplaces_subtitle));
}
}
Run Code Online (Sandbox Code Playgroud)
activity_show_place.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container_showplaces"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="com.krupal.rememberthisplace.ShowPlaceActivity" tools:ignore="MergeRootFrame" />
Run Code Online (Sandbox Code Playgroud)
fragment_show_place.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/listView_places"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
fragment_maps.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/mymap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我想在listview的项目上显示地图点击...所以,我已经实现了两个片段,如下所示:
在PlaceholderFragment.java中,我设置onitemclicklistener为:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle …Run Code Online (Sandbox Code Playgroud) xml android listview android-layout google-maps-android-api-2
我制作了一个应用程序,其中我在我的位置显示了带有GPS位置标记的Google地图.此地图位于第一个选项卡上,我还有另外两个选项卡,如下图所示
现在我面临一个问题,即当我从选项卡1移动到选项卡2然后再转到选项卡1时它完美地工作,但是当我移动到选项卡3然后移回选项卡1或选项卡2时,应用程序崩溃并给出以下内容例外情况LogCat:
01-27 18:35:26.190 9414-9414/com.example.accurat.faisal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.accurat.faisal, PID: 9414
android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at com.example.accurat.faisal.MyLocation.onCreateView(MyLocation.java:62)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2184)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1969)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:620)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:273)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at …Run Code Online (Sandbox Code Playgroud) android google-maps inflate-exception google-maps-android-api-2 android-tablayout
我正在尝试用活动做一些事情,但是在一个片段中.我所做的是使用活动:
首先停止旋转设备时重新启动活动
android:configChanges="keyboardHidden|orientation|screenSize"
在我的活动中添加:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
}
Run Code Online (Sandbox Code Playgroud)
因此,获取活动不会重新启动,而是重新加载main.xml,以使用layout-land
现在我有一个显示viewpager的活动,其中包含三个片段.一切正常.旋转的检测在碎片中
public class FRG_map_web extends Fragment {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("myLogs", "Rotation");
}
Run Code Online (Sandbox Code Playgroud)
问题是片段不使用setContentView(R.layout.main); 这是代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frg.myFragment, null);
Run Code Online (Sandbox Code Playgroud)
我试着用:
LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.frg.myFragment, null);
...
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.frg.myFragment, null);
...
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
view = inflater.inflate(R.layout.frg.myFragment, …Run Code Online (Sandbox Code Playgroud) 我试图找出原因:
getSupportFragmentManager().beginTransaction().commit();
Run Code Online (Sandbox Code Playgroud)
失败了
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
Run Code Online (Sandbox Code Playgroud)
在一个非常基本的FragmentActivity类中.
所以这是我的用例(这将是一些伪代码,而不是一个完整的例子,抱歉):我有一个FragmentActivity与内部AsyncTask类.大概是这样的:
public class HelloWorld extends FragmentActivity {
showFragment(Fragment fragment, String name) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, fragment, name).commit();
}
private class SlowFragmentShow extends AsyncTask<Context, String, Void> {
protected Void doInBackground(Context... contexts) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
/* meh */
}
}
protected void onPostExecute(Void nothing) {
showFragment(new MyFragment(), "myFragment");
}
}
}
Run Code Online (Sandbox Code Playgroud)
或者基本上,在启动应用程序10秒后,它将显示另一个片段.听起来很简单吧?这似乎也很有效,直到我决定旋转手机.当我这样做时,应用程序将在使用"无法执行此操作..."调用"getSupportFragmentManager()..."时崩溃.
android android-asynctask illegalstateexception android-fragmentactivity android-support-library
两天了,我正试图解决这个问题.我在网上和stackoverflow上搜索过,但我找不到我的问题的答案.
在我的应用程序中,我正在尝试实现两个选项卡.一个需要显示一个地图,在另一个选项卡中我将显示一个列表适配器.(使用ActionBarSherlock)
当我单击第二个选项卡然后单击第一个选项卡时,我得到一个例外:
10-08 01:27:45.203:E/AndroidRuntime(7082):java.lang.IllegalStateException:活动已被破坏
这是我的活动代码和我的片段类.(仅用于测试,我将CustomMapFragment类放两次).
public class MapActivity extends SherlockFragmentActivity implements LocationListener,TabListener{
LatLng mLocation;
GoogleMap map;
//CustomMapFragment fragment1 = new CustomMapFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//FragmentManager fm = getSupportFragmentManager();
if (savedInstanceState == null) {
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
setActionBar();
//fragment2 = new Fragment2();
//FragmentTransaction ft = fm.beginTransaction();
//ft.add(R.id.container, fragment1, "frag1");
//ft.add(R.id.fragment_container, fragment2, "frag2");
//ft.commit();
}
}
private void setActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
CustomMapFragment frag1 = new CustomMapFragment();
CustomMapFragment frag2 = …Run Code Online (Sandbox Code Playgroud) 我开发了一个应用程序,它使用片段作为片段的一部分。
public class LoginFragment extends Fragment
EditTextWithCameraButtonFrag userNameFrag;
EditTextWithCameraButtonFrag passwordFrag;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
userNameFrag = (EditTextWithCameraButtonFrag)getChildFragmentManager()
.findFragmentById(R.id.fragment_username);
passwordFrag = (EditTextWithCameraButtonFrag) getChildFragmentManager()
.findFragmentById(R.id.fragment_password);
Run Code Online (Sandbox Code Playgroud)
EditTextWithCameraButtonFrag是 Fragment 的子类型。这段代码就像一个魅力,在 android 5.1.1 上运行。但是,在 Android 4.4.2 上运行它会返回 userNameFrage 和 passwordFrag 为 null。所以看起来,返回的 Child FragmentManager 找不到这些字段。
与4.4.3一起使用时有什么需要注意的吗getChildFragmentManager()?
提前致谢!
编辑:这是主要片段的 XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/huge"
android:orientation="vertical" >
<TextView
android:id="@+id/login_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:gravity="center"
android:textColor="@color/white"
android:textSize="40dp" />
<fragment
android:id="@+id/fragment_username"
android:name="com.example.app.fragments.EditTextWithCameraButtonFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment_password"
android:name="com.example.app.fragments.EditTextWithCameraButtonFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" …Run Code Online (Sandbox Code Playgroud) android android-fragments android-4.4-kitkat child-fragment android-5.1.1-lollipop
我正在开发一个新的Android项目.
第一个活动是使用滑块菜单和片段.在第一个片段上有一个列表视图(PrimaryFragmentDormir.java).选择其中一行后,将启动一个新活动.最后一个活动使用三个选项卡,以显示有关所选行对象的不同信息.listview是从远程JSON文件加载的.第一个选项卡显示有关所选对象的详细信息,第二个选项卡应显示地图.
我已按照官方文档设置了在我的应用中包含Google地图所需的所有内容,至少我是这么认为的.当用户选择第二个选项卡时,会显示一个空白屏幕,左下方有Google徽标.
这是Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.solinpromex.elpasojuarezexperience" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<uses-library android:name="com.google.android.maps"/>
<application
android:name=".app.AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="..hidden here..." />
<activity
android:name=".Inicio"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".Detalle_Hotel"
android:label="@string/title_activity_detalle__hotel" >
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
这是托管标签的主要活动:
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; …Run Code Online (Sandbox Code Playgroud) 应用程序加载和EventFragment.java显示地图的视图.当您单击另一个选项卡片段时,它会正常加载,当您返回到第一个选项卡(地图)时,它会崩溃并出现以下错误:
07-03 14:13:54.640: E/AndroidRuntime(11565): FATAL EXCEPTION: main
07-03 14:13:54.640: E/AndroidRuntime(11565): android.view.InflateException: Binary XML file line #5: Error inflating class fragment
07-03 14:13:54.640: E/AndroidRuntime(11565): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
07-03 14:13:54.640: E/AndroidRuntime(11565): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
07-03 14:13:54.640: E/AndroidRuntime(11565): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
07-03 14:13:54.640: E/AndroidRuntime(11565): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-03 14:13:54.640: E/AndroidRuntime(11565): at co.uk.thesomewhere.townpearlfest.EventFragment.onCreateView(EventFragment.java:48)
07-03 14:13:54.640: E/AndroidRuntime(11565): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-03 14:13:54.640: E/AndroidRuntime(11565): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-03 14:13:54.640: E/AndroidRuntime(11565): at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264)
07-03 14:13:54.640: E/AndroidRuntime(11565): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
07-03 14:13:54.640: E/AndroidRuntime(11565): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-03 14:13:54.640: E/AndroidRuntime(11565): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
07-03 14:13:54.640: E/AndroidRuntime(11565): …Run Code Online (Sandbox Code Playgroud) 我正在尝试将一个片段插入另一个片段,并且我已经成功做到这一点,直到我第一次吃完主要片段为止,但是当我尝试重新加载片段时,应用崩溃了,我有这个错误:
Caused by: java.lang.IllegalArgumentException: Binary XML file line #26:
Duplicate id 0x7f0e00e2, tag null, or parent id 0xffffffff with another
fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
Run Code Online (Sandbox Code Playgroud)
这是我的片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.source_destination,container, false);
PlaceAutocompleteFragment sourcr_autocompleteFragment = (PlaceAutocompleteFragment)
getActivity().getFragmentManager()
.findFragmentById(R.id.sourcr_autocomplete_fragment);
return rootView;
// List item
}
Run Code Online (Sandbox Code Playgroud)
这是我的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dp"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_medium"
android:layout_marginBottom="@dimen/margin_medium">
<fragment
android:id="@+id/sourcr_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 我有一个导航抽屉,其中Map是其他项目中的一个选项.当我点击我的地图时,它有效.如果我再次转到其他导航抽屉项目并返回地图,它会崩溃.谁能告诉我原因?
这是代码..
public class Map extends Fragment {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.map, container, false);
setUpMapIfNeeded();
return view;
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
@Override
public void onPause() {
super.onPause();
setUpMapIfNeeded();
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
FragmentManager fm = getFragmentManager();
mMap = ((SupportMapFragment) fm.findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
} …Run Code Online (Sandbox Code Playgroud) 我正在设计我的应用程序具有向后兼容性,所以我已经实现了支持和设计库,以使用导航抽屉和其他与新的Android版本相关的效果.
导航抽屉管理总共5个片段.其中一个片段包含一张地图:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="300dp"
class="com.google.android.gms.maps.MapFragment" />
Run Code Online (Sandbox Code Playgroud)
-
public class LocationFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_location, container, false);
/*Try to obtain the map from the SupportMapFragment*/
mMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
Run Code Online (Sandbox Code Playgroud)
这就是我如何从mainActivity上的导航抽屉管理片段:
public class MainActivity extends AppCompatActivity implements DrawerLayout.DrawerListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
displayFragment(menuItem.getItemId());
drawerLayout.closeDrawers();
return true;
} …Run Code Online (Sandbox Code Playgroud)