简体   繁体   English

从一个片段移动到另一个片段时,应用程序崩溃

[英]application crashes when I move from one fragment to another

I have four fragments in my application. 我的申请中有四个片段。 the first one gets GPS data for calculating speed which works fine. 第一个获取GPS数据以计算速度,效果很好。 as soon as the application gets the gps data and move to other fragments it crashes. 一旦应用程序获取gps数据并移动到其他片段,它就会崩溃。 FYI, other fragments are all without any code. 仅供参考,其他片段都没有任何代码。

here is my MainActivity class: 这是我的MainActivity类:

 package ir.helpx.speedx; import android.Manifest; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.os.Build; import android.support.design.widget.TabLayout; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.WindowManager; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Toolbar toolbar; TabLayout tabLayout; ViewPager viewPager; ViewPagerAdapter viewPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar =(Toolbar)findViewById(R.id.toolBar); setSupportActionBar(toolbar); tabLayout = (TabLayout) findViewById(R.id.tabLayout); viewPager = (ViewPager) findViewById(R.id.viewPager); viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); viewPagerAdapter.addFragments(new HomeFragment(), "Home"); viewPagerAdapter.addFragments(new CompassFragment(), "Compass"); viewPagerAdapter.addFragments(new HUDFragment(), "HUD"); viewPager.setAdapter(viewPagerAdapter); tabLayout.setupWithViewPager(viewPager); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1); } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this,"GPS permission granted", Toast.LENGTH_LONG).show(); // get Location from your device by some method or code } else { // show user that permission was denied. inactive the location based feature or force user to close the app } break; } } } 

and my MainActivity XML: 和我的MainActivity XML:

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context="ir.helpx.speedx.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="368dp" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="8dp" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <include android:layout_height="wrap_content" android:layout_width="match_parent" layout="@layout/toolbar_layout" /> <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tabLayout" app:tabMode="fixed" app:tabGravity="fill" ></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/viewPager" ></android.support.v4.view.ViewPager> </android.support.design.widget.AppBarLayout> </android.support.constraint.ConstraintLayout> 

and here is my first Fragment called Home: 这是我的第一个片段,称为Home:

 package ir.helpx.speedx; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; /** * A simple {@link Fragment} subclass. */ public class HomeFragment extends Fragment implements LocationListener{ public HomeFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment LocationManager mgr; mgr = (LocationManager)getContext().getSystemService(getActivity().LOCATION_SERVICE); mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); this.onLocationChanged(null); return inflater.inflate(R.layout.fragment_home, container, false); } //TextView msg1; @Override public void onLocationChanged(Location location) { if (location==null){ TextView currentSpeed = null; } else { float nCurrentSpeed = location.getSpeed(); TextView currentSpeed = (TextView) getView().findViewById(R.id.speed); currentSpeed.setText((int)nCurrentSpeed*18/5+""); //msg1=currentSpeed; Toast.makeText(getActivity(), "Location Found!", Toast.LENGTH_LONG).show(); } } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } } 
and here is the related XML 这是相关的XML

 <FrameLayout 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" tools:context="ir.helpx.speedx.HomeFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/speed" android:gravity="center" android:textSize="180sp" android:textStyle="bold" android:textColor="@android:color/white" android:id="@+id/speed"/> </FrameLayout> 

and here is my second Fragment: 这是我的第二个片段:

 package ir.helpx.speedx; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * A simple {@link Fragment} subclass. */ public class HUDFragment extends Fragment { public HUDFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_hud, container, false); } } 

rest of the fragments are similar to 其余片段类似于

I have a toolbar XML: 我有一个工具栏XML:

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minWidth="?attr/actionBarSize" android:fitsSystemWindows="true" android:id="@+id/toolBar" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" > </android.support.v7.widget.Toolbar> 
and finally my my ViewPagerAdapter: 最后是我的ViewPagerAdapter:

 package ir.helpx.speedx; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import java.util.ArrayList; /** * Created by abe on 5/29/2017. */ public class ViewPagerAdapter extends FragmentPagerAdapter { ArrayList<Fragment> fragments = new ArrayList<>(); ArrayList<String> tabTitles = new ArrayList<>(); public void addFragments(Fragment fragments, String titles){ this.fragments.add(fragments); this.tabTitles.add(titles); } public ViewPagerAdapter(FragmentManager fm){ super(fm); } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } @Override public CharSequence getPageTitle(int position) { return tabTitles.get(position); } } 

Please HELP me! 请帮我! thank you 谢谢

I think I could fix it by adding 我想我可以通过添加来修复它

 viewPager.setOffscreenPageLimit(4); 

to my MainActivity. 到我的MainActivity。 I think that was because by default ViewPager retains only one page in the view hierarchy in an idle state. 我认为那是因为默认情况下,ViewPager在空闲状态下仅保留视图层次结构中的一页。 Please tell me if I did the right thing! 请告诉我我是否做对了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我从一个片段滑动到另一个片段时,我的应用程序崩溃(错误:canScrollHorizo​​ntally()&#39; 在空对象引用上) - my app crashes when I swipe from one fragment to another (error: canScrollHorizontally()' on a null object reference) 如何从一个片段移动到另一个片段? - How to move from one fragment to another fragment? 当我将代码从一个应用程序移动到另一个应用程序时,用于编辑Google电子表格的Android应用程序崩溃 - Android app to edit google spread sheet crashes when I moved code from one application to another 如何从一个片段移动到另一个片段 - How to move from one fragment to another 片段通过滑动以及单击按钮从一个片段移动到另一个片段 - Fragment move from one Fragment to another by sliding as well as button click 当我按侧边栏导航上的菜单移动到另一个片段时,突然我的应用程序关闭 - when I pressed the menu on the sidebar navigation to move to another fragment, suddenly my application force closes 当我将物品从一个袋子搬到另一个袋子时出现ObjectDeletedException - ObjectDeletedException when i move items from one bag to another 如何在android中将一个片段移动到另一个片段 - How to move one fragment to another fragment in android 从一个视图(Java类)移动到另一个视图时,我的应用程序崩溃 - My application crashes when moving from one view(Java Class) to another 当我尝试从片段切换到活动时,应用崩溃 - App crashes when I try to switch from fragment to activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM