对于那些对这个问题进行贬低和投票的人来说,认为它与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);
}
这里是logcat
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 text =(TextView)view.findViewById(R.id.tt); //此行位于接收器片段(fragmentArtist)
中答案 0 :(得分:2)
正如我在评论中已经说过的那样,当它尚未正确初始化时,你正试图使用Fragment
。如果FragmentSongs
附加到Activity
(ViewPager
请求的操作),则会调用其onAttach()
方法。在此方法中,您将Activity
强制转换为您的侦听器,但您也会立即调用侦听器的回调。这会导致您的应用崩溃(发布的例外),因为在该侦听器中,您尝试使用Fragment
ViewPager
的第二个FragmentArtists
视图。这个Fragment
尚未初始化,此时ViewPager
的第一个片段几乎没有附加到活动上,你已经尝试使用第二个片段的视图了失败。
你链接到的教程是好的,简单的,但是没问题(没有去那个链接我假设你的代码来自教程)。问题是你在一个更复杂的情况下从一个非常简单的场景中使用了这个代码(事实上,如果你在当前教程的代码中插入当前代码,你将获得相同的异常(过早地调用监听器))。
在片段完全构建时应该使用该回调侦听器接口(例如当用户使用应用程序并选择一首歌并且您想要向他显示应用程序的艺术家部分时(此时调用侦听器和使它通过数据))也仔细处理片段不可用的可能性。
如果你需要在片段生命周期的早期片段之间传递数据,你不能保证片段可用(就像你在片段的onAttach()
中调用监听器一样)然后实现片段在生成活动的一个生命周期方法中,从活动中获取直接数据。你传递的东西不清楚所以我在这里说的不多。
答案 1 :(得分:0)
您的FragmentSongs
课程需要实施OnFragmentCreatedListener
public class FragmentSongs extends Fragment implements OnFragmentCreatedListener {
public void onFragmentCreated(ArrayList<SongDetails> msg) {
// whatever
}
}
或在'片段A'的onAttach
中:
listener = (OnFragmentCreatedListener) activity;
这意味着您的Activity
需要实施OnFragmentCreatedListener