当我的应用程序被长时间最小化时,Android NullPointerException

时间:2014-08-27 21:35:36

标签: android nullpointerexception

我的应用程序在NullPointerException很长时间内崩溃了。我认为问题在于片段的组织。

这是日志猫:

08-28 00:49:49.371: E/AndroidRuntime(30333): FATAL EXCEPTION: main
08-28 00:49:49.371: E/AndroidRuntime(30333): Process: com.dimchel.reserwacompras, PID: 30333
08-28 00:49:49.371: E/AndroidRuntime(30333): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dimchel.reserwacompras/com.dimchel.reserwacompras.fragments.MainActivity}: java.lang.NullPointerException
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.os.Handler.dispatchMessage(Handler.java:102)
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.os.Looper.loop(Looper.java:136)
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-28 00:49:49.371: E/AndroidRuntime(30333): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 00:49:49.371: E/AndroidRuntime(30333): at java.lang.reflect.Method.invoke(Method.java:515)
08-28 00:49:49.371: E/AndroidRuntime(30333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-28 00:49:49.371: E/AndroidRuntime(30333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-28 00:49:49.371: E/AndroidRuntime(30333): at dalvik.system.NativeStart.main(Native Method)
08-28 00:49:49.371: E/AndroidRuntime(30333): Caused by: java.lang.NullPointerException
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:656)
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.app.Activity.performStart(Activity.java:5241)
08-28 00:49:49.371: E/AndroidRuntime(30333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)=

一段MainActivity代码: 公共类MainActivity扩展了ActionBarActivity {     static MainActivity thisActivity = null;

private SiteFragment siteFragment;
public boolean siteCreate;

public String loc = null;
public String url;

public double latitude, longitude;
public boolean loadData = false;

public static ScrollView mainScroll;

public static int displayWidth, oneShit, secondShit;    

private AdvertisementFragment advFragment;
public boolean advCreate;

private ShoppingFragment shoppingFragment;
public boolean createShopping = false;

private LoginFragment loginFragment;
public boolean createCompras = false;

private ComprasFragment comprasFragment;
public boolean createLogin = false;

private AddFragment addFragment;
public boolean createAdd = false;   

public ArrayList<EditFragment> editFragments = new ArrayList<EditFragment>();
public ArrayList<ImageFragment> imageFragments = new ArrayList<ImageFragment>();

public boolean firstBoot = false;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    Log.v("gg", "create");

    setContentView(R.layout.activity_main);

    displayWidth = getWindowManager().getDefaultDisplay().getWidth();
    oneShit = (int) getResources().getDimension(R.dimen.paddingRight);
    secondShit = (int) getResources().getDimension(R.dimen.paddingStart);

    mainScroll = (ScrollView) findViewById(R.id.mainScroll);

    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    loc = Locale.getDefault().toString().substring(0, 2);
}

@SuppressWarnings("deprecation")
@Override
protected void onResume() 
{

    super.onResume();

    Log.v("gg", "resume");

    displayWidth = getWindowManager().getDefaultDisplay().getWidth();
    oneShit = (int) getResources().getDimension(R.dimen.paddingRight);
    secondShit = (int) getResources().getDimension(R.dimen.paddingStart);

    loc = Locale.getDefault().toString().substring(0, 2);
}

public void createAdvFragment()
{
    advFragment = new AdvertisementFragment();
    getSupportFragmentManager().beginTransaction()
            .add(R.id.startLayout, advFragment)
            .commit();

    advCreate = true;
}


public void deleteAdvFragment()
{
    getSupportFragmentManager() .beginTransaction()
            .remove(advFragment)
            .commit();

    advCreate = false;
}

public void showAdvFragment()
{
    getSupportFragmentManager() .beginTransaction()
            .show(advFragment)
            .commit();
}

public void hideAdvFragment()
{
    getSupportFragmentManager() .beginTransaction()
            .hide(advFragment)
            .commit();
}

public void refreshAdvFragment()
{
    if (advCreate)  advFragment.refresh(loc);
}

public void deleteSiteFragment()
{
    getSupportFragmentManager() .beginTransaction()
            .remove(siteFragment)
            .commit();

    siteCreate = false;
}

public void startApp()
{               
    showTabFragment();
    thisActivity = this;    

    FileManager.loadProfileToStorage(this);

    if (FileManager.loadCategoryFromStorage(this) != null)
        AppDataManager.category = FileManager.loadCategoryFromStorage(this);
    else
    {
        AppDataManager.category.add(new Category("Категории не загружены", -2));
    }
    if (FileManager.loadItemsFromStorage(this) != null)
    {       
        AppDataManager.items = FileManager.loadItemsFromStorage(this);
    }   

    FileManager.loadBootData(this);

    if (firstBoot)
    {
        Log.v("gg", "старт firstboot");
    }
    else
        Log.v("gg", "старт NOOOOOfirstboot");   

    if (AppDataManager.check_login)
    {
        createShoppingFragment();   
    }
    else
    {
        createLoginFragment();
    }       
}

public void createSiteFragment(String txt)
{
    siteFragment = new SiteFragment();

    siteFragment.barText = txt;

    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, siteFragment)
            .commit();

    siteCreate = true;
}

public void showStartFragment()
{
    FragmentManager fragmentManager = getSupportFragmentManager();
    StartFragment startFragment = (StartFragment) fragmentManager.findFragmentById(R.id.startFragment);
    getSupportFragmentManager().beginTransaction()
            .show(startFragment)
            .commit();
}

public void showSiteFragment(String txt)
{
    getSupportFragmentManager().beginTransaction()
            .show(siteFragment)
            .commit();    

    siteFragment.barText = txt;
    siteFragment.loadUrl();

    siteFragment.setBarText(txt);
}

public void hideStartFragment()
{       
    FragmentManager fragmentManager = getSupportFragmentManager();
    StartFragment startFragment = (StartFragment) fragmentManager.findFragmentById(R.id.startFragment);
    getSupportFragmentManager().beginTransaction()
            .hide(startFragment)
            .commit();
}

public void hideSiteFragment()
{
    getSupportFragmentManager().beginTransaction()
            .hide(siteFragment)
            .commit();
}

public void setTabEnabled(boolean shopping, boolean settings)
{
    FragmentManager fragmentManager = getSupportFragmentManager();
    TabFragment tabFragment = (TabFragment) fragmentManager.findFragmentById(R.id.tabFragment);
    tabFragment.setTabEnabled(shopping, settings);
}

public void hideStartFramgnet()
{
    FragmentManager fragmentManager = getSupportFragmentManager();
    StartFragment startFragment = (StartFragment) fragmentManager.findFragmentById(R.id.startFragment);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.hide(startFragment);
    ft.commit();                
}

public void createComprasFragment()
{
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();

    createCompras = true;
    comprasFragment = new ComprasFragment();

    fragmentTransaction.add(R.id.fragment_container, comprasFragment);
    fragmentTransaction.commit();
}

public void updateLoginFragment()
{
    loginFragment.refreshLoginFragment();
}

public void updateTabFragment(boolean settings, boolean reserva)
{
    FragmentManager fragmentManager = getSupportFragmentManager();
    TabFragment tabFragment = (TabFragment) fragmentManager.findFragmentById(R.id.tabFragment);
    tabFragment.updateTabFramgent(settings, reserva);
}

public void hideTabFragment()
{
    FragmentManager fragmentManager = getSupportFragmentManager();
    TabFragment tabFragment = (TabFragment) fragmentManager.findFragmentById(R.id.tabFragment);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.hide(tabFragment);
    ft.commit();
}

public void showTabFragment()
{
    FragmentManager fragmentManager = getSupportFragmentManager();
    TabFragment tabFragment = (TabFragment) fragmentManager.findFragmentById(R.id.tabFragment);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.show(tabFragment);
    ft.commit();
}

public void hideComprasFragment()
{
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.hide(comprasFragment);
    ft.commit();
}

public void showComprasFragment()
{
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.show(comprasFragment);
    ft.commit();
}

@Override
protected void onPause() 
{
    super.onPause();

    if (createAdd == true) removeAddFragment();
    FileManager.saveDataToStorage(this, AppDataManager.items, AppDataManager.category);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    int id = item.getItemId();
    if (id == R.id.action_settings) 
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment 
{

    public PlaceholderFragment() 
    {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
    {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

1 个答案:

答案 0 :(得分:0)

是的,您的问题是,您FragmentadvFragment的{​​{1}}长期存在,并且Activity被杀后(由于Android)回收内存或配置更改)。如果您使用空Fragment引用提交事务(例如.show(null).commit()),您将会崩溃。

相反,标记您要添加的片段(制作AD_FRAGMENT_TAG并使用.add(id, new AdvertisingFragment(), AD_FRAGMENT_TAG)),无论何时您想要使用它,请使用getSupportFragmentManager().findFragmentByTag(AD_FRAGMENT_TAG)查找并制作确保在与它进行交易之前它是非空的。