对于所有那些反对并投票支持这个问题的人认为它与 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)中