找不到符号 class 操作栏和工具栏 [Android]
Cannot find symbol class actionbar and toolbar [Android]
我正在制作一个使用 Navigation Drawer 的应用程序。在 运行 应用程序上,出现名为 cannot find symbol class actionbar and toolbar
的错误。我不确定我的 Java 代码或 XML 是否有问题。请帮我解决这个问题。
MainPageActivity.java
public class MainPageActivity extends Activity {
//slider_menu için *******************************************************///////////
private DrawerLayout mDrawerLayout;
/////////////////************************
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainpage);
///// ********************** toolbar -*************************
mDrawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// set item as selected to persist highlight
menuItem.setChecked(true);
// close drawer when item is tapped
mDrawerLayout.closeDrawers();
// Add code here to update the UI based on the item selected
// For example, swap UI fragments here
return true;
}
});
/// **************************** Listen open and close slider menu button ******************************
mDrawerLayout.addDrawerListener(
new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Respond when the drawer's position changes
}
@Override
public void onDrawerOpened(View drawerView) {
// Respond when the drawer is opened
}
@Override
public void onDrawerClosed(View drawerView) {
// Respond when the drawer is closed
}
@Override
public void onDrawerStateChanged(int newState) {
// Respond when the drawer motion state changes
}
}
);
//**********************************************////////////////////
ListView list;
final String[] web = {
getString(R.string.mainpage_acil),
getString(R.string.mainpage_bildirim),
getString(R.string.mainpage_isguveligi),
};
Integer[] imageId = {
R.drawable.mainpage_acil_icon_p,
R.drawable.mainpage_bildirim_icon_p,
R.drawable.mainpage_isguvenligi_icon_p,
};
CustomListMainPage adapter = new
CustomListMainPage(MainPageActivity.this, web, imageId);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Toast.makeText(MainPageActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
Log.v("Pozisyon", String.valueOf(position));
Intent intentPos;
ImageView imageView= (ImageView) view.findViewById(R.id.img);
boolean flag=false;
/// Renk değiştirme buraya tekrar bak***************
switch (position) {
case 0 :
//Toast.makeText(MainPageActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.mainpage_acil_icon_p);
intentPos=new Intent(MainPageActivity.this,EmergencyActivity.class);
startActivity(intentPos);
break;
case 1 :
imageView.setImageResource(R.drawable.mainpage_bildirim_icon_p);
intentPos=new Intent(MainPageActivity.this,NotificationPage.class);
startActivity(intentPos); break;
case 2 :
imageView.setImageResource(R.drawable.mainpage_isguvenligi_icon_p);
intentPos=new Intent(MainPageActivity.this,JobSecurityActivity.class);
startActivity(intentPos);
break;
default :
System.out.println("Hatali secim! 1, 2 ya da 3'e basiniz.");
break;
}
}
});
}
//slider_menu buton ayarları için *******************************************************///////////
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
//slider_menu buton ayarları için
*******************************************************///////////
}
}
activity_mainpage.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:menu="@menu/menu_main"
app:headerLayout="@layout/nav_header"
android:orientation="vertical"
android:background="#000000">
!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Toolbar için ***********************************************-->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<!-- Toolbar için *****************************************-->
<!-- Container for contents of drawer - use NavigationView to make configuration easier -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="60"
android:alpha=".85"
android:background="@drawable/mainpage_background"
android:layout_marginBottom="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="40"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EMAS"
android:textAlignment="center"
android:textColor="@color/colorWhite"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:layout_weight="20">
<include layout="@layout/mainpage_inner_part" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
我不确定我的 Java 代码或 XML 是否有问题。请帮我解决这个问题。
检查您的 android 清单文件中的应用程序标签中的样式,如下所示 ..
检查 android 清单文件中的这一行 ..
change this
public class MainPageActivity 扩展 AppCompatActivity
android:theme="@style/AppTheme"
然后应用下面的样式。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
我正在制作一个使用 Navigation Drawer 的应用程序。在 运行 应用程序上,出现名为 cannot find symbol class actionbar and toolbar
的错误。我不确定我的 Java 代码或 XML 是否有问题。请帮我解决这个问题。
MainPageActivity.java
public class MainPageActivity extends Activity {
//slider_menu için *******************************************************///////////
private DrawerLayout mDrawerLayout;
/////////////////************************
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainpage);
///// ********************** toolbar -*************************
mDrawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// set item as selected to persist highlight
menuItem.setChecked(true);
// close drawer when item is tapped
mDrawerLayout.closeDrawers();
// Add code here to update the UI based on the item selected
// For example, swap UI fragments here
return true;
}
});
/// **************************** Listen open and close slider menu button ******************************
mDrawerLayout.addDrawerListener(
new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Respond when the drawer's position changes
}
@Override
public void onDrawerOpened(View drawerView) {
// Respond when the drawer is opened
}
@Override
public void onDrawerClosed(View drawerView) {
// Respond when the drawer is closed
}
@Override
public void onDrawerStateChanged(int newState) {
// Respond when the drawer motion state changes
}
}
);
//**********************************************////////////////////
ListView list;
final String[] web = {
getString(R.string.mainpage_acil),
getString(R.string.mainpage_bildirim),
getString(R.string.mainpage_isguveligi),
};
Integer[] imageId = {
R.drawable.mainpage_acil_icon_p,
R.drawable.mainpage_bildirim_icon_p,
R.drawable.mainpage_isguvenligi_icon_p,
};
CustomListMainPage adapter = new
CustomListMainPage(MainPageActivity.this, web, imageId);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Toast.makeText(MainPageActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
Log.v("Pozisyon", String.valueOf(position));
Intent intentPos;
ImageView imageView= (ImageView) view.findViewById(R.id.img);
boolean flag=false;
/// Renk değiştirme buraya tekrar bak***************
switch (position) {
case 0 :
//Toast.makeText(MainPageActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.mainpage_acil_icon_p);
intentPos=new Intent(MainPageActivity.this,EmergencyActivity.class);
startActivity(intentPos);
break;
case 1 :
imageView.setImageResource(R.drawable.mainpage_bildirim_icon_p);
intentPos=new Intent(MainPageActivity.this,NotificationPage.class);
startActivity(intentPos); break;
case 2 :
imageView.setImageResource(R.drawable.mainpage_isguvenligi_icon_p);
intentPos=new Intent(MainPageActivity.this,JobSecurityActivity.class);
startActivity(intentPos);
break;
default :
System.out.println("Hatali secim! 1, 2 ya da 3'e basiniz.");
break;
}
}
});
}
//slider_menu buton ayarları için *******************************************************///////////
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
//slider_menu buton ayarları için
*******************************************************///////////
}
}
activity_mainpage.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:menu="@menu/menu_main"
app:headerLayout="@layout/nav_header"
android:orientation="vertical"
android:background="#000000">
!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Toolbar için ***********************************************-->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<!-- Toolbar için *****************************************-->
<!-- Container for contents of drawer - use NavigationView to make configuration easier -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="60"
android:alpha=".85"
android:background="@drawable/mainpage_background"
android:layout_marginBottom="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="40"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EMAS"
android:textAlignment="center"
android:textColor="@color/colorWhite"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:layout_weight="20">
<include layout="@layout/mainpage_inner_part" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
我不确定我的 Java 代码或 XML 是否有问题。请帮我解决这个问题。
检查您的 android 清单文件中的应用程序标签中的样式,如下所示 .. 检查 android 清单文件中的这一行 ..
change this
public class MainPageActivity 扩展 AppCompatActivity
android:theme="@style/AppTheme"
然后应用下面的样式。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>