I have created a navigation drawer activity. I want to send information (via a EditText and button) to my receive_fragment where the information should appear in a TextView. When I tried to implement an interface in my mainActivity and added "sendText" and "SetText" methods in my respective fragments I got the errormessage that "null is not defined" so I had to consider an alternative usage but am totally stuck. How can I go about sending a message from my Communicate-class to end up in my Recieve_fragmentclass?
COMMUNICATE:
public class communicate_fragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_communicate, container, false);
return rootView;
}
}
SEND_FRAGMENT
public class recieve_fragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_recieve, container, false);
return rootView;
}
}
I have tried to add an interface in my Main_activity that handles the request from communicate_fragment and sends it back to recieve_fragment, however I get the error message that "cannot instansiate"
MAIN_ACTIVITY
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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);
android.app.FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new mainfragment()).commit();
}
@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.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
android.app.FragmentManager fm = getFragmentManager();
int id = item.getItemId();
if (id == R.id.about_me) {
fm.beginTransaction().replace(R.id.content_frame, new about_me_fragment()).commit();
} else if (id == R.id.another_fragment) {
fm.beginTransaction().replace(R.id.content_frame, new another_fragment()).commit();
} else if (id == R.id.send) {
fm.beginTransaction().replace(R.id.content_frame, new communicate_fragment()).commit();
}
else if (id == R.id.recieve) {
fm.beginTransaction().replace(R.id.content_frame, new recieve_fragment()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}