I am trying to implement an up button in my toolbar to redirect to the previous activity. The up button coded in the Dashboard.java. When I click the up button, it will display the Homepage.
Android Manifest.xml
<activity android:name="fyp.ui_activities.Dashboard"
android:parentActivityName="fyp.ui_activities.Homepage">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value="fyp.ui_activities.Homepage"/>
</activity>
For Themes.xml, I am using NoActionBar
Dashboard.xml
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
tools:context=".Dashboard">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
app:title="@string/dashboardheader"
app:titleTextColor="@color/font_Primary">
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Dashboard.java
public class Dashboard extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
TextView speed,rpm,test;
int i,j;
Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
setUpToolbar();
print();
createThread();
}
public void setUpToolbar(){
drawerLayout = findViewById(R.id.drawer_layout);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.dashboardheader,R.string.dashboardheader);
}
}
Error
java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
question from:
https://stackoverflow.com/questions/65841150/setdisplayhomeasupenabled-may-produce-null-pointerexception 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…