Android (工具栏)findViewById(R.id.tool_bar)返回NULL
昨天我遇到了一个问题——我的工具栏的findviewbyd()返回NULL。 我在网上到处找,但似乎找不到解决我“大”问题的办法:D 下面是styles.xmlAndroid (工具栏)findViewById(R.id.tool_bar)返回NULL,android,android-toolbar,findviewbyid,Android,Android Toolbar,Findviewbyid,昨天我遇到了一个问题——我的工具栏的findviewbyd()返回NULL。 我在网上到处找,但似乎找不到解决我“大”问题的办法:D 下面是styles.xml <resources> <style name = "AppTheme" parent = "@style/Theme.AppCompat.NoActionBar"> <!-- TODO: Create AppTheme --> <item name="a
<resources>
<style name = "AppTheme" parent = "@style/Theme.AppCompat.NoActionBar">
<!-- TODO: Create AppTheme -->
<item name="android:windowActionBar">false</item>
</style>
</resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
假的
下面是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>
下面是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 );
}
}
}
public类主活动
{
私人抽屉机;
私有字符串[]抽屉;
私人抽屉布局;
私有ArrayListDroperItems;
私用抽屉;
私有列表视图抽屉列表;
专用工具栏;
私有图像查看抽屉按钮;
私人停车场;
私有框架布局视图\容器;
专用变频器;
@在创建时覆盖受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lInflater=(LayoutInflater)getApplicationContext().getSystemService(布局\充气机\服务);
/**识别视图**/
view\u container=(FrameLayout)findViewById(R.id.view\u container);
抽屉布局=(抽屉布局)findViewById(R.id.抽屉布局);
抽屉列表=(列表视图)findViewById(R.id.drawer);
drawerItems=newarraylist();
工具栏=(工具栏)抽屉布局.findViewById(R.id.tool\u栏);
/**将工具栏设置为操作栏**/
设置工具栏();
/**获取资源**/
drawerTitles=getResources().getStringArray(R.array.drawer\u categories);
drawerIcons=getResources().obtainTypedArray(R.array.drawer\u图标);
/**用物品(行)填充抽屉**/
for(int i=0;i父级、视图、整型位置、长id)
{
view_container.removeallview();
开关(位置)
{
案例0:
充气(右布局、碎片距离、视图、容器);
打破
案例1:
充气(右布局、碎片体积、视图、容器);
打破
违约:
Toast.makeText(getApplicationContext(),“尚未准备好!!!”,Toast.LENGTH\u SHORT.show();
打破
}
抽屉布局。封闭抽屉(抽屉列表);
}
}
}
我试过很多不同的方法,但似乎都不管用。
提前感谢!:) 您必须将样式放入样式文件夹中 styles.xml
<resources>
<style name = "AppTheme" parent = "@style/Theme.AppCompat.NoActionBar">
<!-- TODO: Create AppTheme -->
<item name="android:windowActionBar">false</item>
</style>
</resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
v21文件夹中的styles.xml
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
<item name="android:windowBackground">@color/windowBackground</item>
<item name="android:navigationBarColor">@color/navigationBarColor</item>
</style>
真的
真的
真的
@android:过渡/移动
@android:过渡/移动
@颜色/文本颜色主
@颜色/窗口背景
@颜色/导航栏颜色
您要在Avivity中导入哪个工具栏?它必须是android.support.v7.widget.Toolbar
此外,你可以尝试改变
toolbar=(toolbar)抽屉布局.findViewById(R.id.tool\u bar)代码>到
toolbar = ( Toolbar ) findViewById( R.id.tool_bar );
更改toolbar=(toolbar)抽屉布局.findViewById(R.id.tool\u bar)代码>进入工具栏=(工具栏)findViewById(R.id.tool\u栏)代码>好吧,伙计们,不知道怎么做,但是整个事情都被删除了工具栏。bringToFront()代码>
谢谢大家的回答 出于某种原因,我的Android Studio开始默认为CustomView,不允许我编辑布局。它以一个主题开始,该主题包括右下角的工具栏和邮件图标。java正在寻找那个工具栏和fab,它已经不存在了
在MainActivity.java中,替换:
@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();
}
});
}
用这个
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
您的工具栏
是一个安卓.support.v7.widget.Toolbar
?@sw3d,因为还没有任何帮助?工具栏背景可能有问题?您是否在没有该参数的情况下运行应用程序?此外,现在似乎没有人使用fill\u parent,请改用match\u parent。填充父项(在API级别8及更高版本中重命名为匹配父项),尚未完成。他甚至没有试过