问题描述
对于所有那些谁是downvoting和这个问题的投票被关闭以为它是与TextView的范围.....再看看,它无关的TextView的范围。
公共类FragmentArtists扩展片段{
ArrayList的< SongDetails> songinfo2;
查看视图。
TextView的文本;
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState)
{上下文的背景下;
LayoutInflater LF = getActivity()getLayoutInflater()。 //视图= inflater.inflate(R.layout.fragment_artist,NULL);
鉴于= lf.inflate(R.layout.test,集装箱,FALSE);
//视图= inflater.inflate(R.layout.test,集装箱,FALSE);
//文本=(TextView的)view.findViewById(R.id.tt);当我用这个,它工作正常,但我想这样做的另一种方法//text.setText(\"makzzz\");when我用这个,它工作正常,但我想这样做的另一种方法
返回视图。} 无效setSongList(ArrayList的< SongDetails> songinfo)
{// TextView的文本=(TextView的)this.getView()findViewById(R.id.tt); //当我用这个观点是不是全球性的
文字=(TextView的)view.findViewById(R.id.tt); //当我用这个,认为是全球性的;
// TextView的文本=(TextView的)view.findViewById(R.id.tt);
//文本=(TextView的)super.getView()findViewById(R.id.tt)。
//我也试图保持文本本地 text.setText(makzzz);
}}
被发送数据的片段..........
公共类FragmentSongs扩展片段实现Serializable { ListView的SngList;
ArrayList的< SongDetails> Songinfo = NULL;
AdapterView.AdapterContextMenuInfo信息; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState)
{ 视图的ViewGroup =(ViewGroup中)inflater.inflate(R.layout.fragment_song,NULL); SngList =(ListView控件)view.findViewById(R.id.SongList);
registerForContextMenu(SngList);
//文件f =新的文件(Environment.getExternalStorageDirectory()+/音乐);
文件f =新的文件(/系统/);
INT J = 0; INT I = 0; 。getActivity()setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); Songinfo = getSongsFromDirectory(F); //下面的排序完成
如果(Songinfo.size()大于0)
{ 为(J = 0; J< Songinfo.size(); J ++)
{
对于(I = J + 1; I< Songinfo.size();我++)
{
SongDetails一个= Songinfo.get(ⅰ);
SongDetails B = Songinfo.get(J);
如果(a.getSong()与toLowerCase()的compareTo(b.getSong()与toLowerCase())≤; 0)
{ Songinfo.set(I,B);
Songinfo.set(J,一);
}
} }
SngList.setAdapter(新CustomAdapter(Songinfo));
返回视图。
}
否则返回NULL; }
OnFragmentCreatedListener监听;
@覆盖
公共无效onAttach(活动活动){
super.onAttach(活动);
尝试{
听者=(OnFragmentCreatedListener)getActivity();
listener.onFragmentCreated(Songinfo);
}赶上(抛出ClassCastException E){
抛出新ClassCastException异常(activity.toString()+必须实现onFragmentCreated);
}
}
。
。
。
。
持有人活动
包源$ C $ c.jazzplayer;进口的java.util.ArrayList;
进口的java.util.List;
进口java.util.Vector中;进口来源$ C $ c.jazzplayer.FragmentSongs.OnFragmentCreatedListener;
进口来源$ C $ c.jazzplayer.R;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.view.ViewPager;公共类MyMusic扩展FragmentActivity实现OnFragmentCreatedListener {
清单<片断>片段;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.mymusic);
碎片=新的矢量<片断>();
fragments.add(Fragment.instantiate(在此,FragmentSongs.class.getName()));
fragments.add(Fragment.instantiate(在此,FragmentArtists.class.getName()));
fragments.add(Fragment.instantiate(在此,FragmentPlaylists.class.getName()));
fragments.add(Fragment.instantiate(在此,FragmentAlbums.class.getName()));
ViewPagerAdapter适配器=新ViewPagerAdapter(super.getSupportFragmentManager(),片段);
ViewPager寻呼机=(ViewPager)super.findViewById(R.id.viewpager); pager.setAdapter(适配器);
pager.setOffscreenPageLimit(4); pager.setCurrentItem(0); @覆盖
公共无效onFragmentCreated(ArrayList的< SongDetails>味精){
FragmentArtists的Obj =(FragmentArtists)fragments.get(1);
Obj.setSongList(MSG);
} }
您应该叫
文本=(TextView的)view.findViewById(R.id.tt);
onCreateView(),或在从onCreateView()用它做任何事情之前调用的方法。如果文本没有被分配到UI元素,TextView的抛出异常NullPoint。如果你真的想要在其他方法分配,这样写
的方法 私人无效initUI()
For all those who are downvoting and voting for this question to be closed thinking it has something to do with the scope of textview.....then have a look,it has nothing to do with the scope of 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");
}}
the fragment which is sending the data ..........
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");
}
}
.....
the holder activity
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);
}
}
You should call
text=(TextView)view.findViewById(R.id.tt);
onCreateView(), or in the method that is called from onCreateView() before doing anything with it. If text hasn't been assigned to UI element, TextView throws NullPoint Exception. If you really want that assigning in another method, write a method like
private void initUI()
这篇关于无法通过片段之间的数据..... TextView中抛出异常NullPoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!