I am working on Android Studio app and I have just created a menu bar with three options. I am now trying to load a new activity when the menu items are selected. The problem I am having: When clicking on a menu item it opens a blank activity and not the fragment i have selected .
This is what I have done:
- created java classes and XML pages for each corresponding menu item.
I will show the code for only my main activity and one of the new activity pages I created.
friendListFragment.java
This is a new java class I created
package com.example.assignment1;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
public class friendListFragment extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
and then in my main activity file i have the following
package com.example.assignment1;
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 android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item ){
Intent intent = null;
switch (item.getItemId()){
case R.id.viewFriendList:
intent = new Intent(this,friendListFragment.class);
startActivity(intent);
break;
case R.id.toDoList:
intent = new Intent(this,toDoListFragment.class);
startActivity(intent);
break;
case R.id.eventList:
intent = new Intent(this,eventFragment.class);
startActivity(intent);
break;
default:
return super.onOptionsItemSelected(item);
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}
else
{
super.onBackPressed();
}
}
}
I have created this from an empty activity when I do not click on any menu options the menu bar appears correctly. Also, I think the error is in the onNavigationItemSelected function.
this is the error i am getting in my logcat
08-14 15:45:27.834 6816-6833/com.example.assignment1 E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae613460
and this is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.assignment1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".friendListFragment"></activity>
<activity android:name=".toDoListFragment"></activity>
<activity android:name=".eventFragment"></activity>
</application>
</manifest>
Basically, when i click on an option in the menu bar it loads a blank page