Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

java - setDisplayHomeasUpEnabled may produce null PointerException

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...