ViewPager没有从代码,android,eclipse更新

时间:2015-04-15 09:49:33

标签: android eclipse listview android-fragments android-viewpager

在这里搜索了很多答案后,我似乎无法解决我的问题。 首先我有一个问题,使用findViewById从片段中获取listView,为了解决这个问题我必须先扩充片段的布局并使用" mContainer.findViewById .."。

我不知道这是不是更新(或列表视图本身)的原因。

我的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程序将崩溃显示nullpointerexception,因为它无法找到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);
}

}

设计的xmls是:

<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$3.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)

3 个答案:

答案 0 :(得分:1)

在片段中,您应该在onCreateView()内进行视图设置,然后调用

getView().findViewById()

别处。

这是因为片段的生命周期与活动不同。

有关详细信息,请参阅http://developer.android.com/guide/components/fragments.html#Creating

答案 1 :(得分:0)

myContainer = getLayoutInflater().inflate(R.layout.frag_links,null);

这会创建一个带有片段布局的新RelativeLayout,它不会附加到任何地方,不会显示在屏幕上。它当然不会返回您想要修改的组件,它是作为片段的一部分创建的。

从您的代码中,您想要修改其中一个片段中的ListView。但是,可能甚至不显示该特定片段,因为ViewPager决定何时创建片段。执行代码时,片段可能尚不存在,或者可能不在屏幕上。

但最重要的是,片段并不总是活动视图层次结构的一部分。因此,活动findViewById()无法找到任何内容,因为视图可能根本不存在。

将链接更新代码移动到片段中,并在片段布局膨胀后在片段内使用layout.findViewById()。这将在片段从布局XML中膨胀后将ListAdapter附加到片段内的正确ListView

答案 2 :(得分:0)

虽然Fragment被实现为独立于的对象     一个Activity,可以在一个给定的多个活动中使用     Fragment的实例与包含的活动直接相关     它。     具体来说,片段可以访问Activity实例     getActivity()并轻松执行诸如查找视图等任务     Activity布局:

 View listView = getActivity().findViewById(R.id.list);

同样,您的Activity可以通过获取a来调用Fragment中的方法   使用Fragment引用FragmentManager中的findFragmentById()   或findFragmentByTag()。例如:

ExampleFragment fragment = (ExampleFragment)
getFragmentManager().findFragmentById(R.id.example_fragment);