Incident of capping Up (setDisplayHomeAsUpEnabled) in fragment



  • The fragment code:

    public class sovety_Fragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_sovety, null);
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // 28 строка
            getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
        return v;
    }
    

    }

    Erroneous transition:

    01-06 18:43:31.668 9656-9656/com.whitestar.topcar E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NullPointerException
    at com.whitestar.topcar.sovety_Fragment.onCreateView(sovety_Fragment.java:28)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

    activate code:

    package com.whitestar.topcar;

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    import android.widget.ExpandableListView;

    public class MainActivity extends ActionBarActivity implements ExpandableListView.OnChildClickListener, View.OnClickListener {
    private static FragmentManager mManager;
    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

        button1 = (Button) findViewById(R.id.button1);
        mManager = getSupportFragmentManager();
    
       /* 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();
            }
        });*/
    
       // button1.setOnClickListener(this);
            button1.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                Fragment fragment = null;
    
    
                    mManager.findFragmentById(R.id.fragmentContainer);
                    if (fragment == null) {
                        fragment = new sovety_Fragment();
                        mManager.beginTransaction()
                                .add(R.id.fragmentContainer, fragment)
                                .replace(R.id.fragmentContainer, fragment)
                                .addToBackStack(null)
                                .commit();
                    }}
    
    
    });
    }
    
    
    
    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);
    }
    
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    
        return false;
    }
    
    @Override
    public void onClick(View v) {
    
    }
    

    }



  • You use the sappport lib, so you need to getSupportActionBar instead of getActionBar.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2