Application crash when I try to open the activity - java

When I press login on my app it should take me to activity.targa, but instead it crashes and I can't understand why (My phone is Redmi Note 7, but it also crashes on Mi A2)
activity.targa:
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:fitsSystemWindows="true"
tools:context=".TargaActivity">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<com.google.android.material.circularreveal.CircularRevealFrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView.../>
<com.google.android.material.textfield.TextInputLayout.../>
<TextView.../>
<TextView.../>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
and TargaActivity:
package com.efficientparking.efficientparking;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import java.util.HashMap;
public class TargaActivity extends AppCompatActivity {
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_targa);
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
}
#Override
public void onBackPressed() {
if(drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
I had to change here since Android Studio gave me troubles because he was saying that the following method (setSupportActionBar(toolbar)) didn't exist.
But now as soon as I try to get into activity_targa, app crashes.
Android studio actually load the xml and it seems nice, but then I compile the apk and it crashes.
Log file:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.efficientparking/com.efficientparking.efficientparking.TargaActivity}: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3460)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2047)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7590)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
at com.efficientparking.efficientparking.TargaActivity.onCreate(TargaActivity.java:22)
at android.app.Activity.performCreate(Activity.java:7893)
at android.app.Activity.performCreate(Activity.java:7880)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3286)
... 11 more

You should use android.widget.Toolbar in both xml and java, as setSupportActionBar takes android.widget.Toolbar

Related

How can I access a TextView in the onCreate function?

Hi have been using fragments and have a drawer header which is going to display the currently logged in user.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="false"
android:background="?android:attr/colorBackground"
tools:context=".MainActivity">
<include
layout="#layout/drawer_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
This is the code that creates the layout and the adds the header and menu to the navigation view. I am trying to access the text stored in the drawer header with an id.
The problem i get is that when it tries to set the variable usernameHeader to the id "user" it throws an error saying that there is a null object reference. My understanding is that the drawer is create during the onCreate when it says "actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);" i could be wrong and any help is greatly appreciated. Thanks in advance
UPDATE:
I have rewritten and minimal example to show the exact part where it breaks.
I have added the drawer_header.xml which contains the TextView with the id 'user'. I have also added the minimal version of the MainActivity.
drawer_header.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="150dp"
android:background="#color/teal_700">
<TextView
android:id="#+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:text="#string/user"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.secureit.drawerfragment;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer);
navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
actionBarDrawerToggle.syncState();
TextView textView = findViewById(R.id.user); // This is where the error shows "Null object reference"
textView.setText("user");
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
drawerLayout.closeDrawer(GravityCompat.START);
return false;
}
}
The problem is, you are accessing a TextView in different view from your activity. You should inflate your header view first and then use that to access your TextView. Add this to your onCreate method.
View headerView = navigationView.getHeaderView(0);
TextView txtUser= headerView.findViewById(R.id.user);
txtUser.setText("user");

Error inflating class androidx.appcompat.widget.Toolbar android

I try to use the toolbar in my app and I get this message: "Error inflating class androidx.appcompat.widget.Toolbar".
i glad if someone will find my problem (using the latest version of the android studio)
XML:
<androidx.appcompat.widget.Toolbar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00008577"
android:minHeight="?actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="#drawable/main"
android:id="#+id/toolbar"/>
JAVA:
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
setSupportActionBar(toolbar);
}
private void findViews() {
drawer_layout = findViewById(R.id.drawer_layout);
nav_view = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
}
STYLE:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
Exception received
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.or.ourshoppinglist/com.or.ourshoppinglist.MainActivity}: android.view.InflateException: Binary XML file line #28: Binary XML file line #28: Error inflating class <unknown>
Hi noa shen and welcome to Stackoverflow.
Firstly, there can be an issue with your import for Toolbar. Since androix version came out now you got two versions of Toolbar:
androidx.appcompat.widget.Toolbar
and
android.support.v7.widget.Toolbar
So first check your imports in your JAVA.class and see if you imported the right version which would be:
androidx.appcompat.widget.Toolbar
Besides that, why is your toolbar width equals to 0?
<androidx.appcompat.widget.Toolbar
android:layout_width="0dp"
Also to use toolbar you need to paste this in your build Gradle file in dependencies:
implementation 'androidx.appcompat:appcompat:1.0.0'
If this doesn't solve anything please provide your whole XML, JAVA.class, and full stack trace of your exception.
In build.gradle (module app) file, implementation of The upgraded version : implementation 'androidx.appcompat:appcompat:1.4.1' caused me the error. When I used implementation 'androidx.appcompat:appcompat:1.0.0' , The error has gone.
added my whole 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_gravity="start"
android:layout_height="match_parent"
android:layout_width="wrap_content"
app:headerLayout="#layout/header"
app:menu="#menu/main_menu" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<androidx.appcompat.widget.Toolbar
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#00008577"
android:minHeight="?actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="#drawable/main"
android:id="#+id/toolbar"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>
JAVA:
package com.or.ourshoppinglist;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawer_layout;
private NavigationView nav_view;
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer_layout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer_layout.addDrawerListener(toggle);
toggle.syncState();
}
private void findViews() {
drawer_layout = findViewById(R.id.drawer_layout);
nav_view = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
}
}
I fixed this by changing android:background to app:srcCompat when I am using
?android:attr.

How to remove the shadow under Action bar

I have been trying to get the shadow under the app Action bar to disappear but it wont which is very annoying. iI have set the theme to NoActionBar and opted to using toolbar instead.
And have i have also set the elevation to 0dp in my activity.java
package com.example.drawerlayout;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.appbar.AppBarLayout;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private AppBarLayout actionLayout;
private Toolbar t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mToolbar = (Toolbar) findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setElevation(0);
}
}
The Toolbar looks thus:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/mm_blue" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Simply add app:elevation="0dp"
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<!-- Toolbar -->
</com.google.android.material.appbar.AppBarLayout>
Set this property toolbar:elevation="0dp" to AppBarLayout to remove shadow from Toolbar.
eg-
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
toolbar:elevation="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/mm_blue" />
</com.google.android.material.appbar.AppBarLayout>
Check below code
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/mm_blue" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Just remove the AppBar layout and directly put your toolbar in the coordinate layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/nav_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/mm_blue" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Try this may it should help you out.

Missing Hamburger Icon Android

I tried searching for different answers regarding the hamburger icon, but haven't found anything similar. I'm trying to create a hamburger icon and have been following a tutorial. The code runs fine, but the icon is missing when I open it in the virtual device. Any suggestions are appreciated.
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android.navigationapp.navigationapp.MainActivity"
android:id="#+id/drawerLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Main Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_gravity="center_vertical"
android:textAlignment="center"
android:textSize="24dp" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
MainActivity Java
package com.android.navigationapp.navigationapp;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Android App Crash On Button Click

I am building an app and on a button click the app crashes. The app was working perfectly about a week ago and the code has not been altered in anyway since the last time I tested it. Now when I click the Login in button it crashes. This makes no sense as it hasn't been edited. Source code below.
Login.java
package com.example.martin.ivebeenthere;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class Login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void onClickbtnFeed(View view)
{
startActivity(new Intent(Login.this, Feed.class));
}
}
content_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.martin.ivebeenthere.Login"
tools:showIn="#layout/activity_login">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/logo"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="276dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:hint="Username"
android:id="#+id/editText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:hint="Password"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/button3"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:onClick="onClickbtnFeed"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Login With Facebook"
android:id="#+id/button4"
android:background="#3b5998"
android:textColor="#ffffff"
android:layout_below="#+id/button3"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3"
android:layout_marginTop="25dp" />
</RelativeLayout>
Feed.java (Page Linked With Button)
package com.example.martin.ivebeenthere;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Feed extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
String[] users = {"\nMartin Hoban\n\nVisited Dublin 2 Hours Ago\n","\nJack White\n\nVisited Mayo 10 Hours Ago\n","\nSarah Cox\n\nVisited Cork 1 Day Ago\n","\nMalcolm Holmes\n\nVisited Knock 2 Days Ago\n", "\nCraig Fisher\n\nVisited The Spire 2 Days Ago\n", "\nHannah Smith\n\nVisited Mayo 3 Days Ago\n"
,"\nLanelle Delaune\n\nVisited Limerick 1 Week Ago\n", "\nHanna Whitlow\n\nVisited Paris 1 Week Ago\n", "\nMoses Primm\n\nVisited Italy 3 Weeks Ago\n", "\nBerta Pedraza\n\nVisited The Aillwee Caves 1 Month Ago\n"};
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, users);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(itemsAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 4) {
Intent myIntent = new Intent(view.getContext(), CheckinInfo.class);
startActivityForResult(myIntent, 0);
}
}
});
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.feed, menu);
return true;
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_feed) {
startActivity(new Intent(getApplicationContext(), Feed.class));
} else if (id == R.id.nav_map) {
startActivity(new Intent(getApplicationContext(), Map.class));
} else if (id == R.id.nav_account) {
startActivity(new Intent(getApplicationContext(), Account.class));
} else if (id == R.id.nav_logout) {
startActivity(new Intent(getApplicationContext(), Home.class));
Toast.makeText(getApplicationContext(), "Logged Out",
Toast.LENGTH_LONG).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Logcat
Caused by: java.lang.NullPointerException
at com.example.martin.ivebeenthere.Feed.onCreate(Feed.java:38)
at android.app.Activity.performCreate(Activity.java:5047)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117) 
at android.app.ActivityThread.access$700(ActivityThread.java:134) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4867) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 
at dalvik.system.NativeStart.main(Native Method)
activity_feed.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_feed"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
app_bar_feed.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.example.martin.ivebeenthere.Feed">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_feed" />
</android.support.design.widget.CoordinatorLayout>
Rendering Problem In Design View of activity_feed.xml
java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1464)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.android.layoutlib.bridge.util.ReflectionUtils.invoke(ReflectionUtils.java:45)
at com.android.layoutlib.bridge.android.support.DrawerLayoutUtil.openDrawer(DrawerLayoutUtil.java:56)
at com.android.layoutlib.bridge.android.support.DrawerLayoutUtil.openDrawer(DrawerLayoutUtil.java:50)
at android.view.BridgeInflater.postInflateProcess(BridgeInflater.java:361)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.postInflateProcess(RenderSessionImpl.java:968)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.postInflateProcess(RenderSessionImpl.java:973)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:235)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:426)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:510)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:498)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:888)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:498)
at com.android.tools.idea.rendering.RenderTask.access$600(RenderTask.java:72)
at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:610)
at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:607)
at com.android.tools.idea.rendering.RenderService.runRenderAction(RenderService.java:362)
at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:607)
at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:629)
at com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel$6.run(AndroidDesignerEditorPanel.java:480)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:351)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
I think that the referenced dimension resource can't be found.
The reason may be, you have dimensions specified for specific screen sizes only.
your content.xml:
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
define them in values-820dp/dimens.xml or define them in content.xml
you can remove them too.
The local variable navigationView is null. That means there's no view with id nav_view in your activity_feed layout. Make sure to include all views you try to find with findViewById() in onCreate() into the layout.

Categories