1

我已創建導航抽屜活動。 我想發送信息(通過EditText和按鈕)到我的receive_fragment信息應該出現在TextView中。 當我試圖在我的mainActivity中實現一個接口,並在我的相應片段中添加「sendText」和「SetText」方法時,我得到了「null未定義」的錯誤消息,因此我不得不考慮替代用法,但完全卡住了。我怎麼能從我的Communicate-class發送一條消息到我的Recieve_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; 
    }  
    } 

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; 
    } 
} 

我試圖在我的Main_activity從communicate_fragment處理該請求,並將其發送回recieve_fragment增加一個接口,但是我得到「不能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; 
    }  
} 
+0

http://stackoverflow.com/questions/25134645/get-edittext-value-from-fragment,這應該會對你有所幫助。 – BIW

回答

0

首先創建監聽器接口:

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(); 
    } 

實現Activity類接口。 在導出方法:

@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); 
     } 
    } 

現在在你的第二個片段,在上創建方法使用:

Bundle pb=getArguments(); 
     h=pb.getInt("ID"); 
     title=pb.getString("title"); 

現在你有一個整型H的值,字符串標題。使用此值在您的textview中顯示。