Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

( Toolbar ) findViewById( R.id.tool_bar ) return NULL

Yesterday I encountered a problem - findViewById( ) return NULL for my toolbar. I was looking all around through the interner but it seems I can't find the solution for my "BIG" problem :D

Here's the styles.xml

<resources>
    <style name = "AppTheme" parent = "@style/Theme.AppCompat.NoActionBar">
        <!-- TODO: Create AppTheme -->
        <item name="android:windowActionBar">false</item>
    </style>
</resources>

Here's the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
    xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:tools = "http://schemas.android.com/tools"
    android:id = "@+id/drawer_layout"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    tools:context = ".Main" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width = "fill_parent"
        android:layout_height = "fill_parent" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/tool_bar"
            android:layout_width = "fill_parent"
            android:layout_height = "@dimen/toolbar_height"
            android:background = "@mipmap/bg_toolbar" >

            <ImageView
                android:id = "@+id/toolbar_drawer_button"
                android:clickable="true"
                android:layout_width = "wrap_content"
                android:layout_height = "@dimen/toolbar_height"
                android:src = "@mipmap/ic_drawer" />

            <TextView
                android:id = "@+id/toolbar_title"
                android:layout_width = "wrap_content"
                android:layout_height = "wrap_content"
                android:layout_gravity = "center"
                android:text = "@string/app_name"
                android:textColor = "@color/toolbar_text"
                android:textSize = "@dimen/toolbar_text_size"
                android:textStyle = "bold|italic" />

            <ImageView
                android:id = "@+id/toolbar_settings"
                android:clickable="true"
                android:layout_width = "wrap_content"
                android:layout_height = "@dimen/toolbar_height"
                android:layout_gravity = "right"
                android:background = "@android:color/transparent"
                android:padding = "8dp"
                android:src = "@mipmap/ic_settings" />
        </android.support.v7.widget.Toolbar>

        <FrameLayout
            android:id = "@+id/view_container"
            android:background="@mipmap/bg_main"
            android:layout_width = "match_parent"
            android:layout_height = "match_parent" />

    </LinearLayout>


    <ListView
        android:id = "@+id/drawer"
        android:layout_width = "@dimen/drawer_width"
        android:layout_height = "match_parent"
        android:layout_gravity = "left"
        android:gravity = "center_vertical"
        android:background = "@mipmap/bg_drawer" />

</android.support.v4.widget.DrawerLayout>

And here's the Main.java:

public class Main extends AppCompatActivity
{
    private DrawerAdapter           drawerAdapter;
    private String[]                drawerTitles;
    private DrawerLayout            drawerLayout;
    private ArrayList< DrawerItem > drawerItems;
    private TypedArray              drawerIcons;
    private ListView                drawerList;
    private Toolbar                 toolbar;
    private ImageView               drawerButton;
    private LayoutInflater          lInflater;
    private FrameLayout             view_container;
    private Converter               converter;

    @Override protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        lInflater = ( LayoutInflater ) getApplicationContext( ).getSystemService( LAYOUT_INFLATER_SERVICE );

        /** Identifying views **/
        view_container = ( FrameLayout ) findViewById( R.id.view_container );
        drawerLayout = ( DrawerLayout ) findViewById( R.id.drawer_layout );
        drawerList = ( ListView ) findViewById( R.id.drawer );
        drawerItems = new ArrayList<>( );

        toolbar = ( Toolbar ) drawerLayout.findViewById( R.id.tool_bar );

        /** Setting toolbar as action bar **/
        SetupToolbar( );

        /** Getting resources **/
        drawerTitles = getResources( ).getStringArray( R.array.drawer_categories );
        drawerIcons = getResources( ).obtainTypedArray( R.array.drawer_icons );

        /** Filling drawer with items ( rows ) **/
        for( int i = 0; i < drawerTitles.length; i++ )
            drawerItems.add( new DrawerItem( drawerTitles[ i ], drawerIcons.getResourceId( i, -1 ) ) );

        /** Clearing icon array **/
        drawerIcons.recycle( );

        /** Setting drawer adapter **/
        drawerAdapter = new DrawerAdapter( getApplicationContext( ), drawerItems );
        drawerList.setAdapter( drawerAdapter );

        /** Making drawer items selectable **/
        drawerList.setOnItemClickListener( new DrawerItemClickListener( ) );
    }

    private void SetupToolbar( )
    {
        toolbar.bringToFront( );

        if( toolbar != null )
        {
            setSupportActionBar( toolbar );
            setTitle( null );

            drawerButton = ( ImageView ) findViewById( R.id.toolbar_drawer_button );

            if( drawerButton != null )
            {
                drawerButton.setOnClickListener( new View.OnClickListener( )
                {
                    @Override public void onClick( View v )
                    {
                        if( drawerLayout.isDrawerOpen( drawerList ) )
                            drawerLayout.closeDrawer( drawerList );

                        else
                            drawerLayout.openDrawer( drawerList );
                    }
                } );
            }

        }
    }

    public class DrawerItemClickListener implements AdapterView.OnItemClickListener
    {
        @Override public void onItemClick( AdapterView< ? > parent, View view, int position, long id )
        {
            view_container.removeAllViews( );

            switch( position )
            {
                case 0:
                    lInflater.inflate( R.layout.fragment_distance, view_container );
                    break;

                case 1:
                    lInflater.inflate( R.layout.fragment_volume, view_container );
                    break;

                default:
                    Toast.makeText( getApplicationContext( ), "NOT YET READY !!!", Toast.LENGTH_SHORT ).show( );
                    break;
            }

            drawerLayout.closeDrawer( drawerList );
        }
    }

}

I've tried lots of diffrent things but none of them seemed to work. Thanks in advance! : )

like image 633
Sw3das Avatar asked Aug 24 '15 06:08

Sw3das


1 Answers

My Android Studio, for some reason, started to default to a CustomView that would not allow me to edit the layout. It starts up with with a theme that includes a toolbar and mail icon in the bottom right. mainAcitivty.java is then looking for that toolbar and fab, which is no longer there.

in mainAcitivty.java REPLACE:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    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();
        }
    });
}

WITH this

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
like image 132
elite clientele Avatar answered Oct 31 '22 04:10

elite clientele