0

对于所有那些反对并投票支持这个问题的人认为它与 textview 的范围有关.....然后看看,它与 textview 的范围无关

无法在片段之间传递数据,应用程序崩溃,我不知道我做错了什么......我点击了这个链接 http://manishkpr.webheavens.com/android-passing-data-between-fragments/

这是我正在使用片段 A 的代码(此片段正在传递数据)

public class FragmentSongs extends  Fragment implements Serializable,OnFragmentCreatedListener {
ListView SngList;


 ArrayList<SongDetails> Songinfo=null;
AdapterView.AdapterContextMenuInfo info;

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

    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_song, null);

    SngList = (ListView) view.findViewById(R.id.SongList);
    registerForContextMenu(SngList);
    //File f=new File(Environment.getExternalStorageDirectory()+"/Music");
    File f=new File("/system/");
    int j=0;int i=0;

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

      Songinfo = getSongsFromDirectory(f);



    //sorting done below
    if (Songinfo.size()>0)
    {

        for( j=0; j<Songinfo.size();j++)
        {
            for ( i=j+1 ; i<Songinfo.size(); i++)
            { 
                SongDetails a=Songinfo.get(i);
                SongDetails b=Songinfo.get(j);
                if(a.getSong().toLowerCase().compareTo(b.getSong().toLowerCase())<0)
                {   

                    Songinfo.set(i,b );
                    Songinfo.set(j,a);
                }
            }

        }






   SngList.setAdapter(new CustomAdapter(Songinfo));
   return view;
    }
    else return null;

}
OnFragmentCreatedListener listener;
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        listener = (OnFragmentCreatedListener) getActivity();
        listener.onFragmentCreated(Songinfo);
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement onFragmentCreated");
    }
}

片段 B(接收者片段)

    public class FragmentArtists extends Fragment {
    ArrayList<SongDetails> songinfo2;
ViewGroup view;
int s=5;//TextView text;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{Context context;
LayoutInflater lf = getActivity().getLayoutInflater();   

    view = (ViewGroup) inflater.inflate(R.layout.fragment_artist,  null);
// view =  lf.inflate(R.layout.test,container, false);
// view = inflater.inflate(R.layout.test,container, false);


 s=s+5;
//     text=(TextView)view.findViewById(R.id.tt);when i use this,it works fine but i want to do it in the other method

//text.setText("makzzz");when i use this,it works fine but i want to do it in the other method
return view;

}

 void setSongList(ArrayList<SongDetails> songinfo) 
{    //TextView text=(TextView)this.getView().findViewById(R.id.tt);//when i used this view was not global
      TextView text=(TextView)view.findViewById(R.id.tt);//when i used this,view was global;
      //TextView text=(TextView)view.findViewById(R.id.tt);
    // text=(TextView)super.getView().findViewById(R.id.tt);


     text.setText("makzzz");

持有者活动中的方法(此活动持有片段)

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mymusic);


           fragments = new Vector<Fragment>();
            fragments.add(Fragment.instantiate(this, FragmentSongs.class.getName()));
            fragments.add(Fragment.instantiate(this, FragmentArtists.class.getName()));
            fragments.add(Fragment.instantiate(this, FragmentPlaylists.class.getName()));
            fragments.add(Fragment.instantiate(this, FragmentAlbums.class.getName())); 


            ViewPagerAdapter adapter = new ViewPagerAdapter(super.getSupportFragmentManager(), fragments);


            ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);



            pager.setAdapter(adapter);
            pager.setOffscreenPageLimit(4);

            pager.setCurrentItem(0);

    }      public void onFragmentCreated(ArrayList<SongDetails> msg) {
            FragmentArtists Obj=(FragmentArtists)  fragments.get(1);
            Obj.setSongList(msg);
            }

    }

回调类:

package sourcecode.jazzplayer;

import java.util.ArrayList;

public interface OnFragmentCreatedListener {
    public void onFragmentCreated(ArrayList<SongDetails> msg);

}

这是日志猫

  09-19 23:50:46.851: E/AndroidRuntime(2125): java.lang.NullPointerException
09-19 23:50:46.851: E/AndroidRuntime(2125):     at sourcecode.jazzplayer.FragmentArtists.setSongList(FragmentArtists.java:55)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at sourcecode.jazzplayer.MyMusic.onFragmentCreated(MyMusic.java:45)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at sourcecode.jazzplayer.FragmentSongs.onAttach(FragmentSongs.java:128)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:883)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:823)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:819)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1066)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.view.View.measure(View.java:12603)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.view.View.measure(View.java:12603)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.view.View.measure(View.java:12603)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2072)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.view.View.measure(View.java:12603)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1044)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.os.Looper.loop(Looper.java:137)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at android.app.ActivityThread.main(ActivityThread.java:4340)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at java.lang.reflect.Method.invoke(Method.java:511)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-19 23:50:46.851: E/AndroidRuntime(2125):     at dalvik.system.NativeStart.main(Native Method)

错误在这行代码中

TextView 文本=(TextView)view.findViewById(R.id.tt); //这一行在接收者片段(fragmentArtist)中

4

2 回答 2

2

正如我在评论中已经说过的那样,您正在尝试使用Fragment尚未正确初始化的 a 。当FragmentSongs附加到(将出现Activity的位置请求的操作)时,将调用其方法。在此方法中,您将 投射到您的侦听器,但您也立即调用侦听器的回调。这将使您的应用程序崩溃(您发布的例外情况除外),因为在该侦听器中,您正在尝试使用 , 的第二个视图。这还没有初始化,此时第一个片段几乎没有附加到活动上,您已经在尝试使用第二个片段的视图,这显然会失败。ViewPageronAttach()ActivityFragmentViewPagerFragmentArtistsFragmentViewPager

您链接到的教程是好的,简单的,但是好的(没有去那个链接,我假设您的代码来自教程)。问题是您在更复杂的情况下使用了一个非常简单的场景中的代码(事实上,如果您在该教程的代码中插入当前代码,您将得到相同的异常(过早调用侦听器))。

该回调监听器接口应该在片段完全构建时使用(例如当用户使用应用程序并选择一首歌曲并且您想向他展示应用程序的艺术家部分时(此时调用监听器并使其通过数据))也仔细处理片段不可用的可能性。

如果您需要在片段生命周期的早期在片段之间传递数据,以至于您不能保证片段仍然可用(就像您在onAttach()片段中调用侦听器一样),那么实现片段以直接从活动中获取数据当它被构建时,在它的生命周期方法之一中。你通过什么不清楚,所以我不能在这里说太多。

于 2013-09-20T08:32:43.630 回答
0

你的FragmentSongs类需要实现OnFragmentCreatedListener

public class FragmentSongs extends Fragment implements OnFragmentCreatedListener { 

  public void onFragmentCreated(ArrayList<SongDetails> msg) {
    // whatever
  }


}

或在onAttach您的“片段 A”中:

        listener =  (OnFragmentCreatedListener) activity;

这意味着您Activity需要实施OnFragmentCreatedListener

于 2013-09-19T12:49:25.590 回答