-2

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

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

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


     //     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
      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);
    //i also tried keeping the "text" local     

     text.setText("makzzz");
}}

正在发送数据的片段............

public class FragmentSongs extends  Fragment implements Serializable {

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

. . . . .

持有人活动

package sourcecode.jazzplayer;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import sourcecode.jazzplayer.FragmentSongs.OnFragmentCreatedListener;
import sourcecode.jazzplayer.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MyMusic  extends FragmentActivity implements OnFragmentCreatedListener{
    List<Fragment> fragments;
    @Override
    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);

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

    }
4

1 回答 1

1

你应该打电话

text=(TextView)view.findViewById(R.id.tt);

onCreateView(),或者在对它进行任何操作之前从 onCreateView() 调用的方法中。如果文本尚未分配给 UI 元素,TextView 将抛出 NullPoint 异常。如果您真的希望在另一种方法中进行分配,请编写如下方法

private void initUI()
于 2013-09-19T14:16:47.713 回答