当我将自定义工具栏包含在另一个 activity 中时,如何使用自定义工具栏上的图标?
How can I use icon on custom tool bar when I include it on another activity?
我在后续 activity 添加后添加了工具栏我想在右侧添加一个图标,但它给了我 error.Please 帮助我如何解决它。
public class Add_Followup extends AppCompatActivity {
androidx.appcompat.widget.Toolbar tool_bar
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__followup);
tool_bar=(Toolbar) findViewById(R.id.custom_toolbar);
setSupportActionBar(tool_bar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
tool_bar.setNavigationIcon(R.drawable.back);
}
我是这样收录的
<include
layout="@layout/custom_tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
这是自定义布局
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:fontFamily="serif"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#FF9B9B"
android:elevation="4dp"
app:title="Welcome"
app:titleTextColor="#ffff"
app:subtitleTextColor="#ffff"
android:id="@+id/custom_toolbar">
我遇到了这个异常
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at com.example.loginproject.Add_Followup.onCreate(Add_Followup.java:54)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
这样调用id就可以了
<include layout="@layout/custom_tool_bar"
android:id="@+id/custom_toolbar"/>
我在后续 activity 添加后添加了工具栏我想在右侧添加一个图标,但它给了我 error.Please 帮助我如何解决它。
public class Add_Followup extends AppCompatActivity {
androidx.appcompat.widget.Toolbar tool_bar
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__followup);
tool_bar=(Toolbar) findViewById(R.id.custom_toolbar);
setSupportActionBar(tool_bar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
tool_bar.setNavigationIcon(R.drawable.back);
}
我是这样收录的
<include
layout="@layout/custom_tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
这是自定义布局
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:fontFamily="serif"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#FF9B9B"
android:elevation="4dp"
app:title="Welcome"
app:titleTextColor="#ffff"
app:subtitleTextColor="#ffff"
android:id="@+id/custom_toolbar">
我遇到了这个异常
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at com.example.loginproject.Add_Followup.onCreate(Add_Followup.java:54)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
这样调用id就可以了
<include layout="@layout/custom_tool_bar"
android:id="@+id/custom_toolbar"/>