-2

我正在制作一个应用程序,您可以在其中查看报价,如果您喜欢任何报价,则可以按星形按钮并将其复制到另一个名为favorite的片段中。 引号本身被写入不同的片段,而不是主要活动。任何人都可以帮助我吗?如何与Android Studio2.0中的片段进行通信?

这里是我的MainActivity

package com.example.nouma.life; 

import android.app.Fragment; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.app.FragmentManager; 
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.widget.Button; 
import android.widget.TextView; 

import java.util.HashMap; 
import java.util.Map; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    public static final String My_Custom_Fragment_Key = "Custom_Text"; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     final TextView t1=(TextView)findViewById(R.id.t1); 
     Button b1=(Button)findViewById(R.id.b1); 

     b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

     } 
    }); 

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

    @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. 
     int id = item.getItemId(); 
     android.app.FragmentManager fragmentManager = getFragmentManager(); 

     if (id == R.id.nav_first_layout) { 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame,new FirstFragment()) 
        .commit(); 
     } else if (id == R.id.nav_second_layout) { 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame,new SecondFragment()) 
        .commit(); 

     } else if (id == R.id.nav_third_layout) { 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame, new ThirdFragment()) 
        .commit(); 
     }else if(id ==R.id.nav_favorites) { 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame, new favorites()) 
        .commit(); 
     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

,这是我firstfragment代码

package com.example.nouma.life; 

import android.app.Activity; 
import android.app.Fragment; 
import android.content.Intent; 
import android.icu.util.BuddhistCalendar; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.FragmentManager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.ToggleButton; 

import org.greenrobot.eventbus.EventBus; 

import java.net.FileNameMap; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Random; 

/** 
* Created by nouma on 28-08-2016. 
*/ 
public class FirstFragment extends Fragment { 


    View myView; 
    final TextView t3=(TextView)myView.findViewById(R.id.t3); 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     myView=inflater.inflate(R.layout.first_layout,container,false); 

     final TextView t4=(TextView)myView.findViewById(R.id.t4); 
     final TextView t5=(TextView)myView.findViewById(R.id.t5); 

     final Map<String,String> quoteMap= new HashMap<String, String>(); 
     quoteMap.put("quoteKey", t3.getText().toString()); 
     EventBus.getDefault().post(quoteMap); 
     Button b1 =(Button)myView.findViewById(R.id.button); 
     ToggleButton toggleButton=(ToggleButton)myView.findViewById(R.id.toggleButton); 
     toggleButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       EventBus.getDefault().post(quoteMap); 


      } 
     }); 
     b1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      } 
     }); 
     return myView; 

    } 
} 

这是最喜欢的片段

package com.example.nouma.life; 

import android.app.Fragment; 
import android.app.usage.UsageEvents; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import org.greenrobot.eventbus.EventBus; 
import org.greenrobot.eventbus.Subscribe; 

import java.util.HashMap; 
import java.util.Map; 

/** 
* Created by nouma on 16-09-2016. 
*/ 
public class favorites extends Fragment { 

    EventBus eventBus = EventBus.getDefault(); 

    View myView; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     myView = inflater.inflate(R.layout.favorites, container, false); 

     return myView; 
    } 
    public void onCreate() 
    { 
     eventBus.register(this); 
    } 
    public void onDestroy() 
    { 
     eventBus.unregister(this); 
    } 
    @Subscribe 
    public void onEvent(Map quoteValues) 
    { 
     if (quoteValues.get("quoteValue") != null) 
     { 
      //get text 
      String quoteText = (String) quoteValues.get("quoteKey"); 
     } 
    } 
} 

我很新的片段和整体Java编程 我甚至把这个链接https://developer.android.com/training/basics/fragments/communicating.html 但culdnt gt我的答案。

这是使用EventBus

09-19 05:01:04.089 11861-11861/com.example.nouma.life I/art: Late-enabling -Xcheck:jni 
09-19 05:01:05.199 11861-11861/com.example.nouma.life W/art: Failed to find OatDexFile for DexFile /data/data/com.example.nouma.life/files/instant-run/dex/slice-slice_3-classes.dex (canonical path /data/data/com.example.nouma.life/files/instant-run/dex/slice-slice_3-classes.dex) with checksum 0x554edc19 in OatFile /data/data/com.example.nouma.life/cache/slice-slice_3-classes.dex 
09-19 05:01:06.849 11861-11861/com.example.nouma.life W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
09-19 05:01:07.789 11861-12136/com.example.nouma.life D/OpenGLRenderer: Render dirty regions requested: true 
09-19 05:01:07.819 11861-11861/com.example.nouma.life D/ActivityThreadInjector: clearCachedDrawables. 
09-19 05:01:08.209 11861-12136/com.example.nouma.life I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017_msm8916_64_LA.BR.1.1.3_RB1__release_AU (Ie228694f41) 
                    OpenGL ES Shader Compiler Version: E031.25.03.04 
                    Build Date: 09/29/15 Tue 
                    Local Branch: mybranch14662643 
                    Remote Branch: quic/LA.BR.1.1.3_rb1.32 
                    Local Patches: NONE 
                    Reconstruct Branch: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017 + 26a3cba + 6f69ea6 + 8bc2bc8 + 649fcde + a52cccf + dbf281f + 15f0bf8 + 8d02f76 + 9b2cb1a + 25f3b04 + 7cd8c84 + b54906e + 675fd74 + 7c22ef4 + 79b094c 
09-19 05:01:08.239 11861-12136/com.example.nouma.life I/OpenGLRenderer: Initialized EGL, version 1.4 
09-19 05:01:08.399 11861-12136/com.example.nouma.life D/OpenGLRenderer: Enabling debug mode 0 
09-19 05:01:08.459 11861-12136/com.example.nouma.life I/qdutils: PartialUpdate status: Disabled 
09-19 05:01:08.459 11861-12136/com.example.nouma.life I/qdutils: Left Align: 0 
09-19 05:01:08.459 11861-12136/com.example.nouma.life I/qdutils: Width Align: 0 
09-19 05:01:08.459 11861-12136/com.example.nouma.life I/qdutils: Top Align: 0 
09-19 05:01:08.459 11861-12136/com.example.nouma.life I/qdutils: Height Align: 0 
09-19 05:01:08.459 11861-12136/com.example.nouma.life I/qdutils: Min ROI Width: 0 
09-19 05:01:08.459 11861-12136/com.example.nouma.life I/qdutils: Min ROI Height: 0 
09-19 05:01:08.459 11861-12136/com.example.nouma.life I/qdutils: Needs ROI Merge: 0 
09-19 05:01:08.459 11861-12136/com.example.nouma.life I/qdutils: Dynamic Fps: Enabled 
09-19 05:01:08.459 11861-12136/com.example.nouma.life I/qdutils: Min Panel fps: 45 
09-19 05:01:08.459 11861-12136/com.example.nouma.life I/qdutils: Max Panel fps: 60 
09-19 05:01:08.709 11861-11861/com.example.nouma.life I/Choreographer: Skipped 52 frames! The application may be doing too much work on its main thread. 
09-19 05:01:09.209 11861-11861/com.example.nouma.life I/Timeline: Timeline: Activity_idle id: [email protected] time:36482266 
09-19 05:01:10.319 11861-11861/com.example.nouma.life W/PathParser: Points are too far apart 4.000000596046461 
09-19 05:01:11.099 11861-11861/com.example.nouma.life D/AndroidRuntime: Shutting down VM 
09-19 05:01:11.119 11861-11861/com.example.nouma.life E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.example.nouma.life, PID: 11861 
                     java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference 
                      at com.example.nouma.life.FirstFragment.<init>(FirstFragment.java:35) 
                      at com.example.nouma.life.MainActivity.onNavigationItemSelected(MainActivity.java:106) 
                      at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:153) 
                      at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810) 
                      at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) 
                      at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957) 
                      at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:328) 
                      at android.view.View.performClick(View.java:4759) 
                      at android.view.View$PerformClick.run(View.java:19770) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5237) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707) 

回答

1

您可以使用EventBus库后logchat。看看这个http://greenrobot.org/eventbus/documentation/how-to-get-started/

在活动

制作地图为您要发送的值。

例如:

Map<String,String> quoteMap= new HashMap<String, String>(); 
     quoteMap.put("quoteKey", "Quote text"); 

邮政当用户使用此代码EventBus.getDefault().post(quoteMap);

在收藏片段

定义EventBus使用此代码EventBus eventBus = EventBus.getDefault();

压在星号按钮的地图你应该注册并取消注册,使用的onCreate方法用于寄存器eventBus.register(this);

用于注销使用的onDestroy方法。 eventBus.unregister(this);

在Favorite Fragment中写入新方法。你可以任意命名。

 @Subscribe 
    public void onEvent(Map quoteValues) 
    { 
     if (quoteValues.get("quoteValue") != null) 
     { 
      //get text 
      String quoteText = (String) quoteValues.get("quoteKey"); 
     } 
    } 
+0

其实我试图从第一个片段获取值到最喜欢的片段,所以我应该把这个代码放在第一个片段上? –

+0

是的,您应该将此代码EventBus.getDefault()。post(quoteMap)放到第一个片段中。 –

+0

它表示无法解析符号EventBus –

相关问题