Inside of my XML document I have just a basic textview with the ID of username. When I remove username.setText() the program runs fine.
Here's my code:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private ImageButton homeButton;
int totalTickets = 0;
public TextView showValue;
private MySqliteOpenHelper mySqliteOpenHelper;
private SQLiteDatabase mDatabase;
public String googleDisplayName;
private TextView username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_menu);
navigationView.setNavigationItemSelectedListener(this);
showValue = (TextView) findViewById(R.id.ticketCounter);
username = (TextView) findViewById(R.id.username);
username.setText("Test");
Not really sure if the problem has something to do with my XML code. I'm getting a NPE but I am not really sure what I need to do to fix that. My textview, username, has a text so it's not null. Maybe I am misunderstanding something, I am pretty new to android.
XML Code:
<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="176dp"
android:background="#414040"
android:gravity="bottom"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
tools:layout_editor_absoluteY="25dp">
<TextView
android:id="@+id/version_Number"
android:layout_width="178dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:text="@string/nav_header"
android:textSize="8sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/username"
app:layout_constraintTop_toBottomOf="@+id/username" />
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="26dp"
android:layout_above="@+id/version_Number"
android:layout_alignParentStart="true"
android:text="user"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/version_Number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/navButton" />