我哪里出错了?我试图在Communicate_fragment中设置editText并将其传递给Recieve_Fragment并将其显示在textView中。
这不会是一个问题如果我没有使用导航抽屉活动,因为我可以在MainAcitivty中绘制片段,这是我现在无法做到的,但我正在努力学习如何实现它以这种方式。
请指出我正确的方向,因为我是初学者,并试图学习工艺品。
AndroidManager说:
Attempt to invoke virtual method 'void fragmentdrawer.recieve_fragment.setText(java.lang.String)' on a null object reference
但我无法理解为什么。
主:
package com.example.name.fragmentdrawer;
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.support.v4.app.FragmentManager;
import fragmentdrawer.about_me_fragment;
import fragmentdrawer.another_fragment;
import fragmentdrawer.communicate_fragment;
import fragmentdrawer.mainfragment;
import fragmentdrawer.recieve_fragment;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, communicate_fragment.TopSectionListener{
@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;
}
public void createMeme(String text) {
recieve_fragment texten = (recieve_fragment) getFragmentManager().findFragmentById(R.id.recieve);
texten.setText(text);
}
}
Communicate_fragmentclass:
package fragmentdrawer;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.example.name.fragmentdrawer.R;
public class communicate_fragment extends Fragment{
private static EditText topTextInput;
private static Button button;
TopSectionListener activityCommander;
public interface TopSectionListener{
public void createMeme(String text);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
activityCommander = (TopSectionListener) activity;
}catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_communicate, container, false);
topTextInput = (EditText) rootView.findViewById(R.id.message);
final Button button = (Button) rootView.findViewById(R.id.buttonSend);
button.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v){
buttonClicked(v);
}
}
);
return rootView;
}
public void buttonClicked(View view) {
activityCommander.createMeme(topTextInput.getText().toString());
}
}
RECIEVE_CLASS_FRAMENT
package fragmentdrawer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.name.fragmentdrawer.R;
public class recieve_fragment extends Fragment{
private static TextView textView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_recieve, container, false);
textView = (TextView) rootView.findViewById(R.id.displayMsg);
return rootView;
}
public void setText(String text){
textView.setText(text);
}
}
Recieve_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/displayMsg"
android:layout_gravity="center" />
</FrameLayout>