Java 导航抽屉活动,通过碎片发送
我已经创建了一个导航抽屉活动。 我想将信息(通过EditText和按钮)发送到我的receive_片段,信息应显示在TextView中。 当我试图在我的主活动中实现一个接口并在我的各个片段中添加“sEntEdt”和“StEtTeX”方法时,我得到了一个错误消息:“NULL没有被定义”,所以我不得不考虑另一种用法,但是我完全被卡住了。我怎样才能从我的通信类发送消息,最终进入我的Receive\u fragmentclass 沟通:Java 导航抽屉活动,通过碎片发送,java,android,android-studio,android-fragments,navigation-drawer,Java,Android,Android Studio,Android Fragments,Navigation Drawer,我已经创建了一个导航抽屉活动。 我想将信息(通过EditText和按钮)发送到我的receive_片段,信息应显示在TextView中。 当我试图在我的主活动中实现一个接口并在我的各个片段中添加“sEntEdt”和“StEtTeX”方法时,我得到了一个错误消息:“NULL没有被定义”,所以我不得不考虑另一种用法,但是我完全被卡住了。我怎样才能从我的通信类发送消息,最终进入我的Receive\u fragmentclass 沟通: public class communicate_fragment
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;
}
}
发送片段
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;
}
}
我曾尝试在我的主_活动中添加一个接口,用于处理来自communicate_片段的请求并将其发送回receive_片段,但我收到了错误消息“cannot instansiate”
主要活动
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;
}
}
首先创建侦听器接口:
public interface YourCustomListener {
void onItemClick(int position, int ID);
}
现在将此添加到您的第一个片段:
YourCustomListener mListener;
以及您的片段onclick方法:
mListener.onItemClick(position,"YOUR ID");
添加以下内容:
@Override
public void onAttach(Context con) {
super.onAttach(con);
try {
mListener= (YourCustomListener ) con;
} catch (ClassCastException e) {
e.printStackTrace();
}
}
@Override
public void onDetach() {
mListener=null;
super.onDetach();
}
在活动类中实现接口。
在衍生方法中:
@Override
public void onItemClick(int position,int ID) {
fragment2 =new SecondFragment();
Bundle arg= new Bundle();
arg.putInt("ID",ID);
arg.putString("title","ANY TITLE");
fragment2.setArguments(arg);
if (fragment2 != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_left_enter,R.anim.slide_left_exit,R.anim.slide_left_enter,R.anim.slide_left_exit);
fragmentTransaction.replace(R.id.container_body, fragment2,"PROF");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
getSupportActionBar().setLogo(null);
}
}
现在在第二个片段中,在on Create方法中使用以下内容:
Bundle pb=getArguments();
h=pb.getInt("ID");
title=pb.getString("title");
现在有了int h的值和字符串Title。使用此值在文本视图中显示。首先创建侦听器界面:
public interface YourCustomListener {
void onItemClick(int position, int ID);
}
现在将此添加到您的第一个片段:
YourCustomListener mListener;
以及您的片段onclick方法:
mListener.onItemClick(position,"YOUR ID");
添加以下内容:
@Override
public void onAttach(Context con) {
super.onAttach(con);
try {
mListener= (YourCustomListener ) con;
} catch (ClassCastException e) {
e.printStackTrace();
}
}
@Override
public void onDetach() {
mListener=null;
super.onDetach();
}
在活动类中实现接口。
在衍生方法中:
@Override
public void onItemClick(int position,int ID) {
fragment2 =new SecondFragment();
Bundle arg= new Bundle();
arg.putInt("ID",ID);
arg.putString("title","ANY TITLE");
fragment2.setArguments(arg);
if (fragment2 != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_left_enter,R.anim.slide_left_exit,R.anim.slide_left_enter,R.anim.slide_left_exit);
fragmentTransaction.replace(R.id.container_body, fragment2,"PROF");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
getSupportActionBar().setLogo(null);
}
}
现在在第二个片段中,在on Create方法中使用以下内容:
Bundle pb=getArguments();
h=pb.getInt("ID");
title=pb.getString("title");
现在有了int h的值和字符串Title。使用此值在文本视图中显示。,这应该对您有所帮助。。这应该对您有所帮助。