ViewPager 未从代码更新,android,eclipse
ViewPager not updating from code, android, eclipse
在这里搜索了很多答案后,我似乎无法解决我的问题。
首先,我在使用 findViewById 从片段中获取 listView 时遇到问题,为了解决这个问题,我必须先膨胀片段的布局并使用 "mContainer.findViewById..".
我不知道这是否是 viewpager 未更新(或列表视图本身)的原因。
我的 OnCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myContainer = getLayoutInflater().inflate(R.layout.frag_links,null);
root = (RelativeLayout) findViewById(R.id.rootLayout);
root.post(new Runnable() {
@Override
public void run() {
Rect rect = new Rect();
Window win = getWindow(); // Get the Window
win.getDecorView().getWindowVisibleDisplayFrame(rect);
// Get the height of Status Bar
int statusBarHeight = rect.top;
// Get the height occupied by the decoration contents
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
// Calculate titleBarHeight by deducting statusBarHeight from contentViewTop
int titleBarHeight = contentViewTop - statusBarHeight;
Log.i("MY", "titleHeight = " + titleBarHeight + " statusHeight = " + statusBarHeight + " contentViewTop = " + contentViewTop);
// By now we got the height of titleBar & statusBar
// Now lets get the screen size
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenHeight = metrics.heightPixels;
screenWidth = metrics.widthPixels;
Log.i("MY", "Actual Screen Height = " + screenHeight + " Width = " + screenWidth);
// Now calculate the height that our layout can be set
// If you know that your application doesn't have statusBar added, then don't add here also. Same applies to application bar also
int layoutHeight = screenHeight - (titleBarHeight + statusBarHeight);
Log.i("MY", "Layout Height = " + layoutHeight);
// Lastly, set the height of the layout
// LinearLayout.LayoutParams rootParams = (android.widget.LinearLayout.LayoutParams)root.getLayoutParams();
// rootParams.height = layoutHeight;
// root.setLayoutParams(rootParams);
}
});
// (array => res/values/strings)
titulosMenu = getResources().getStringArray(R.array.drawer_items);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
lvdrawer = (ListView) findViewById(R.id.lvdrawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
View header = getLayoutInflater().inflate(R.layout.header, null);
lvdrawer.addHeaderView(header);
itemsMenu = new ArrayList<ItemDrawer>();
for (int i = 0; i < titulosMenu.length; i++) {
itemsMenu.add(new ItemDrawer(titulosMenu[i]));
}
AdaptadorDrawer adapter=new AdaptadorDrawer(this, R.layout.filaitem, itemsMenu);
lvdrawer.setAdapter(adapter);
lvdrawer.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int posicion,
long id) {
Intent intent;
switch (posicion) {
// case 1:
// break;
// case 2:
// break;
case 3:
intent = new Intent(MainActivity.this, Contacto.class);
startActivity(intent);
break;
}
drawerLayout.closeDrawers();
}
});
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
toggle = new ActionBarDrawerToggle(this, // Activity
drawerLayout, // Panel del Navigation Drawer
new Toolbar(MainActivity.this), // Icono que va a utilizar
R.string.menu, // Descripcion al abrir el drawer
R.string.app_name // Descripcion al cerrar el drawer
) {
@Override
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.menu);
invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View view) {
getActionBar().setTitle(TITULOREEM);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(toggle);
pager = (ViewPager)findViewById(R.id.viewPager);
pager.setAdapter(new AdaptadorPager(getSupportFragmentManager()));
TitlePageIndicator titleIndicator = (TitlePageIndicator)findViewById(R.id.titulo);
titleIndicator.setViewPager(pager);
LinearNews=(LinearLayout)findViewById(R.id.LinearNoticias);
putLinks();
}
}
问题来了,我调用了这个方法,但没有任何反应。如果我不使用 myContainer.findViewById 程序将崩溃并显示空指针异常,因为它找不到 ListView:
private void putLinks() {
lvLinks = (ListView)myContainer.findViewById(R.id.lvlinksnuevo);
listaLinks = new ArrayList<Link>();
listaLinks.add(new Link("test", "test", "link_afijma"));
AdaptadorLinks adaplink = new AdaptadorLinks(MainActivity.this, R.layout.linealinks , listaLinks);
lvLinks.setAdapter(adaplink);
pager.getAdapter().notifyDataSetChanged();
}
我不知道你是否需要这个,但无论如何:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
toggle.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (toggle.onOptionsItemSelected(item)) {
return true;
}
int id = item.getItemId();
if (id == R.id.menuDrawer) {
drawerLayout.openDrawer(Gravity.START);
return true;
}
return super.onOptionsItemSelected(item);
}
这是我的寻呼机适配器(我也试过使用 FragmentStatePagerAdapter):
public class AdaptadorPager extends FragmentPagerAdapter {
private final String[] titles = { "Presentación", "Noticias", "Links"};
public AdaptadorPager(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentoPresentacion();
case 1:
return new FragmentoNoticias();
case 2:
return new FragmentoLinks();
}
return null;
}
@Override
public int getCount() {
return titles.length;
}
// 3º Poner en el adaptador el método que devuelve la cadena del titulo
// Devuelve el título segun nos coloquemos en uno u otro fragmento
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
这是我的 ListView 适配器:
public class AdaptadorLinks extends ArrayAdapter<Link>{
private int layoutFila;
private LayoutInflater inflater;
private Context context;
public AdaptadorLinks(Context ctx, int resourceId, ArrayList<Link> objects) {
super(ctx, resourceId, objects);
layoutFila = resourceId;
inflater = LayoutInflater.from(ctx);
context=ctx;
}
@Override
public View getView(int position, View fila, ViewGroup parent) {
fila = inflater.inflate(layoutFila, null );
Link link = getItem(position);
ImageView iconfila=(ImageView) fila.findViewById(R.id.imageLineaLinks);
TextView titulofila = (TextView)fila.findViewById(R.id.tvLineaLinks1);
TextView urlfila = (TextView)fila.findViewById(R.id.tvLineaLinks2);
int id=context.getResources().getIdentifier(link.getImagen(), "drawable", context.getPackageName());
iconfila.setImageResource(id);
titulofila.setText(link.getTitulo());
urlfila.setText(link.getUrl());
return(fila);
}
}
设计的 xml 是:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content -->
<RelativeLayout
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#70000000" >
<LinearLayout
android:id="@+id/LinearNoticias"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/titulo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/horizontalScrollView1"
android:background="#95000000" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/titulo"
android:background="#80000000" />
</RelativeLayout>
<!-- Navigation Drawer -->
<ListView
android:id="@+id/lvdrawer"
android:layout_width="270dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>
链接片段:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutFragLinks"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/lvlinksnuevo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#90000000" >
</ListView>
</RelativeLayout>
LV 适配器线:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageLineaLinks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/link_emmen" />
<TextView
android:id="@+id/tvLineaLinks1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/imageLineaLinks"
android:layout_toRightOf="@+id/imageLineaLinks"
android:gravity="center"
android:text="TextView"
android:textColor="@color/white"
android:textSize="15sp" />
<TextView
android:id="@+id/tvLineaLinks2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvLineaLinks1"
android:layout_alignStart="@+id/tvLineaLinks1"
android:layout_below="@+id/tvLineaLinks1"
android:gravity="center"
android:text="TextView"
android:textColor="@color/white"
android:textSize="12sp" />
</RelativeLayout>
提前致谢。我希望有人能帮助我。
我的新片段代码:
public class FragmentoLinks extends Fragment {
private ListView lvLinks;
private ArrayList<Link> listaLinks;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.frag_links, container, false);
lvLinks = (ListView)getActivity().findViewById(R.id.lvlinksnuevo);
listaLinks = new ArrayList<Link>();
listaLinks.add(new Link("test", "test", "link_afijma"));
AdaptadorLinks adaplink = new AdaptadorLinks(getActivity(), R.layout.linealinks , listaLinks);
lvLinks.setAdapter(adaplink);
return layout;
}
}
我尝试使用 getView() 而不是 getActivity() 结果相同。
这是我现在得到的错误:
04-15 12:44:41.192: E/AndroidRuntime(32713): FATAL EXCEPTION: main
04-15 12:44:41.192: E/AndroidRuntime(32713): Process: com.aeioumusica.aplireem, PID: 32713
04-15 12:44:41.192: E/AndroidRuntime(32713): java.lang.NullPointerException
04-15 12:44:41.192: E/AndroidRuntime(32713): at com.aeioumusica.aplireem.FragmentoLinks.onCreateView(FragmentoLinks.java:32)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.view.ViewPager.run(ViewPager.java:249)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.view.Choreographer.doCallbacks(Choreographer.java:603)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.view.Choreographer.doFrame(Choreographer.java:572)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.os.Handler.handleCallback(Handler.java:733)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.os.Handler.dispatchMessage(Handler.java:95)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.os.Looper.loop(Looper.java:157)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.app.ActivityThread.main(ActivityThread.java:5356)
04-15 12:44:41.192: E/AndroidRuntime(32713): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 12:44:41.192: E/AndroidRuntime(32713): at java.lang.reflect.Method.invoke(Method.java:515)
04-15 12:44:41.192: E/AndroidRuntime(32713): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
04-15 12:44:41.192: E/AndroidRuntime(32713): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
04-15 12:44:41.192: E/AndroidRuntime(32713): at dalvik.system.NativeStart.main(Native Method)
在片段中,您应该在 onCreateView()
中进行视图设置,然后调用
getView().findViewById()
别处。
这是因为片段的生命周期不同于activity。
有关详细信息,请参阅 http://developer.android.com/guide/components/fragments.html#Creating。
myContainer = getLayoutInflater().inflate(R.layout.frag_links,null);
这会创建一个新的 RelativeLayout
片段布局,它没有附加到任何地方,也没有显示在屏幕上。它肯定不会 return 您要修改的组件,它是作为片段的一部分创建的。
根据您的代码,您想修改其中一个片段中的 ListView
。但是,该特定片段甚至可能不会显示,因为 ViewPager
决定您的片段何时创建。当您的代码被执行时,该片段可能还不存在,或者它可能不在屏幕上。
然而,最重要的是,片段并不总是 activity 的视图层次结构的一部分。因此 activity 上的 findViewById()
将找不到任何内容,因为视图可能根本不存在。
将您的 link 更新代码移动到片段中,并在片段布局膨胀后在片段内使用 layout.findViewById()
。在从布局 XML.
膨胀后,这会将 ListAdapter
附加到片段内正确的 ListView
尽管 Fragment
被实现为一个独立于
一个 Activity
并且可以在多个活动中使用,一个给定的
Fragment
的实例直接绑定到包含的 activity
它。
具体来说,该片段可以访问 Activity
实例
getActivity()
并轻松执行任务,例如在
Activity
布局:
View listView = getActivity().findViewById(R.id.list);
同样,您的 Activity
可以通过获取
使用 findFragmentById()
从 FragmentManager
引用 Fragment
或 findFragmentByTag()
。例如:
ExampleFragment fragment = (ExampleFragment)
getFragmentManager().findFragmentById(R.id.example_fragment);
在这里搜索了很多答案后,我似乎无法解决我的问题。 首先,我在使用 findViewById 从片段中获取 listView 时遇到问题,为了解决这个问题,我必须先膨胀片段的布局并使用 "mContainer.findViewById..".
我不知道这是否是 viewpager 未更新(或列表视图本身)的原因。
我的 OnCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myContainer = getLayoutInflater().inflate(R.layout.frag_links,null);
root = (RelativeLayout) findViewById(R.id.rootLayout);
root.post(new Runnable() {
@Override
public void run() {
Rect rect = new Rect();
Window win = getWindow(); // Get the Window
win.getDecorView().getWindowVisibleDisplayFrame(rect);
// Get the height of Status Bar
int statusBarHeight = rect.top;
// Get the height occupied by the decoration contents
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
// Calculate titleBarHeight by deducting statusBarHeight from contentViewTop
int titleBarHeight = contentViewTop - statusBarHeight;
Log.i("MY", "titleHeight = " + titleBarHeight + " statusHeight = " + statusBarHeight + " contentViewTop = " + contentViewTop);
// By now we got the height of titleBar & statusBar
// Now lets get the screen size
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenHeight = metrics.heightPixels;
screenWidth = metrics.widthPixels;
Log.i("MY", "Actual Screen Height = " + screenHeight + " Width = " + screenWidth);
// Now calculate the height that our layout can be set
// If you know that your application doesn't have statusBar added, then don't add here also. Same applies to application bar also
int layoutHeight = screenHeight - (titleBarHeight + statusBarHeight);
Log.i("MY", "Layout Height = " + layoutHeight);
// Lastly, set the height of the layout
// LinearLayout.LayoutParams rootParams = (android.widget.LinearLayout.LayoutParams)root.getLayoutParams();
// rootParams.height = layoutHeight;
// root.setLayoutParams(rootParams);
}
});
// (array => res/values/strings)
titulosMenu = getResources().getStringArray(R.array.drawer_items);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
lvdrawer = (ListView) findViewById(R.id.lvdrawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
View header = getLayoutInflater().inflate(R.layout.header, null);
lvdrawer.addHeaderView(header);
itemsMenu = new ArrayList<ItemDrawer>();
for (int i = 0; i < titulosMenu.length; i++) {
itemsMenu.add(new ItemDrawer(titulosMenu[i]));
}
AdaptadorDrawer adapter=new AdaptadorDrawer(this, R.layout.filaitem, itemsMenu);
lvdrawer.setAdapter(adapter);
lvdrawer.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int posicion,
long id) {
Intent intent;
switch (posicion) {
// case 1:
// break;
// case 2:
// break;
case 3:
intent = new Intent(MainActivity.this, Contacto.class);
startActivity(intent);
break;
}
drawerLayout.closeDrawers();
}
});
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
toggle = new ActionBarDrawerToggle(this, // Activity
drawerLayout, // Panel del Navigation Drawer
new Toolbar(MainActivity.this), // Icono que va a utilizar
R.string.menu, // Descripcion al abrir el drawer
R.string.app_name // Descripcion al cerrar el drawer
) {
@Override
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.menu);
invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View view) {
getActionBar().setTitle(TITULOREEM);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(toggle);
pager = (ViewPager)findViewById(R.id.viewPager);
pager.setAdapter(new AdaptadorPager(getSupportFragmentManager()));
TitlePageIndicator titleIndicator = (TitlePageIndicator)findViewById(R.id.titulo);
titleIndicator.setViewPager(pager);
LinearNews=(LinearLayout)findViewById(R.id.LinearNoticias);
putLinks();
}
}
问题来了,我调用了这个方法,但没有任何反应。如果我不使用 myContainer.findViewById 程序将崩溃并显示空指针异常,因为它找不到 ListView:
private void putLinks() {
lvLinks = (ListView)myContainer.findViewById(R.id.lvlinksnuevo);
listaLinks = new ArrayList<Link>();
listaLinks.add(new Link("test", "test", "link_afijma"));
AdaptadorLinks adaplink = new AdaptadorLinks(MainActivity.this, R.layout.linealinks , listaLinks);
lvLinks.setAdapter(adaplink);
pager.getAdapter().notifyDataSetChanged();
}
我不知道你是否需要这个,但无论如何:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
toggle.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (toggle.onOptionsItemSelected(item)) {
return true;
}
int id = item.getItemId();
if (id == R.id.menuDrawer) {
drawerLayout.openDrawer(Gravity.START);
return true;
}
return super.onOptionsItemSelected(item);
}
这是我的寻呼机适配器(我也试过使用 FragmentStatePagerAdapter):
public class AdaptadorPager extends FragmentPagerAdapter {
private final String[] titles = { "Presentación", "Noticias", "Links"};
public AdaptadorPager(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentoPresentacion();
case 1:
return new FragmentoNoticias();
case 2:
return new FragmentoLinks();
}
return null;
}
@Override
public int getCount() {
return titles.length;
}
// 3º Poner en el adaptador el método que devuelve la cadena del titulo
// Devuelve el título segun nos coloquemos en uno u otro fragmento
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
这是我的 ListView 适配器:
public class AdaptadorLinks extends ArrayAdapter<Link>{
private int layoutFila;
private LayoutInflater inflater;
private Context context;
public AdaptadorLinks(Context ctx, int resourceId, ArrayList<Link> objects) {
super(ctx, resourceId, objects);
layoutFila = resourceId;
inflater = LayoutInflater.from(ctx);
context=ctx;
}
@Override
public View getView(int position, View fila, ViewGroup parent) {
fila = inflater.inflate(layoutFila, null );
Link link = getItem(position);
ImageView iconfila=(ImageView) fila.findViewById(R.id.imageLineaLinks);
TextView titulofila = (TextView)fila.findViewById(R.id.tvLineaLinks1);
TextView urlfila = (TextView)fila.findViewById(R.id.tvLineaLinks2);
int id=context.getResources().getIdentifier(link.getImagen(), "drawable", context.getPackageName());
iconfila.setImageResource(id);
titulofila.setText(link.getTitulo());
urlfila.setText(link.getUrl());
return(fila);
}
}
设计的 xml 是:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content -->
<RelativeLayout
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#70000000" >
<LinearLayout
android:id="@+id/LinearNoticias"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/titulo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/horizontalScrollView1"
android:background="#95000000" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/titulo"
android:background="#80000000" />
</RelativeLayout>
<!-- Navigation Drawer -->
<ListView
android:id="@+id/lvdrawer"
android:layout_width="270dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>
链接片段:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutFragLinks"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/lvlinksnuevo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#90000000" >
</ListView>
</RelativeLayout>
LV 适配器线:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageLineaLinks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/link_emmen" />
<TextView
android:id="@+id/tvLineaLinks1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/imageLineaLinks"
android:layout_toRightOf="@+id/imageLineaLinks"
android:gravity="center"
android:text="TextView"
android:textColor="@color/white"
android:textSize="15sp" />
<TextView
android:id="@+id/tvLineaLinks2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvLineaLinks1"
android:layout_alignStart="@+id/tvLineaLinks1"
android:layout_below="@+id/tvLineaLinks1"
android:gravity="center"
android:text="TextView"
android:textColor="@color/white"
android:textSize="12sp" />
</RelativeLayout>
提前致谢。我希望有人能帮助我。
我的新片段代码:
public class FragmentoLinks extends Fragment {
private ListView lvLinks;
private ArrayList<Link> listaLinks;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.frag_links, container, false);
lvLinks = (ListView)getActivity().findViewById(R.id.lvlinksnuevo);
listaLinks = new ArrayList<Link>();
listaLinks.add(new Link("test", "test", "link_afijma"));
AdaptadorLinks adaplink = new AdaptadorLinks(getActivity(), R.layout.linealinks , listaLinks);
lvLinks.setAdapter(adaplink);
return layout;
}
}
我尝试使用 getView() 而不是 getActivity() 结果相同。
这是我现在得到的错误:
04-15 12:44:41.192: E/AndroidRuntime(32713): FATAL EXCEPTION: main
04-15 12:44:41.192: E/AndroidRuntime(32713): Process: com.aeioumusica.aplireem, PID: 32713
04-15 12:44:41.192: E/AndroidRuntime(32713): java.lang.NullPointerException
04-15 12:44:41.192: E/AndroidRuntime(32713): at com.aeioumusica.aplireem.FragmentoLinks.onCreateView(FragmentoLinks.java:32)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.support.v4.view.ViewPager.run(ViewPager.java:249)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.view.Choreographer.doCallbacks(Choreographer.java:603)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.view.Choreographer.doFrame(Choreographer.java:572)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.os.Handler.handleCallback(Handler.java:733)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.os.Handler.dispatchMessage(Handler.java:95)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.os.Looper.loop(Looper.java:157)
04-15 12:44:41.192: E/AndroidRuntime(32713): at android.app.ActivityThread.main(ActivityThread.java:5356)
04-15 12:44:41.192: E/AndroidRuntime(32713): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 12:44:41.192: E/AndroidRuntime(32713): at java.lang.reflect.Method.invoke(Method.java:515)
04-15 12:44:41.192: E/AndroidRuntime(32713): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
04-15 12:44:41.192: E/AndroidRuntime(32713): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
04-15 12:44:41.192: E/AndroidRuntime(32713): at dalvik.system.NativeStart.main(Native Method)
在片段中,您应该在 onCreateView()
中进行视图设置,然后调用
getView().findViewById()
别处。
这是因为片段的生命周期不同于activity。
有关详细信息,请参阅 http://developer.android.com/guide/components/fragments.html#Creating。
myContainer = getLayoutInflater().inflate(R.layout.frag_links,null);
这会创建一个新的 RelativeLayout
片段布局,它没有附加到任何地方,也没有显示在屏幕上。它肯定不会 return 您要修改的组件,它是作为片段的一部分创建的。
根据您的代码,您想修改其中一个片段中的 ListView
。但是,该特定片段甚至可能不会显示,因为 ViewPager
决定您的片段何时创建。当您的代码被执行时,该片段可能还不存在,或者它可能不在屏幕上。
然而,最重要的是,片段并不总是 activity 的视图层次结构的一部分。因此 activity 上的 findViewById()
将找不到任何内容,因为视图可能根本不存在。
将您的 link 更新代码移动到片段中,并在片段布局膨胀后在片段内使用 layout.findViewById()
。在从布局 XML.
ListAdapter
附加到片段内正确的 ListView
尽管 Fragment
被实现为一个独立于
一个 Activity
并且可以在多个活动中使用,一个给定的
Fragment
的实例直接绑定到包含的 activity
它。
具体来说,该片段可以访问 Activity
实例
getActivity()
并轻松执行任务,例如在
Activity
布局:
View listView = getActivity().findViewById(R.id.list);
同样,您的 Activity
可以通过获取
使用 findFragmentById()
从 FragmentManager
引用 Fragment
或 findFragmentByTag()
。例如:
ExampleFragment fragment = (ExampleFragment)
getFragmentManager().findFragmentById(R.id.example_fragment);